org.paumard.spliterators.ValidatingSpliterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of streams-utils Show documentation
Show all versions of streams-utils Show documentation
Streams Utils is a set of operations written on Java 8 Streams. It allows several basic operations that
are not available in the the Java 8, and that have proven to be very useful in some cases.
/*
* Copyright (C) 2015 José Paumard
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.paumard.spliterators;
import org.paumard.streams.StreamsUtils;
import java.util.Objects;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* See the documentation and patterns to be used in this class in the {@link StreamsUtils} factory class.
*
* @author José
*/
public class ValidatingSpliterator implements Spliterator {
private final Spliterator spliterator;
private final Function super E, ? extends R> transformIfValid, transformIfNotValid;
private final Predicate super E> validator;
public static class Builder {
private Spliterator spliterator;
private Function super E, ? extends R> transformIfValid, transformIfNotValid;
private Predicate super E> validator;
public Builder() {
}
public Builder with(Spliterator spliterator) {
this.spliterator = Objects.requireNonNull(spliterator);
return this;
}
public Builder validatedBy(Predicate super E> validator) {
this.validator = Objects.requireNonNull(validator);
return this;
}
public Builder withValidFunction(Function super E, ? extends R> validFunction) {
this.transformIfValid = Objects.requireNonNull(validFunction);
return this;
}
public Builder withNotValidFunction(Function super E, ? extends R> notValidFunction) {
this.transformIfNotValid = Objects.requireNonNull(notValidFunction);
return this;
}
public ValidatingSpliterator build() {
return new ValidatingSpliterator<>(spliterator, validator, transformIfValid, transformIfNotValid);
}
}
ValidatingSpliterator(
Spliterator spliterator, Predicate super E> validator,
Function super E, ? extends R> transformIfValid, Function super E, ? extends R> transformIfNotValid) {
this.spliterator = Objects.requireNonNull(spliterator, "spliterator");
this.validator = Objects.requireNonNull(validator, "validator");
this.transformIfValid = Objects.requireNonNull(transformIfValid, "validFunction");
this.transformIfNotValid = Objects.requireNonNull(transformIfNotValid, "notValidFunction");
}
@Override
public boolean tryAdvance(Consumer super R> action) {
return this.spliterator.tryAdvance(
e -> {
if (validator.test(e)) {
action.accept(transformIfValid.apply(e));
} else {
action.accept(transformIfNotValid.apply(e));
}
}
);
}
@Override
public Spliterator trySplit() {
Spliterator split = this.spliterator.trySplit();
return split == null ? null : new ValidatingSpliterator<>(split, validator, transformIfValid, transformIfNotValid);
}
@Override
public long estimateSize() {
return spliterator.estimateSize();
}
@Override
public int characteristics() {
return this.spliterator.characteristics();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy