A Spliterator for MatchResults in Java

I recently needed to iterate over the MatchResult of a Matcher, so I have implemented an AbstractSpliterator for it.

// Copyright (c) Philipp Wagner. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class MatchSpliterator extends Spliterators.AbstractSpliterator<MatchResult> {

    private Matcher matcher;

    public MatchSpliterator(Matcher matcher) {
        super(Long.MAX_VALUE, NONNULL | ORDERED);

        this.matcher = matcher;
    }

    @Override
    public boolean tryAdvance(Consumer<? super MatchResult> action) {
        if(matcher.find()) {
            action.accept(matcher.toMatchResult());
            return true;
        }
        return false;
    }

    public static Stream<MatchResult> stream(String regex, String input) {
        return stream(Pattern.compile(regex), input);
    }

    public static Stream<MatchResult> stream(Pattern pattern, String input) {
        return stream(pattern.matcher(input));
    }

    public static Stream<MatchResult> stream(Matcher matcher) {
        return StreamSupport.stream(new MatchSpliterator(matcher), false);
    }
}

How to contribute

One of the easiest ways to contribute is to participate in discussions. You can also contribute by submitting pull requests.

General feedback and discussions?

Do you have questions or feedback on this article? Please create an issue on the GitHub issue tracker.

Something is wrong or missing?

There may be something wrong or missing in this article. If you want to help fixing it, then please make a Pull Request to this file on GitHub.