com.compilit.validation.ContinuingRuleValidationBuilder Maven / Gradle / Ivy
package com.compilit.validation;
import com.compilit.validation.api.ActionValidationBuilder;
import com.compilit.validation.api.ContinuingValidationBuilder;
import com.compilit.validation.api.ReturningValidationBuilder;
import com.compilit.validation.api.Rule;
import com.compilit.validation.api.VoidValidationBuilder;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
final class ContinuingRuleValidationBuilder extends AbstractLoggingValidatable implements
ContinuingValidationBuilder {
ContinuingRuleValidationBuilder(final Subject subject) {
super(subject);
}
@Override
public ContinuingValidationBuilder and(final Rule rule) {
subject.addRule(rule);
return this;
}
@Override
public ContinuingValidationBuilder and(Rule.WithDualInput rule) {
subject.addDualInputRule(rule);
return this;
}
@Override
public ActionValidationBuilder thenApply(final Consumer consumer) {
subject.addIntermediateAction(consumer);
return this;
}
@Override
public ReturningValidationBuilder andThen(final Consumer consumer) {
return new ReturningRuleValidationBuilder<>(subject, consumer);
}
@Override
public ReturningValidationBuilder andThen(final Supplier supplier) {
return new ReturningRuleValidationBuilder<>(subject, supplier);
}
@Override
public VoidValidationBuilder andThen(final Runnable runnable) {
return new VoidRuleValidatableBuilder<>(subject, runnable);
}
@Override
public ReturningValidationBuilder andThen(final Function function) {
return new ReturningRuleValidationBuilder<>(subject, function);
}
@Override
public T orElseThrow(final Function throwableFunction) {
final var isValid = subject.validate();
if (!isValid) {
throw throwableFunction.apply(subject.getMessage());
}
subject.processIntermediateActions();
return subject.getValue();
}
@Override
public T orElseReturn(final T other) {
final var isValid = subject.validate();
if (!isValid) {
return other;
}
subject.processIntermediateActions();
return subject.getValue();
}
@Override
public T orElseReturn(final Function other) {
final var isValid = subject.validate();
if (!isValid) {
return other.apply(subject.getMessage());
}
subject.processIntermediateActions();
return subject.getValue();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy