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

functionalj.result.Validation Maven / Gradle / Ivy

// ============================================================================
// Copyright (c) 2017-2019 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ============================================================================
package functionalj.result;

import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import functionalj.function.Absent;
import functionalj.lens.core.LensSpec;
import functionalj.lens.lenses.BooleanAccess;
import functionalj.lens.lenses.ObjectLens;
import functionalj.lens.lenses.ObjectLensImpl;
import functionalj.lens.lenses.ResultAccess;
import functionalj.lens.lenses.StringLens;
import functionalj.pipeable.Pipeable;
import functionalj.result.Specs.ValidationSpec;
import lombok.val;
import nullablej.utils.reflection.UProxy;

@SuppressWarnings({"javadoc", "rawtypes", "unchecked"})
public abstract class Validation implements Pipeable> {
    
    public static final  ToBoolean ToBoolean(Function checker, String messageTemplate) {
        return new ToBoolean(checker, messageTemplate);
    }
    public static final  ToMessage ToMessage(Function errorMsg) {
        return new ToMessage(errorMsg);
    }
    public static final  ToException ToException(Function errorChecker) {
        return new ToException(errorChecker);
    }
    
    private final ValidationSpec __spec = UProxy.createDefaultProxy(ValidationSpec.class);
    
    public static final ValidationLens theValidation = new ValidationLens<>(LensSpec.of(Validation.class));
    public static class ValidationLens extends ObjectLensImpl {

        public final BooleanAccess isToBoolean = Validation::isToBoolean;
        public final BooleanAccess isToMessage = Validation::isToMessage;
        public final BooleanAccess isToException = Validation::isToException;
        public final ResultAccess> asToBoolean = createSubResultLens(Validation::asToBoolean,           null, spec -> new ToBoolean.ToBooleanLens(spec));
        public final ResultAccess> asToMessage = createSubResultLens(Validation::asToMessage,           null, spec -> new ToMessage.ToMessageLens(spec));
        public final ResultAccess> asToException = createSubResultLens(Validation::asToException, null, spec -> new ToException.ToExceptionLens(spec));
        public ValidationLens(LensSpec spec) {
            super(spec);
        }
    }
    
    private Validation() {}
    public Validation __data() throws Exception { return this; }
    public Result> toResult() { return Result.valueOf(this); }
    
    public static final class ToBoolean extends Validation {
        public static final ToBooleanLens theToBoolean = new ToBooleanLens<>(LensSpec.of(ToBoolean.class));
        private Function checker;
        private String messageTemplate;
        private ToBoolean(Function checker, String messageTemplate) {
            this.checker = $utils.notNull(checker);
            this.messageTemplate = $utils.notNull(messageTemplate);
        }
        public Function checker() { return checker; }
        public String messageTemplate() { return messageTemplate; }
        public ToBoolean withChecker(Function checker) { return new ToBoolean(checker, messageTemplate); }
        public ToBoolean withMessageTemplate(String messageTemplate) { return new ToBoolean(checker, messageTemplate); }
        public static class ToBooleanLens extends ObjectLensImpl {
            
            public final ObjectLens checker = (ObjectLens)createSubLens(Validation.ToBoolean::checker, Validation.ToBoolean::withChecker, ObjectLens::of);
            public final StringLens messageTemplate = (StringLens)createSubLens(Validation.ToBoolean::messageTemplate, Validation.ToBoolean::withMessageTemplate, StringLens::of);
            
            public ToBooleanLens(LensSpec spec) {
                super(spec);
            }
            
        }
    }
    public static final class ToMessage extends Validation {
        public static final ToMessageLens theToMessage = new ToMessageLens<>(LensSpec.of(ToMessage.class));
        private Function errorMsg;
        private ToMessage(Function errorMsg) {
            this.errorMsg = $utils.notNull(errorMsg);
        }
        public Function errorMsg() { return errorMsg; }
        public ToMessage withErrorMsg(Function errorMsg) { return new ToMessage(errorMsg); }
        public static class ToMessageLens extends ObjectLensImpl {
            
