<commit_before>// @@author A0130195M

package jfdi.logic.commands;

import jfdi.logic.events.SearchDoneEvent;
import jfdi.logic.interfaces.Command;
import jfdi.storage.apis.TaskAttributes;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.stream.Collectors;

/**
 * @author <NAME>
 */
public class SearchCommand extends Command {

    private HashSet<String> keywords;

    private SearchCommand(Builder builder) {
        this.keywords = builder.keywords;
    }

    public HashSet<String> getKeywords() {
        return keywords;
    }

    public static class Builder {

        HashSet<String> keywords = new HashSet<>();

        public Builder addKeyword(String keyword) {
            this.keywords.add(keyword);
            return this;
        }

        public Builder addKeywords(Collection<String> keywords) {
            this.keywords.addAll(keywords);
            return this;
        }

        public SearchCommand build() {
            return new SearchCommand(this);
        }

    }

    @Override
    public void execute() {
        ArrayList<TaskAttributes> results = new ArrayList<>();
        results.addAll(getFullMatches());
        results.addAll(getPartialMatches());

        eventBus.post(new SearchDoneEvent(results, keywords));
        System.out.println(results);
    }

    private ArrayList<TaskAttributes> getFullMatches() {
        return taskDb.getAll().stream().filter(task -> {
            for (String keyword : keywords) {
                if (!task.getDescription().matches(String.format("(?i:.*\\b%s\\b.*)", keyword))) {
                    return false;
                }
            }
            return true;
        }).collect(Collectors.toCollection(ArrayList::new));
    }

    private ArrayList<TaskAttributes> getPartialMatches() {
        return taskDb.getAll().stream().filter(task -> {
            int matches = 0;
            for (String keyword : keywords) {
                if (task.getDescription().matches(String.format("(?i:.*\\b%s\\b.*)", keyword))) {
                    matches++;
                }
            }
            return matches != 0 && matches != keywords.size();
        }).collect(Collectors.toCollection(ArrayList::new));
    }

    @Override
    public void undo() {
        assert false;<commit_msg>Allow to get results from `SearchCommand`<commit_after>// @@author A0130195M

package jfdi.logic.commands;

import jfdi.logic.events.SearchDoneEvent;
import jfdi.logic.interfaces.Command;
import jfdi.storage.apis.TaskAttributes;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.stream.Collectors;

/**
 * @author <NAME>
 */
public class SearchCommand extends Command {

    private HashSet<String> keywords;

    private ArrayList<TaskAttributes> results;

    private SearchCommand(Builder builder) {
        this.keywords = builder.keywords;
    }

    public HashSet<String> getKeywords() {
        return keywords;
    }

    public ArrayList<TaskAttributes> getResults() {
        return results;
    }

    public static class Builder {

        HashSet<String> keywords = new HashSet<>();

        public Builder addKeyword(String keyword) {
            this.keywords.add(keyword);
            return this;
        }

        public Builder addKeywords(Collection<String> keywords) {
            this.keywords.addAll(keywords);
            return this;
        }

        public SearchCommand build() {
            return new SearchCommand(this);
        }

    }

    @Override
    public void execute() {
        results = new ArrayList<>();
        results.addAll(getFullMatches());
        results.addAll(getPartialMatches());

        eventBus.post(new SearchDoneEvent(results, keywords));
    }

    private ArrayList<TaskAttributes> getFullMatches() {
        return taskDb.getAll().stream().filter(task -> {
            for (String keyword : keywords) {
                if (!task.getDescription().matches(String.format("(?i:.*\\b%s\\b.*)", keyword))) {
                    return false;
                }
            }
            return true;
        }).collect(Collectors.toCollection(ArrayList::new));
    }

    private ArrayList<TaskAttributes> getPartialMatches() {
        return taskDb.getAll().stream().filter(task -> {
            int matches = 0;
            for (String keyword : keywords) {
                if (task.getDescription().matches(String.format("(?i:.*\\b%s\\b.*)", keyword))) {
                    matches++;
                }
            }
            return matches != 0 && matches != keywords.size();
        }).collect(Collectors.toCollection(ArrayList::new));
    }

    @Override
    public void undo() {
        assert false;
{'fraction_non_alphanumeric': 0.12147368421052632, 'fraction_numerical': 0.003789473684210526, 'mean_word_length': 2.245218579234973, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '131231', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1226, 'n_words': 262}