Package org.kiwiproject.regex
Class MatchSpliterator
- All Implemented Interfaces:
Spliterator<MatchResult>
This is a
Spliterator that lets you traverse MatchResults of a Matcher.
Generally you should use one of the stream methods, but if you need a Spliterator
instance you can use the constructor directly.
Gratefully included here in Kiwi courtesy of Philipp Wagner from his "A Spliterator for MatchResults in Java"
blog entry. Slightly modified to check arguments and add an instance stream method.
Original blog post: A Spliterator for MatchResults in Java by Philipp Wagner.
-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.Spliterator
Spliterator.OfDouble, Spliterator.OfInt, Spliterator.OfLong, Spliterator.OfPrimitive<T extends Object,T_CONS extends Object, T_SPLITR extends Spliterator.OfPrimitive<T, T_CONS, T_SPLITR>> -
Field Summary
Fields inherited from interface java.util.Spliterator
CONCURRENT, DISTINCT, IMMUTABLE, NONNULL, ORDERED, SIZED, SORTED, SUBSIZED -
Constructor Summary
ConstructorsConstructorDescriptionMatchSpliterator(Matcher matcher) Create a new instance from the givenMatcher. -
Method Summary
Modifier and TypeMethodDescriptionstream()Create aStreamofMatchResultfrom this instance.static Stream<MatchResult>Create aStreamofMatchResultusing the given regular expression and String input.static Stream<MatchResult>static Stream<MatchResult>booleantryAdvance(Consumer<? super MatchResult> action) Methods inherited from class java.util.Spliterators.AbstractSpliterator
characteristics, estimateSize, trySplitMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Spliterator
forEachRemaining, getComparator, getExactSizeIfKnown, hasCharacteristics
-
Constructor Details
-
MatchSpliterator
Create a new instance from the givenMatcher. Usually you won't use this directly. Instead, use thestreammethods.- Parameters:
matcher- the Matcher to split
-
-
Method Details
-
tryAdvance
-
stream
Create aStreamofMatchResultfrom this instance.- Returns:
- a stream of match results
-
stream
Create aStreamofMatchResultusing the given regular expression and String input.This method will compile a new
Patternevery time it is called, even if the same regular expression is supplied. Compiling regular expressions is considered an "expensive" operation. Therefore, you should generally preferstream(Pattern, String)since you can precompile aPatternand re-use it many times.- Parameters:
regex- the regular expression as a Stringinput- the input to match against- Returns:
- a stream of match results
- Throws:
IllegalArgumentException- if the regex is blank
-
stream
- Parameters:
pattern- thePatternto use when matching the inputinput- the input to match against- Returns:
- a stream of match results
- Throws:
IllegalArgumentException- if the pattern is null
-
stream
- Parameters:
matcher- theMatcherto use- Returns:
- a stream of match results
- Throws:
IllegalArgumentException- if the matcher is null
-