/** * 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; } }
resultList.add(new SearchResults.Result(phraseMatcher.start()));
/** * 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)); }