/** * Return a string version of the object. */ @Override public String toString() { String output = ""; if (!isEmpty()) { output += headerToString() // Create a string containing indices of all the matches. + "[" + mList // Convert list to a stream. .stream() // Create a custom collector to join all the results // together. .collect(Collector.of(() -> new StringJoiner("|"), // supplier (j, r) -> j.add(r.toString()), // accumulator StringJoiner::merge, // combiner StringJoiner::toString)) // finisher + "]"; } return output; }
return new SearchResults(Thread.currentThread().getId(), 0, phrase,
/** * Test PhraseMatchTask. */ @Test public void test() { // Create a tokenizer for the input. StringTokenizer tokenizer = new StringTokenizer(sINPUT_DATA, " "); // Conduct a sequential and parallel search for each entry in // the input. while (tokenizer.hasMoreElements()) { String nextWord = tokenizer.nextToken(); // Conduct a parallel search. SearchResults r1 = searchForPhrase(nextWord, sINPUT_DATA); // Get the results as strings. String parallelResult = r1.toString(); // Make sure the results are correct. assertNotEquals(0, r1.getResultList().size()); // Print the results. System.out.println (parallelResult + ": parallel"); } } }
if (results.size() > 0) listOfSearchResults.add(results);
/** * Looks for all instances of @code phrase in @code inputData and * return a list of all the @code SearchResults (if any). */ public SearchResults searchForPhrase(String phrase, CharSequence input, String title, boolean parallel) { List<SearchResults.Result> resultList = // Use a PhraseMatchSpliterator to add the indices of all // places in the inputData where phrase matches. StreamSupport // Create a stream of Results to record the indices // (if any) where the phrase matched the input data. .stream(new PhraseMatchSpliterator(input, phrase), parallel) // This terminal operation triggers aggregate // operation processing and returns a list of Results. .collect(toList()); // Create/return SearchResults to keep track of relevant info. return new SearchResults(Thread.currentThread().getId(), currentCycle(), phrase, title, resultList); }
-> new SearchResults (Thread.currentThread().getId(), 1,
/** * Search for a phrase in the input data. */ private static SearchResults searchForPhrase(String phrase, CharSequence inputData) { return new SearchResults (phrase, "", // Perform the processing (either sequentially or in // parallel) and return a list of Results. new PhraseMatchTask(inputData, phrase).compute()); }