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

org.solidcoding.validation.api.ContinuingPredicateValidator Maven / Gradle / Ivy

There is a newer version: 2.0.1
Show newest version
package org.solidcoding.validation.api;

import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

final class ContinuingPredicateValidator implements ContinuingValidator {

    static final String DEFAULT_MESSAGE = "Nothing to report";
    private final List> ruleDefinitions;
    private final T value;
    private String message;

    ContinuingPredicateValidator(List> ruleDefinitions, T value) {
        this.ruleDefinitions = ruleDefinitions;
        this.value = value;
    }

    @Override
    public String getMessage() {
        if (message == null)
            return DEFAULT_MESSAGE;
        return message;
    }

    @Override
    public ContinuingValidator and(Rule rule) {
        ruleDefinitions.add(rule);
        return this;
    }

    @Override
    public boolean validate() {
        message = DEFAULT_MESSAGE;
        var stringBuilder = new StringBuilder();
        var isValid = true;
        for (var ruleDefinition : ruleDefinitions) {
            if (!ruleDefinition.test(value)) {
                stringBuilder.append("Rule broken: ").append(ruleDefinition.getFailMessage()).append("\n");
                isValid = false;
            }
        }
        if (!isValid) {
            message = stringBuilder.toString();
        }
        return isValid;
    }

    @Override
    public  ReturningValidator andThen(Supplier supplier) {
        return new EndingValidator<>(supplier, this);
    }

    @Override
    public VoidValidator andThen(Runnable runnable) {
        return new VoidEndingValidator<>(runnable, this);
    }


    @Override
    public  Boolean orElseThrow(Function throwableFunction) {
        var isValid = validate();
        if (!isValid) {
            throw throwableFunction.apply(getMessage());
        }
        return true;
    }

    @Override
    public T orElseReturn(T other) {
        var isValid = validate();
        if (!isValid) {
            return other;
        }
        return value;
    }

    @Override
    public T orElseReturn(Function other) {
        var isValid = validate();
        if (!isValid) {
            return other.apply(message);
        }
        return value;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy