/** * Attempt to split the input so phrases can be matched * concurrently. */ @Override public Spliterator<Result> trySplit() { // Bail out if the input is too small to split further. if (mInput.length() <= mMinSplitSize) return null; // Compute a candidate position for splitting the input. int startPos, splitPos = mInput.length() / 2; // Get the position to start determining if a phrase spans the // split position. if ((startPos = computeStartPos(splitPos)) < 0) return null; // Update splitPos if a phrase spans across the initial // splitPos. if ((splitPos = tryToUpdateSplitPos(startPos, splitPos)) < 0) return null; // Create a new Spliterator that handles the "left hand" // portion of the input, while the "this" object handles the // "right hand" portion of the input. return splitInput(splitPos); }
/** * Return a new Spliterator that handles the "left hand" portion * of the input, while "this" object handles the "right hand" * portion of the input. */ private Spliterator<Result> splitInput(int splitPos) { // Split the input at the appropriate location. CharSequence leftHandSide = mInput.subSequence(0, splitPos); // Update this field to account for the shorter input on the // "right hand" portion. mInput = mInput.subSequence(splitPos, mInput.length()); // Update this field to handle the shorter input. mPhraseMatcher = mPattern.matcher(mInput); // Create a new Spliterator that handles the "left hand" // portion of the input. Spliterator<Result> leftHalfSpliterator = new PhraseMatchSpliterator(leftHandSide, mPhrase, mPattern, mMinSplitSize, mOffset); // Update the offset. mOffset += splitPos; // Return a spliterator for the left half of the input, while // "this" object handles the "right hand" of the input. return leftHalfSpliterator; }
/** * 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); }