            public final ObjectLens errorMsg = (ObjectLens)createSubLens(Validation.ToMessage::errorMsg, Validation.ToMessage::withErrorMsg, ObjectLens::of);
            
            public ToMessageLens(LensSpec spec) {
                super(spec);
            }
            
        }
    }
    public static final class ToException extends Validation {
        public static final ToExceptionLens theToException = new ToExceptionLens<>(LensSpec.of(ToException.class));
        private Function errorChecker;
        private ToException(Function errorChecker) {
            this.errorChecker = $utils.notNull(errorChecker);
        }
        public Function errorChecker() { return errorChecker; }
        public ToException withErrorChecker(Function errorChecker) { return new ToException(errorChecker); }
        public static class ToExceptionLens extends ObjectLensImpl {
            
            public final ObjectLens errorChecker = (ObjectLens)createSubLens(Validation.ToException::errorChecker, Validation.ToException::withErrorChecker, ObjectLens::of);
            
            public ToExceptionLens(LensSpec spec) {
                super(spec);
            }
            
        }
    }
    
    private final ValidationFirstSwitch __switch = new ValidationFirstSwitch(this);
    public ValidationFirstSwitch match() {
         return __switch;
    }
    
    private volatile String toString = null;
    @Override
    public String toString() {
        if (toString != null)
            return toString;
        synchronized(this) {
            if (toString != null)
                return toString;
            toString = match()
                    .toBoolean(toBoolean -> "ToBoolean(" + String.format("%1$s,%2$s", toBoolean.checker,toBoolean.messageTemplate) + ")")
                    .toMessage(toMessage -> "ToMessage(" + String.format("%1$s", toMessage.errorMsg) + ")")
                    .toException(toException -> "ToException(" + String.format("%1$s", toException.errorChecker) + ")")
            ;
            return toString;
        }
    }
    
