All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kiwiproject.util.regex.MatcherSpliterator Maven / Gradle / Ivy

Go to download

Kiwi is a utility library. We really like Google's Guava, and also use Apache Commons. But if they don't have something we need, and we think it is useful, this is where we put it.

There is a newer version: 4.5.2
Show newest version
package org.kiwiproject.util.regex;

import static java.util.Objects.requireNonNull;

import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;

/**
 * A simple {@link java.util.Spliterator} over a {@link Matcher}.
 */
public class MatcherSpliterator extends Spliterators.AbstractSpliterator {

    private final Matcher matcher;

    public MatcherSpliterator(Matcher matcher) {
        super(Long.MAX_VALUE, ORDERED | NONNULL | IMMUTABLE);
        this.matcher = matcher;
    }

    @Override
    public boolean tryAdvance(Consumer action) {
        requireNonNull(action, "action cannot be null");

        if (!matcher.find()) {
            return false;
        }

        action.accept(matcher.toMatchResult());
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy