resultList.add(new SearchResults.Result(phraseMatcher.start()));
/** * 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; }
/** * Attempt to advance the spliterator by one matching phrase. */ @Override public boolean tryAdvance(Consumer<? super Result> action) { // Try to find a phrase match in the input, ignoring case. If // there's no match then we're done with the iteration. if (!mPhraseMatcher.find()) return false; else { // Create/accept a new Result object that stores the index // of where the phrase occurs in the original string // (which is why we add mOffset). action.accept(new Result(mOffset + mPhraseMatcher.start())); // Indicate that the spliterator should continue. return true; } }
/** * Try to find phrase matches sequentially. */ private List<SearchResults.Result> computeSequentially () { return StreamSupport // Use the MatcherSpliterator to create a new stream of // MatchResults. .stream(new MatcherSpliterator(mPhraseMatcher), // Create a sequential stream. false) // Map each MatchResult into a SearchResults.Result. .map(mr -> new SearchResults.Result(mOffset + mr.start())) // Collect all the results into a list. .collect(toList()); }
/** * Add a Result. */ public void add(int index) { mList.add(new Result(index)); }