com.github.wellcomer.query3.core.Autocomplete.java Source code

Java tutorial

Introduction

Here is the source code for com.github.wellcomer.query3.core.Autocomplete.java

Source

/*
 * Copyright (c) 2016. Sergey V. Katunin <sergey.blaster@gmail.com>
 * Licensed under the Apache License, Version 2.0
 */

package com.github.wellcomer.query3.core;

import org.apache.commons.lang.StringUtils;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.*;

/**
 * <h3>?.</h3>
 *
*/

public class Autocomplete {

    private String filePath;
    private Charset charset;
    private int maxItems;

    /**
        * @param filePath    
        * @param fileCharset  
        * @param maxItems ? ?   
        */
    public Autocomplete(String filePath, String fileCharset, int maxItems) {
        this.filePath = filePath;
        charset = Charset.forName(fileCharset);
        this.maxItems = maxItems;
    }

    /**
         ?? ? ?.
         ?  ,   ? ? ? regex.
        
        @param fileName ? .
        @param regex ? .
    */
    public List<String> get(String fileName, String regex) throws IOException {

        List<String> out = new ArrayList<>();

        if (regex == null || regex.equals(""))
            return out;

        List<String> lines = Files.readAllLines(Paths.get(filePath, fileName), charset);

        regex = regex.toLowerCase(); // ?? ?

        for (String line : lines) {
            if (line.toLowerCase().matches(".*" + regex + ".*")) {
                out.add(line.trim());
                if (maxItems == 0) // ?? 
                    continue;
                if (out.size() >= maxItems)
                    break;
            }
        }
        return out;
    }

    /**
        ?.   ?,  TreeSet ?  ? ?. ? TreeSet   .
        
        @param queryList ?? ?.
        @param scanModifiedOnly ?   ?.
        @param mergePrevious ?? ?  ??  .
    */
    public void autolearn(QueryList queryList, boolean scanModifiedOnly, boolean mergePrevious) throws IOException {

        FileTime timestamp;
        long modifiedSince = 0;
        Path timestampFilePath = Paths.get(filePath, ".timestamp");

        if (scanModifiedOnly) { //   
            try { //  ?   ? ?
                timestamp = Files.getLastModifiedTime(timestampFilePath);
                modifiedSince = timestamp.toMillis();
            } catch (IOException e) { //    ?    ? 
                Files.createFile(timestampFilePath);
            }
        }

        HashMap<String, TreeSet<String>> fields = new HashMap<>(); //  - ? ?,  -  ? 
        Iterator<Query> queryIterator = queryList.iterator(modifiedSince); // ? ?? ? ?  ?

        String k, v;

        while (queryIterator.hasNext()) {

            Query query = queryIterator.next();

            for (Map.Entry<String, String> entry : query.entrySet()) {

                k = entry.getKey().toLowerCase();
                v = entry.getValue().trim();

                if (v.length() < 2)
                    continue;

                if (!fields.containsKey(k)) {

                    TreeSet<String> treeSet = new TreeSet<>();

                    try {
                        if (mergePrevious) { // ? ?  
                            List<String> lines = Files.readAllLines(Paths.get(filePath, k), charset);
                            treeSet.addAll(lines);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    fields.put(k, treeSet);
                }
                TreeSet<String> treeSet = fields.get(k);
                treeSet.add(v);
            }
        }

        for (Map.Entry<String, TreeSet<String>> entry : fields.entrySet()) {

            k = entry.getKey();
            ArrayList<String> lines = new ArrayList<>(fields.get(k));

            FileWriter fileWriter = new FileWriter(Paths.get(filePath, k).toString());
            fileWriter.write(StringUtils.join(lines, System.getProperty("line.separator")));
            fileWriter.flush();
            fileWriter.close();
        }

        try {
            Files.setLastModifiedTime(timestampFilePath, FileTime.fromMillis(System.currentTimeMillis()));
        } catch (IOException e) {
            if (e.getClass().getSimpleName().equals("NoSuchFileException"))
                Files.createFile(timestampFilePath);
            e.printStackTrace();
        }
    }
}