Package org.kiwiproject.regex
Class MatchSpliterator
- java.lang.Object
-
- java.util.Spliterators.AbstractSpliterator<MatchResult>
-
- org.kiwiproject.regex.MatchSpliterator
-
- All Implemented Interfaces:
Spliterator<MatchResult>
public class MatchSpliterator extends Spliterators.AbstractSpliterator<MatchResult>
This is aSpliteratorthat lets you traverseMatchResults of aMatcher. Generally you should use one of thestreammethods, but if you need aSpliteratorinstance 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
streammethod.Original blog post: https://bytefish.de/blog/matcher_spliterator/
-
-
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
Constructors Constructor Description MatchSpliterator(Matcher matcher)Create a new instance from the givenMatcher.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Stream<MatchResult>stream()Create aStreamofMatchResultfrom this instance.static Stream<MatchResult>stream(String regex, String input)Create aStreamofMatchResultusing the given regular expression and String input.static Stream<MatchResult>stream(Matcher matcher)static Stream<MatchResult>stream(Pattern pattern, String input)booleantryAdvance(Consumer<? super MatchResult> action)-
Methods inherited from class java.util.Spliterators.AbstractSpliterator
characteristics, estimateSize, trySplit
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Spliterator
forEachRemaining, getComparator, getExactSizeIfKnown, hasCharacteristics
-
-
-
-
Method Detail
-
tryAdvance
public boolean tryAdvance(Consumer<? super MatchResult> action)
-
stream
public Stream<MatchResult> stream()
Create aStreamofMatchResultfrom this instance.- Returns:
- a stream of match results
-
stream
public static Stream<MatchResult> stream(String regex, String input)
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
public static Stream<MatchResult> stream(Pattern pattern, String input)
- 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
public static Stream<MatchResult> stream(Matcher matcher)
- Parameters:
matcher- theMatcherto use- Returns:
- a stream of match results
- Throws:
IllegalArgumentException- if the matcher is null
-
-