    @Override
    public int hashCode() {
        return toString().hashCode();
    }
    
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Validation))
            return false;
        
        if (this == obj)
            return true;
        
        String objToString  = obj.toString();
        String thisToString = this.toString();
        return thisToString.equals(objToString);
    }
    
    public boolean ensureValid(D data) {
        return __spec.ensureValid(this, data);
    }
    public functionalj.result.ValidationException validate(D data) {
        return __spec.validate(this, data);
    }
    public functionalj.validator.Validator toValidator() {
        return __spec.toValidator(this);
    }
    
    public boolean isToBoolean() { return this instanceof ToBoolean; }
    public Result> asToBoolean() { return Result.valueOf(this).filter(ToBoolean.class).map(ToBoolean.class::cast); }
    public Validation ifToBoolean(Consumer> action) { if (isToBoolean()) action.accept((ToBoolean)this); return this; }
    public Validation ifToBoolean(Runnable action) { if (isToBoolean()) action.run(); return this; }
    public boolean isToMessage() { return this instanceof ToMessage; }
    public Result> asToMessage() { return Result.valueOf(this).filter(ToMessage.class).map(ToMessage.class::cast); }
    public Validation ifToMessage(Consumer> action) { if (isToMessage()) action.accept((ToMessage)this); return this; }
    public Validation ifToMessage(Runnable action) { if (isToMessage()) action.run(); return this; }
    public boolean isToException() { return this instanceof ToException; }
    public Result> asToException() { return Result.valueOf(this).filter(ToException.class).map(ToException.class::cast); }
    public Validation ifToException(Consumer> action) { if (isToException()) action.accept((ToException)this); return this; }
    public Validation ifToException(Runnable action) { if (isToException()) action.run(); return this; }
    
    public static class ValidationFirstSwitch {
        private Validation $value;
        private ValidationFirstSwitch(Validation theValue) { this.$value = theValue; }
        public  ValidationFirstSwitchTyped toA(Class clzz) {
            return new ValidationFirstSwitchTyped($value);
        }
        
        public  ValidationSwitchToMessageToException toBoolean(Function, TARGET> theAction) {
            Function, TARGET> $action = null;
            Function, TARGET> oldAction = (Function, TARGET>)$action;
            Function, TARGET> newAction =
                ($action != null)
                ? oldAction : 
                    ($value instanceof ToBoolean)
                    ? (Function, TARGET>)(d -> theAction.apply((ToBoolean)d))
                    : oldAction;
            
            return new ValidationSwitchToMessageToException($value, newAction);
        }
        public  ValidationSwitchToMessageToException toBoolean(Supplier theSupplier) {
            return toBoolean(d->theSupplier.get());
        }
        public  ValidationSwitchToMessageToException toBoolean(TARGET theValue) {
            return toBoolean(d->theValue);
        }
        
        public  ValidationSwitchToBooleanToMessageToException toBoolean(Predicate> check, Function, TARGET> theAction) {
            Function, TARGET> $action = null;
            Function, TARGET> oldAction = (Function, TARGET>)$action;
            Function, TARGET> newAction =
                ($action != null)
                ? oldAction : 
                    (($value instanceof ToBoolean) && check.test((ToBoolean)$value))
                    ? (Function, TARGET>)(d -> theAction.apply((ToBoolean)d))
                    : oldAction;
            
            return new ValidationSwitchToBooleanToMessageToException($value, newAction);
        }
        public  ValidationSwitchToBooleanToMessageToException toBoolean(Predicate> check, Supplier theSupplier) {
            return toBoolean(check, d->theSupplier.get());
        }
        public  ValidationSwitchToBooleanToMessageToException toBoolean(Predicate> check, TARGET theValue) {
            return toBoolean(check, d->theValue);
        }
        
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Absent messageTemplate, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker), theAction);
        }
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Absent messageTemplate, Supplier theSupplier) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker), theSupplier);
        }
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Absent messageTemplate, TARGET theValue) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker), theValue);
        }
        
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Absent messageTemplate, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker), theAction);
        }
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Absent messageTemplate, Supplier theSupplier) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker), theSupplier);
        }
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Absent messageTemplate, TARGET theValue) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker), theValue);
        }
        
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, String aMessageTemplate, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theAction);
        }
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, String aMessageTemplate, Supplier theSupplier) {
            return toBoolean(toBoolean -> $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theSupplier);
        }
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, String aMessageTemplate, TARGET theValue) {
            return toBoolean(toBoolean -> $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theValue);
        }
        
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, String aMessageTemplate, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theAction);
        }
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, String aMessageTemplate, Supplier theSupplier) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theSupplier);
        }
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, String aMessageTemplate, TARGET theValue) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theValue);
        }
        
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, String aMessageTemplate, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theAction);
        }
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, String aMessageTemplate, Supplier theSupplier) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theSupplier);
        }
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, String aMessageTemplate, TARGET theValue) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theValue);
        }
        
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, Predicate messageTemplateCheck, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> messageTemplateCheck.test(toBoolean.messageTemplate), theAction);
        }
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, Predicate messageTemplateCheck, Supplier theSupplier) {
            return toBoolean(toBoolean -> messageTemplateCheck.test(toBoolean.messageTemplate), theSupplier);
        }
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, Predicate messageTemplateCheck, TARGET theValue) {
            return toBoolean(toBoolean -> messageTemplateCheck.test(toBoolean.messageTemplate), theValue);
        }
        
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Predicate messageTemplateCheck, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theAction);
        }
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Predicate messageTemplateCheck, Supplier theSupplier) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theSupplier);
        }
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Predicate messageTemplateCheck, TARGET theValue) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theValue);
        }
        
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Predicate messageTemplateCheck, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theAction);
        }
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Predicate messageTemplateCheck, Supplier theSupplier) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theSupplier);
        }
        public  ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Predicate messageTemplateCheck, TARGET theValue) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theValue);
        }
    }
    public static class ValidationFirstSwitchTyped {
        private Validation $value;
        private ValidationFirstSwitchTyped(Validation theValue) { this.$value = theValue; }
        
        public ValidationSwitchToMessageToException toBoolean(Function, TARGET> theAction) {
            Function, TARGET> $action = null;
            Function, TARGET> oldAction = (Function, TARGET>)$action;
            Function, TARGET> newAction =
                ($action != null)
                ? oldAction : 
                    ($value instanceof ToBoolean)
                    ? (Function, TARGET>)(d -> theAction.apply((ToBoolean)d))
                    : oldAction;
            
            return new ValidationSwitchToMessageToException($value, newAction);
        }
        public ValidationSwitchToMessageToException toBoolean(Supplier theSupplier) {
            return toBoolean(d->theSupplier.get());
        }
        public ValidationSwitchToMessageToException toBoolean(TARGET theValue) {
            return toBoolean(d->theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBoolean(Predicate> check, Function, TARGET> theAction) {
            Function, TARGET> $action = null;
            Function, TARGET> oldAction = (Function, TARGET>)$action;
            Function, TARGET> newAction =
                ($action != null)
                ? oldAction : 
                    (($value instanceof ToBoolean) && check.test((ToBoolean)$value))
                    ? (Function, TARGET>)(d -> theAction.apply((ToBoolean)d))
                    : oldAction;
            
            return new ValidationSwitchToBooleanToMessageToException($value, newAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBoolean(Predicate> check, Supplier theSupplier) {
            return toBoolean(check, d->theSupplier.get());
        }
        public ValidationSwitchToBooleanToMessageToException toBoolean(Predicate> check, TARGET theValue) {
            return toBoolean(check, d->theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Absent messageTemplate, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker), theAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Absent messageTemplate, Supplier theSupplier) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker), theSupplier);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Absent messageTemplate, TARGET theValue) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker), theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Absent messageTemplate, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker), theAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Absent messageTemplate, Supplier theSupplier) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker), theSupplier);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Absent messageTemplate, TARGET theValue) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker), theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, String aMessageTemplate, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, String aMessageTemplate, Supplier theSupplier) {
            return toBoolean(toBoolean -> $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theSupplier);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, String aMessageTemplate, TARGET theValue) {
            return toBoolean(toBoolean -> $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, String aMessageTemplate, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, String aMessageTemplate, Supplier theSupplier) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theSupplier);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, String aMessageTemplate, TARGET theValue) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, String aMessageTemplate, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, String aMessageTemplate, Supplier theSupplier) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theSupplier);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, String aMessageTemplate, TARGET theValue) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, Predicate messageTemplateCheck, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> messageTemplateCheck.test(toBoolean.messageTemplate), theAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, Predicate messageTemplateCheck, Supplier theSupplier) {
            return toBoolean(toBoolean -> messageTemplateCheck.test(toBoolean.messageTemplate), theSupplier);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, Predicate messageTemplateCheck, TARGET theValue) {
            return toBoolean(toBoolean -> messageTemplateCheck.test(toBoolean.messageTemplate), theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Predicate messageTemplateCheck, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Predicate messageTemplateCheck, Supplier theSupplier) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theSupplier);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Predicate messageTemplateCheck, TARGET theValue) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Predicate messageTemplateCheck, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Predicate messageTemplateCheck, Supplier theSupplier) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theSupplier);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Predicate messageTemplateCheck, TARGET theValue) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theValue);
        }
    }
    public static class ValidationSwitchToBooleanToMessageToException extends ChoiceTypeSwitch, TARGET> {
        private ValidationSwitchToBooleanToMessageToException(Validation theValue, Function, TARGET> theAction) { super(theValue, theAction); }
        
        public ValidationSwitchToMessageToException toBoolean(Function, TARGET> theAction) {
            Function, TARGET> oldAction = (Function, TARGET>)$action;
            Function, TARGET> newAction =
                ($action != null)
                ? oldAction : 
                    ($value instanceof ToBoolean)
                    ? (Function, TARGET>)(d -> theAction.apply((ToBoolean)d))
                    : oldAction;
            
            return new ValidationSwitchToMessageToException($value, newAction);
        }
        public ValidationSwitchToMessageToException toBoolean(Supplier theSupplier) {
            return toBoolean(d->theSupplier.get());
        }
        public ValidationSwitchToMessageToException toBoolean(TARGET theValue) {
            return toBoolean(d->theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBoolean(Predicate> check, Function, TARGET> theAction) {
            Function, TARGET> oldAction = (Function, TARGET>)$action;
            Function, TARGET> newAction =
                ($action != null)
                ? oldAction : 
                    (($value instanceof ToBoolean) && check.test((ToBoolean)$value))
                    ? (Function, TARGET>)(d -> theAction.apply((ToBoolean)d))
                    : oldAction;
            
            return new ValidationSwitchToBooleanToMessageToException($value, newAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBoolean(Predicate> check, Supplier theSupplier) {
            return toBoolean(check, d->theSupplier.get());
        }
        public ValidationSwitchToBooleanToMessageToException toBoolean(Predicate> check, TARGET theValue) {
            return toBoolean(check, d->theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Absent messageTemplate, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker), theAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Absent messageTemplate, Supplier theSupplier) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker), theSupplier);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Absent messageTemplate, TARGET theValue) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker), theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Absent messageTemplate, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker), theAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Absent messageTemplate, Supplier theSupplier) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker), theSupplier);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Absent messageTemplate, TARGET theValue) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker), theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, String aMessageTemplate, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, String aMessageTemplate, Supplier theSupplier) {
            return toBoolean(toBoolean -> $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theSupplier);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, String aMessageTemplate, TARGET theValue) {
            return toBoolean(toBoolean -> $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, String aMessageTemplate, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, String aMessageTemplate, Supplier theSupplier) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theSupplier);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, String aMessageTemplate, TARGET theValue) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, String aMessageTemplate, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, String aMessageTemplate, Supplier theSupplier) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theSupplier);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, String aMessageTemplate, TARGET theValue) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && $utils.checkEquals(aMessageTemplate, toBoolean.messageTemplate), theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, Predicate messageTemplateCheck, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> messageTemplateCheck.test(toBoolean.messageTemplate), theAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, Predicate messageTemplateCheck, Supplier theSupplier) {
            return toBoolean(toBoolean -> messageTemplateCheck.test(toBoolean.messageTemplate), theSupplier);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Absent checker, Predicate messageTemplateCheck, TARGET theValue) {
            return toBoolean(toBoolean -> messageTemplateCheck.test(toBoolean.messageTemplate), theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Predicate messageTemplateCheck, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Predicate messageTemplateCheck, Supplier theSupplier) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theSupplier);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Function aChecker, Predicate messageTemplateCheck, TARGET theValue) {
            return toBoolean(toBoolean -> $utils.checkEquals(aChecker, toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theValue);
        }
        
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Predicate messageTemplateCheck, Function, TARGET> theAction) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theAction);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Predicate messageTemplateCheck, Supplier theSupplier) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theSupplier);
        }
        public ValidationSwitchToBooleanToMessageToException toBooleanOf(Predicate checkerCheck, Predicate messageTemplateCheck, TARGET theValue) {
            return toBoolean(toBoolean -> checkerCheck.test(toBoolean.checker) && messageTemplateCheck.test(toBoolean.messageTemplate), theValue);
        }
    }
    public static class ValidationSwitchToMessageToException extends ChoiceTypeSwitch, TARGET> {
        private ValidationSwitchToMessageToException(Validation theValue, Function, TARGET> theAction) { super(theValue, theAction); }
        
        public ValidationSwitchToException toMessage(Function, TARGET> theAction) {
            Function, TARGET> oldAction = (Function, TARGET>)$action;
            Function, TARGET> newAction =
                ($action != null)
                ? oldAction : 
                    ($value instanceof ToMessage)
                    ? (Function, TARGET>)(d -> theAction.apply((ToMessage)d))
                    : oldAction;
            
            return new ValidationSwitchToException($value, newAction);
        }
        public ValidationSwitchToException toMessage(Supplier theSupplier) {
            return toMessage(d->theSupplier.get());
        }
        public ValidationSwitchToException toMessage(TARGET theValue) {
            return toMessage(d->theValue);
        }
        
        public ValidationSwitchToMessageToException toMessage(Predicate> check, Function, TARGET> theAction) {
            Function, TARGET> oldAction = (Function, TARGET>)$action;
            Function, TARGET> newAction =
                ($action != null)
                ? oldAction : 
                    (($value instanceof ToMessage) && check.test((ToMessage)$value))
                    ? (Function, TARGET>)(d -> theAction.apply((ToMessage)d))
                    : oldAction;
            
            return new ValidationSwitchToMessageToException($value, newAction);
        }
        public ValidationSwitchToMessageToException toMessage(Predicate> check, Supplier theSupplier) {
            return toMessage(check, d->theSupplier.get());
        }
        public ValidationSwitchToMessageToException toMessage(Predicate> check, TARGET theValue) {
            return toMessage(check, d->theValue);
        }
        
        public ValidationSwitchToMessageToException toMessageOf(Function aErrorMsg, Function, TARGET> theAction) {
            return toMessage(toMessage -> $utils.checkEquals(aErrorMsg, toMessage.errorMsg), theAction);
        }
        public ValidationSwitchToMessageToException toMessageOf(Function aErrorMsg, Supplier theSupplier) {
            return toMessage(toMessage -> $utils.checkEquals(aErrorMsg, toMessage.errorMsg), theSupplier);
        }
        public ValidationSwitchToMessageToException toMessageOf(Function aErrorMsg, TARGET theValue) {
            return toMessage(toMessage -> $utils.checkEquals(aErrorMsg, toMessage.errorMsg), theValue);
        }
        
        public ValidationSwitchToMessageToException toMessageOf(Predicate errorMsgCheck, Function, TARGET> theAction) {
            return toMessage(toMessage -> errorMsgCheck.test(toMessage.errorMsg), theAction);
        }
        public ValidationSwitchToMessageToException toMessageOf(Predicate errorMsgCheck, Supplier theSupplier) {
            return toMessage(toMessage -> errorMsgCheck.test(toMessage.errorMsg), theSupplier);
        }
        public ValidationSwitchToMessageToException toMessageOf(Predicate errorMsgCheck, TARGET theValue) {
            return toMessage(toMessage -> errorMsgCheck.test(toMessage.errorMsg), theValue);
        }
    }
    public static class ValidationSwitchToException extends ChoiceTypeSwitch, TARGET> {
        private ValidationSwitchToException(Validation theValue, Function, TARGET> theAction) { super(theValue, theAction); }
        
        public TARGET toException(Function, TARGET> theAction) {
            Function, TARGET> oldAction = (Function, TARGET>)$action;
            Function, TARGET> newAction =
                ($action != null)
                ? oldAction : 
                    ($value instanceof ToException)
                    ? (Function, TARGET>)(d -> theAction.apply((ToException)d))
                    : oldAction;
            
            return newAction.apply($value);
        }
        public TARGET toException(Supplier theSupplier) {
            return toException(d->theSupplier.get());
        }
        public TARGET toException(TARGET theValue) {
            return toException(d->theValue);
        }
        
        public ValidationSwitchToException toException(Predicate> check, Function, TARGET> theAction) {
            Function, TARGET> oldAction = (Function, TARGET>)$action;
            Function, TARGET> newAction =
                ($action != null)
                ? oldAction : 
                    (($value instanceof ToException) && check.test((ToException)$value))
                    ? (Function, TARGET>)(d -> theAction.apply((ToException)d))
                    : oldAction;
            
            return new ValidationSwitchToException($value, newAction);
        }
        public ValidationSwitchToException toException(Predicate> check, Supplier theSupplier) {
            return toException(check, d->theSupplier.get());
        }
        public ValidationSwitchToException toException(Predicate> check, TARGET theValue) {
            return toException(check, d->theValue);
        }
        
        public ValidationSwitchToException toExceptionOf(Function aErrorChecker, Function, TARGET> theAction) {
            return toException(toException -> $utils.checkEquals(aErrorChecker, toException.errorChecker), theAction);
        }
        public ValidationSwitchToException toExceptionOf(Function aErrorChecker, Supplier theSupplier) {
            return toException(toException -> $utils.checkEquals(aErrorChecker, toException.errorChecker), theSupplier);
        }
        public ValidationSwitchToException toExceptionOf(Function aErrorChecker, TARGET theValue) {
            return toException(toException -> $utils.checkEquals(aErrorChecker, toException.errorChecker), theValue);
        }
        
        public ValidationSwitchToException toExceptionOf(Predicate errorCheckerCheck, Function, TARGET> theAction) {
            return toException(toException -> errorCheckerCheck.test(toException.errorChecker), theAction);
        }
        public ValidationSwitchToException toExceptionOf(Predicate errorCheckerCheck, Supplier theSupplier) {
            return toException(toException -> errorCheckerCheck.test(toException.errorChecker), theSupplier);
        }
        public ValidationSwitchToException toExceptionOf(Predicate errorCheckerCheck, TARGET theValue) {
            return toException(toException -> errorCheckerCheck.test(toException.errorChecker), theValue);
        }
    }
    
    
    //== Duplicate ==
    
    
    public static class $utils {
        public static  D notNull(D value) {
            return Objects.requireNonNull(value);
        }
        
        public static boolean checkEquals(byte a, byte b) {
            return a == b;
        }
        public static boolean checkEquals(short a, short b) {
            return a == b;
        }
        public static boolean checkEquals(int a, int b) {
            return a == b;
        }
        public static boolean checkEquals(long a, long b) {
            return a == b;
        }
        public static boolean checkEquals(float a, float b) {
            return a == b;
        }
        public static boolean checkEquals(double a, double b) {
            return a == b;
        }
        public static boolean checkEquals(boolean a, boolean b) {
            return a == b;
        }
        public static boolean checkEquals(Object a, Object b) {
            return ((a == null) && (b == null)) || Objects.equals(a, b);
        }
    }
    
    public static abstract class ChoiceTypeSwitch {
        protected final D                                $value;
        protected final Function $action;
        protected ChoiceTypeSwitch(D theValue, Function theAction) {
            this.$value  = theValue;
            this.$action = theAction;
        }
        
        public T orElse(T elseValue) {
            return ($action != null)
                    ? $action.apply($value)
                    : elseValue;
        }
        
        public T orGet(Supplier valueSupplier) {
            return ($action != null)
                    ? $action.apply($value)
                    : valueSupplier.get();
        }
        public T orGet(Function valueMapper) {
            val newAction = (Function)(($action != null) ? $action : valueMapper);
            return newAction.apply($value);
        }
        public T orElseGet(Supplier valueSupplier) {
            return orGet(valueSupplier);
        }
        public T orElseGet(Function valueMapper) {
            return orGet(valueMapper);
        }
        
        public static class ChoiceTypeSwitchData {
            protected final D              value;
            protected final Function action;
            
            public ChoiceTypeSwitchData(D value) {
                this(value, null);
            }
            public ChoiceTypeSwitchData(D value, Function action) {
                this.value  = value;
                this.action = action;
            }
            public D                         value()                           { return value; }
            public Function            action()                          { return action; }
            public ChoiceTypeSwitchData withValue(D value)                { return new ChoiceTypeSwitchData<>(value, action); }
            public ChoiceTypeSwitchData withAction(Function action) { return new ChoiceTypeSwitchData<>(value, action); }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy