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

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

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

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

final class EndingValidator extends AbstractLoggingValidator implements ReturningValidator {

    private final Supplier supplier;

    EndingValidator(Supplier supplier, ContinuingValidator validator) {
        super(validator);
        this.supplier = supplier;
    }

    public  R orElseThrow(Function throwableFunction) {
        var isValid = validator.validate();
        if (!isValid) {
            throw throwableFunction.apply(validator.getMessage());
        }
        return supplier.get();
    }

    @Override
    public R orElseReturn(R other) {
        var isValid = validator.validate();
        if (!isValid) {
            return other;
        }
        return supplier.get();
    }

    @Override
    public R orElseReturn(Function other) {
        var isValid = validator.validate();
        var message = validator.getMessage();
        if (!isValid) {
            return other.apply(message);
        }
        return supplier.get();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy