/** * This method searches the @a inputString for all occurrences of * the phrases to find. */ @Override public List<SearchResults> compute() { if (mPhraseList.size() < mMinSplitSize) return computeSequentially(); else // Compute position to split the phrase list and forward // to the splitPhraseList() method to perform the split. return splitPhraseList(mPhraseList.size() / 2); }
/** * Use the fork-join framework to recursively split the input list * and return a list of SearchResults that contain all matching * phrases in the input list. */ private List<SearchResults> splitPhraseList(int splitPos) { // Create and fork a new SearchWithForkJoinTask that // concurrently handles the "left hand" part of the input, // while "this" handles the "right hand" part of the input. ForkJoinTask<List<SearchResults>> leftTask = new SearchForPhrasesTask(mInputString, mPhraseList.subList(0, splitPos), mMinSplitSize).fork(); // Update "this" SearchForPhrasesTask to handle the "right // hand" portion of the input. mPhraseList = mPhraseList.subList(splitPos, mPhraseList.size()); // Recursively call compute() to continue the splitting. List<SearchResults> rightResult = compute(); // Wait and join the results from the left task. List<SearchResults> leftResult = leftTask.join(); // sConcatenate the left result with the right result. leftResult.addAll(rightResult); // Return the result. return leftResult; }
/** * Perform the computations sequentially at this point. */ private List<List<SearchResults>> computeSequentially() { // Return the list of lists of SearchResults. return mInputList // Convert the list of input strings into a stream. .stream() // Create a SearchForPhrasesTask that searches an input // string for a list of phrases and store the results from // computing the task. .map(input -> new SearchForPhrasesTask(input, mPhrasesToFind).compute()) // If a phrase was found add it to the list of results. .filter(not(List<SearchResults>::isEmpty)) // Trigger stream processing and collect the results into a // list. .collect(toList()); }
String title = getTitle(mInputString);