functionalj.result.Validation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionalj-core Show documentation
Show all versions of functionalj-core Show documentation
The module for FunctionalJ Core.
// ============================================================================
// Copyright (c) 2017-2021 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.BooleanAccessPrimitive;
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({"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 BooleanAccessPrimitive isToBoolean = Validation::isToBoolean;
public final BooleanAccessPrimitive isToMessage = Validation::isToMessage;
public final BooleanAccessPrimitive 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 super ToBoolean, 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 super ToBoolean, 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 super ToBoolean, 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 super ToBoolean, 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