xyz.funjava.functional.validation.Validation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of validation Show documentation
Show all versions of validation Show documentation
This is a typesafe validation library with an aim on composition, reuse and safety. Tools are provided to create, compose or enrich validator while maintaining type safety.
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.funjava.functional.validation;
import xyz.funjava.functional.CheckedFunction10;
import xyz.funjava.functional.CheckedFunction11;
import xyz.funjava.functional.CheckedFunction12;
import xyz.funjava.functional.CheckedFunction13;
import xyz.funjava.functional.CheckedFunction14;
import xyz.funjava.functional.CheckedFunction15;
import xyz.funjava.functional.CheckedFunction16;
import xyz.funjava.functional.CheckedFunction17;
import xyz.funjava.functional.CheckedFunction18;
import xyz.funjava.functional.CheckedFunction19;
import xyz.funjava.functional.CheckedFunction2;
import xyz.funjava.functional.CheckedFunction20;
import xyz.funjava.functional.CheckedFunction21;
import xyz.funjava.functional.CheckedFunction22;
import xyz.funjava.functional.CheckedFunction23;
import xyz.funjava.functional.CheckedFunction24;
import xyz.funjava.functional.CheckedFunction25;
import xyz.funjava.functional.CheckedFunction26;
import xyz.funjava.functional.CheckedFunction3;
import xyz.funjava.functional.CheckedFunction4;
import xyz.funjava.functional.CheckedFunction5;
import xyz.funjava.functional.CheckedFunction6;
import xyz.funjava.functional.CheckedFunction7;
import xyz.funjava.functional.CheckedFunction8;
import xyz.funjava.functional.CheckedFunction9;
import xyz.funjava.functional.Function10;
import xyz.funjava.functional.Function11;
import xyz.funjava.functional.Function12;
import xyz.funjava.functional.Function13;
import xyz.funjava.functional.Function14;
import xyz.funjava.functional.Function15;
import xyz.funjava.functional.Function16;
import xyz.funjava.functional.Function17;
import xyz.funjava.functional.Function18;
import xyz.funjava.functional.Function19;
import xyz.funjava.functional.Function2;
import xyz.funjava.functional.Function20;
import xyz.funjava.functional.Function21;
import xyz.funjava.functional.Function22;
import xyz.funjava.functional.Function23;
import xyz.funjava.functional.Function24;
import xyz.funjava.functional.Function25;
import xyz.funjava.functional.Function26;
import xyz.funjava.functional.Function3;
import xyz.funjava.functional.Function4;
import xyz.funjava.functional.Function5;
import xyz.funjava.functional.Function6;
import xyz.funjava.functional.Function7;
import xyz.funjava.functional.Function8;
import xyz.funjava.functional.Function9;
import xyz.funjava.functional.higherkinded.applicative.Applicative;
import xyz.funjava.functional.higherkinded.monoid.Monoid;
import java.util.function.Function;
/**
* @author Carlos Sierra Andrés
*/
public interface Validation>
extends Applicative {
public boolean isSuccess();
public T get();
public F failures();
public Validation map(Function fun);
public > Validation mapFailures(
Function fun);
public Validation flatMap(Function> fun);
@Override
public Validation applyTo(
Applicative> fun);
default Validation pure(S s) {
return new Success<>(s);
}
class Success> implements Validation {
private final T _t;
public Success(T t) {
_t = t;
}
public Validation map(Function fun) {
return new Success<>(fun.apply(_t));
}
@Override
public > Validation mapFailures(
Function fun) {
return (Validation)this;
}
@Override
public Validation applyTo(
Applicative> fun) {
return ((Validation, F>)fun).map(f -> f.apply(_t));
}
@Override
public String toString() {
return "Success {" + _t + '}';
}
public Validation flatMap(Function> fun) {
return fun.apply(_t);
}
@Override
public boolean isSuccess() {
return true;
}
@Override
public T get() {
return _t;
}
@Override
public F failures() {
throw new IllegalStateException();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Success, ?> success = (Success, ?>) o;
return _t.equals(success._t);
}
@Override
public int hashCode() {
return _t.hashCode();
}
}
class Failure> implements Validation {
private F _failure;
public Failure(F failure) {
_failure = failure;
}
@Override
public Validation map(
Function fun) {
return (Validation)this;
}
@Override
public > Validation mapFailures(
Function fun) {
return new Failure<>(fun.apply(_failure));
}
@Override
public Validation applyTo(
Applicative> ap) {
if (ap instanceof Failure) {
return new Failure<>((F)_failure.mappend(((Failure) ap)._failure));
}
else {
return (Validation)this;
}
}
@Override
public String toString() {
return "Failure {" + "Reasons: " + _failure.toString() + '}';
}
@Override
public Validation flatMap(
Function> fun) {
return (Validation)this;
}
@Override
public boolean isSuccess() {
return false;
}
@Override
public T get() {
throw new IllegalStateException();
}
@Override
public F failures() {
return _failure;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Failure, ?> failure = (Failure, ?>) o;
return _failure.equals(failure._failure);
}
@Override
public int hashCode() {
return _failure.hashCode();
}
}
static > Validation just(T t) {
return new Success<>(t);
}
public static > Function2> wrap(CheckedFunction2 fun, Function error) {
return (a, b) -> {
try {
return new Success<>(fun.apply(a, b));
}
catch (Exception ex) {
return new Failure<>(error.apply(ex));
}
};
}
public static Function2 ignore(CheckedFunction2 fun) {
return (a, b) -> {
try {
return fun.apply(a, b);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}
public static > Validation apply(Function2 fun, Validation a, Validation b) {
return (Validation)Applicative.apply(fun, a, b);
}
public static > Validation apply(Function3 fun, Validation a, Validation b, Validation c) {
return (Validation)Applicative.apply(fun, a, b, c);
}
public static > Validation apply(Function4 fun, Validation a, Validation b, Validation c, Validation d) {
return (Validation)Applicative.apply(fun, a, b, c, d);
}
public static > Validation apply(Function5 fun, Validation a, Validation b, Validation c, Validation d, Validation e) {
return (Validation)Applicative.apply(fun, a, b, c, d, e);
}
public static > Validation apply(Function6 fun, Validation a, Validation b, Validation c, Validation d, Validation e, Validation f) {
return (Validation)Applicative.apply(fun, a, b, c, d, e, f);
}
public static > Validation apply(Function7 fun, Validation a, Validation b, Validation c, Validation d, Validation e, Validation f, Validation g) {
return (Validation)Applicative.apply(fun, a, b, c, d, e, f, g);
}
public static > Validation apply(Function8 fun, Validation a, Validation b, Validation c, Validation d, Validation e, Validation f, Validation g, Validation h) {
return (Validation)Applicative.apply(fun, a, b, c, d, e, f, g, h);
}
public static > Validation apply(Function9 fun, Validation a, Validation b, Validation c, Validation d, Validation e, Validation f, Validation g, Validation h, Validation i) {
return (Validation)Applicative.apply(fun, a, b, c, d, e, f, g, h, i);
}
public static > Validation apply(Function10 fun, Validation a, Validation b, Validation c, Validation d, Validation e, Validation f, Validation g, Validation h, Validation i, Validation j) {
return (Validation)Applicative.apply(fun, a, b, c, d, e, f, g, h, i, j);
}
public static > Validation apply(Function11 fun, Validation a, Validation b, Validation c, Validation d, Validation e, Validation f, Validation g, Validation h, Validation i, Validation j, Validation k) {
return (Validation)Applicative.apply(fun, a, b, c, d, e, f, g, h, i, j, k);
}
public static > Validation apply(Function12 fun, Validation a, Validation b, Validation c, Validation d, Validation e, Validation f, Validation g, Validation h, Validation i, Validation j, Validation k, Validation l) {
return (Validation)Applicative.apply(fun, a, b, c, d, e, f, g, h, i, j, k, l);
}
public static > Validation apply(Function13 fun, Validation a, Validation b, Validation c, Validation d, Validation e, Validation f, Validation g, Validation h, Validation i, Validation j, Validation k, Validation l, Validation m) {
return (Validation)Applicative.apply(fun, a, b, c, d, e, f, g, h, i, j, k, l, m);
}
public static > Validation apply(Function14 fun, Validation a, Validation b, Validation c, Validation d, Validation e, Validation f, Validation g, Validation h, Validation i, Validation j, Validation k, Validation l, Validation m, Validation n) {
return (Validation)Applicative.apply(fun, a, b, c, d, e, f, g, h, i, j, k, l, m, n);
}
public static > Validation apply(Function15 fun, Validation a, Validation b, Validation c, Validation d, Validation e, Validation f, Validation g, Validation h, Validation i, Validation j, Validation k, Validation l, Validation m, Validation n, Validation o) {
return (Validation)Applicative.apply(fun, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
}
public static > Validation apply(Function16 fun, Validation a, Validation b, Validation c, Validation d, Validation e, Validation f, Validation g, Validation h, Validation i, Validation j, Validation k, Validation l, Validation m, Validation n, Validation o, Validation p) {
return (Validation)Applicative.apply(fun, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p);
}
public static > Validation apply(Function17 fun, Validation a, Validation b, Validation c, Validation d, Validation e, Validation f, Validation g, Validation h, Validation i, Validation j, Validation k, Validation l, Validation m, Validation n, Validation o, Validation p, Validation q) {
return (Validation