diff --git a/scripts/generate-applicative.sh b/scripts/generate-applicative.sh new file mode 100755 index 00000000..adcda9ca --- /dev/null +++ b/scripts/generate-applicative.sh @@ -0,0 +1,124 @@ +#!/bin/bash +set -e +n=16 + +for i in `seq 1 ${n}`;do + class="Function${i}" + file="$(dirname $0)/../src/main/java/am/ik/yavi/fn/${class}.java" + echo $file + cat < ${file} +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface ${class}<$(echo $(for j in `seq 1 ${i}`;do echo -n "T${j}, ";done) | sed 's/,$//'), R> { + + R apply($(echo $(for j in `seq 1 ${i}`;do echo -n "T${j} t${j}, ";done) | sed 's/,$//')) throws Exception; + + default $(echo $(for j in `seq 1 ${i}`;do echo -n "Function1";done)) curried() { + return $(echo $(for j in `seq 1 ${i}`;do echo -n "t${j} -> ";done)) this.apply($(echo $(for j in `seq 1 ${i}`;do echo -n "t${j}, ";done) | sed 's/,$//')); + } + +} +EOF +done + +for i in `seq 1 ${n}`;do + class="Composing${i}" + file="$(dirname $0)/../src/main/java/am/ik/yavi/fn/${class}.java" + echo $file + cat < ${file} +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class ${class} { +$(for j in `seq 1 ${i}`;do echo " protected final Validation v${j};";echo;done) + + public ${class}($(echo $(for j in `seq 1 ${i}`;do echo -n "Validation v${j}, ";done) | sed 's/,$//')) { +$(for j in `seq 1 ${i}`;do echo " this.v${j} = v${j};";done) + } + + public Validation, R> apply(Function${i}<$(echo $(for j in `seq 1 ${i}`;do echo -n "T${j}, ";done) | sed 's/,$//'), R> f) { + return $(echo $(for j in `seq ${i} 1`;do echo -n "v${j}.apply(";done) | sed 's/,$//')Validation.success(f.curried())$(echo $(for j in `seq 1 ${i}`;do echo -n ")";done)); + } +$(if [ ${i} -lt ${n} ];then echo;echo " public Composing$((${i} + 1)) compose(Validation v$((${i} + 1))) {"; echo " return new Composing$((${i} + 1))<>($(echo $(for j in `seq 1 $((${i} + 1))`;do echo -n "v${j}, ";done) | sed 's/,$//'));"; echo " }"; else echo -n "";fi) +} +EOF +done + +class="Validations" +file="$(dirname $0)/../src/main/java/am/ik/yavi/fn/${class}.java" +echo $file +cat < ${file} +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class ${class} { +$(for i in `seq 1 ${n}`;do echo " public static Composing${i} compose($(echo $(for j in `seq 1 ${i}`;do echo -n "Validation v${j}, ";done) | sed 's/,$//')) {"; echo " return new Composing${i}<>($(echo $(for j in `seq 1 ${i}`;do echo -n "v${j}, ";done) | sed 's/,$//'));"; echo " }";echo;done) + +$(for i in `seq 1 ${n}`;do echo " public static Validation, R> apply($(echo $(for j in `seq 1 ${i}`;do echo -n "Validation v${j}, ";done))Function${i}<$(echo $(for j in `seq 1 ${i}`;do echo -n "T${j}, ";done) | sed 's/,$//'), R> f) {"; echo " return compose($(echo $(for j in `seq 1 ${i}`;do echo -n "v${j}, ";done) | sed 's/,$//')).apply(f);"; echo " }";echo;done) +} +EOF \ No newline at end of file diff --git a/src/main/java/am/ik/yavi/builder/ValidatorBuilder.java b/src/main/java/am/ik/yavi/builder/ValidatorBuilder.java index a6ce0ffe..b53f1d77 100644 --- a/src/main/java/am/ik/yavi/builder/ValidatorBuilder.java +++ b/src/main/java/am/ik/yavi/builder/ValidatorBuilder.java @@ -764,7 +764,7 @@ private Consumer, ValidatorSubset>> appendNes final ConstraintCondition condition = new NestedConstraintCondition<>( nested, conditionalValidator.first()); final ValidatorSubset v = new NestedValidatorSubset<>(nested, - conditionalValidator.second(), name + this.messageKeySeparator); + conditionalValidator.second(), name); this.conditionalValidators.add(new Pair<>(condition, v)); }; } diff --git a/src/main/java/am/ik/yavi/core/ApplicativeValidator.java b/src/main/java/am/ik/yavi/core/ApplicativeValidator.java new file mode 100644 index 00000000..3a052ed4 --- /dev/null +++ b/src/main/java/am/ik/yavi/core/ApplicativeValidator.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.core; + +import java.util.Locale; + +import am.ik.yavi.fn.Validation; + +/** + * Applicative validator class + * @param Target class + * @since 0.6.0 + */ +public final class ApplicativeValidator { + private final Validator validator; + + ApplicativeValidator(Validator validator) { + this.validator = validator; + } + + public Validation validate(T target) { + return this.validate(target, Locale.getDefault(), ConstraintGroup.DEFAULT); + } + + public Validation validate(T target, Locale locale, + ConstraintGroup constraintGroup) { + final ConstraintViolations violations = this.validator.validate(target, locale, + constraintGroup); + if (violations.isValid()) { + return Validation.success(target); + } + else { + return Validation.failure(violations); + } + } +} diff --git a/src/main/java/am/ik/yavi/core/ConstraintViolations.java b/src/main/java/am/ik/yavi/core/ConstraintViolations.java index 54e6b0c6..f5f9c1ad 100644 --- a/src/main/java/am/ik/yavi/core/ConstraintViolations.java +++ b/src/main/java/am/ik/yavi/core/ConstraintViolations.java @@ -26,7 +26,38 @@ import java.util.stream.Collectors; public class ConstraintViolations implements List { - private final List delegate = new ArrayList<>(); + private final List delegate; + + /** + * Constructs an empty list with an initial capacity of ten. + */ + public ConstraintViolations() { + this.delegate = new ArrayList<>(); + } + + /** + * Constructs an empty list with the specified initial capacity. + * + * @param initialCapacity the initial capacity of the list + * @throws IllegalArgumentException if the specified initial capacity is negative + * @since 0.6.0 + */ + public ConstraintViolations(int initialCapacity) { + this.delegate = new ArrayList<>(initialCapacity); + } + + /** + * Concatenate all violations + * @param violations + * @return concatenated violations + * @since 0.6.0 + */ + public static ConstraintViolations concat(List violations) { + final ConstraintViolations constraintViolations = new ConstraintViolations( + violations.stream().mapToInt(ConstraintViolations::size).sum()); + violations.forEach(constraintViolations::addAll); + return constraintViolations; + } /** * Appends the specified element to the end of this list (optional operation). diff --git a/src/main/java/am/ik/yavi/core/Validator.java b/src/main/java/am/ik/yavi/core/Validator.java index 3cb0696a..19c8da13 100644 --- a/src/main/java/am/ik/yavi/core/Validator.java +++ b/src/main/java/am/ik/yavi/core/Validator.java @@ -28,9 +28,9 @@ /** * Validates the target instances. - * + * * A Validator instance is immutable and can be used as a singleton. - * + * * @param the type of the instance to validate * @author Toshiaki Maki */ @@ -73,12 +73,12 @@ private Validator(String messageKeySeparator, Validator prefixed(String prefix) { return new Validator<>(this.messageKeySeparator, this.predicatesList, this.collectionValidators, this.conditionalValidators, - this.messageFormatter, prefix); + this.messageFormatter, prefix + this.messageKeySeparator); } /** * This method is supposed to be used only internally. - * + * * @param action callback per ConstraintPredicates. */ public void forEachPredicates(Consumer> action) { @@ -121,7 +121,7 @@ public ConstraintViolations validate(T target, ConstraintGroup constraintGroup) /** * Validates all constraints on {@code target}.
* {@code ConstraintGroup.DEFAULT} is used as a constraint group. - * + * * @param target target to validate * @param locale the locale targeted for the violation messages. * @return constraint violations @@ -158,6 +158,15 @@ public ConstraintViolations validate(T target) { return this.validate(target, Locale.getDefault(), ConstraintGroup.DEFAULT); } + /** + * Convert to the applicative validator + * @return applicative validator + * @since 0.6.0 + */ + public ApplicativeValidator applicative() { + return new ApplicativeValidator<>(this); + } + /** * Validates all constraints on {@code target} and returns {@code Either} object that * has constraint violations on the left or validated object on the right.
diff --git a/src/main/java/am/ik/yavi/fn/Composing1.java b/src/main/java/am/ik/yavi/fn/Composing1.java new file mode 100644 index 00000000..8e80ccd8 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Composing1.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Composing1 { + protected final Validation v1; + + public Composing1(Validation v1) { + this.v1 = v1; + } + + public Validation, R> apply(Function1 f) { + return v1.apply(Validation.success(f.curried())); + } + + public Composing2 compose(Validation v2) { + return new Composing2<>(v1, v2); + } +} diff --git a/src/main/java/am/ik/yavi/fn/Composing10.java b/src/main/java/am/ik/yavi/fn/Composing10.java new file mode 100644 index 00000000..6142dfdb --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Composing10.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Composing10 { + protected final Validation v1; + + protected final Validation v2; + + protected final Validation v3; + + protected final Validation v4; + + protected final Validation v5; + + protected final Validation v6; + + protected final Validation v7; + + protected final Validation v8; + + protected final Validation v9; + + protected final Validation v10; + + public Composing10(Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10) { + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + this.v4 = v4; + this.v5 = v5; + this.v6 = v6; + this.v7 = v7; + this.v8 = v8; + this.v9 = v9; + this.v10 = v10; + } + + public Validation, R> apply( + Function10 f) { + return v10.apply(v9.apply(v8.apply(v7.apply(v6.apply(v5.apply(v4.apply( + v3.apply(v2.apply(v1.apply(Validation.success(f.curried()))))))))))); + } + + public Composing11 compose( + Validation v11) { + return new Composing11<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11); + } +} diff --git a/src/main/java/am/ik/yavi/fn/Composing11.java b/src/main/java/am/ik/yavi/fn/Composing11.java new file mode 100644 index 00000000..d18f62c3 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Composing11.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Composing11 { + protected final Validation v1; + + protected final Validation v2; + + protected final Validation v3; + + protected final Validation v4; + + protected final Validation v5; + + protected final Validation v6; + + protected final Validation v7; + + protected final Validation v8; + + protected final Validation v9; + + protected final Validation v10; + + protected final Validation v11; + + public Composing11(Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11) { + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + this.v4 = v4; + this.v5 = v5; + this.v6 = v6; + this.v7 = v7; + this.v8 = v8; + this.v9 = v9; + this.v10 = v10; + this.v11 = v11; + } + + public Validation, R> apply( + Function11 f) { + return v11.apply(v10.apply(v9.apply(v8.apply(v7.apply(v6.apply(v5.apply(v4.apply( + v3.apply(v2.apply(v1.apply(Validation.success(f.curried())))))))))))); + } + + public Composing12 compose( + Validation v12) { + return new Composing12<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12); + } +} diff --git a/src/main/java/am/ik/yavi/fn/Composing12.java b/src/main/java/am/ik/yavi/fn/Composing12.java new file mode 100644 index 00000000..1f4d9a8f --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Composing12.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Composing12 { + protected final Validation v1; + + protected final Validation v2; + + protected final Validation v3; + + protected final Validation v4; + + protected final Validation v5; + + protected final Validation v6; + + protected final Validation v7; + + protected final Validation v8; + + protected final Validation v9; + + protected final Validation v10; + + protected final Validation v11; + + protected final Validation v12; + + public Composing12(Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11, Validation v12) { + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + this.v4 = v4; + this.v5 = v5; + this.v6 = v6; + this.v7 = v7; + this.v8 = v8; + this.v9 = v9; + this.v10 = v10; + this.v11 = v11; + this.v12 = v12; + } + + public Validation, R> apply( + Function12 f) { + return v12.apply(v11.apply( + v10.apply(v9.apply(v8.apply(v7.apply(v6.apply(v5.apply(v4.apply(v3.apply( + v2.apply(v1.apply(Validation.success(f.curried()))))))))))))); + } + + public Composing13 compose( + Validation v13) { + return new Composing13<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13); + } +} diff --git a/src/main/java/am/ik/yavi/fn/Composing13.java b/src/main/java/am/ik/yavi/fn/Composing13.java new file mode 100644 index 00000000..8cc564f2 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Composing13.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Composing13 { + protected final Validation v1; + + protected final Validation v2; + + protected final Validation v3; + + protected final Validation v4; + + protected final Validation v5; + + protected final Validation v6; + + protected final Validation v7; + + protected final Validation v8; + + protected final Validation v9; + + protected final Validation v10; + + protected final Validation v11; + + protected final Validation v12; + + protected final Validation v13; + + public Composing13(Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11, Validation v12, + Validation v13) { + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + this.v4 = v4; + this.v5 = v5; + this.v6 = v6; + this.v7 = v7; + this.v8 = v8; + this.v9 = v9; + this.v10 = v10; + this.v11 = v11; + this.v12 = v12; + this.v13 = v13; + } + + public Validation, R> apply( + Function13 f) { + return v13.apply(v12.apply(v11.apply( + v10.apply(v9.apply(v8.apply(v7.apply(v6.apply(v5.apply(v4.apply(v3.apply( + v2.apply(v1.apply(Validation.success(f.curried())))))))))))))); + } + + public Composing14 compose( + Validation v14) { + return new Composing14<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, + v14); + } +} diff --git a/src/main/java/am/ik/yavi/fn/Composing14.java b/src/main/java/am/ik/yavi/fn/Composing14.java new file mode 100644 index 00000000..f2c2ae67 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Composing14.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Composing14 { + protected final Validation v1; + + protected final Validation v2; + + protected final Validation v3; + + protected final Validation v4; + + protected final Validation v5; + + protected final Validation v6; + + protected final Validation v7; + + protected final Validation v8; + + protected final Validation v9; + + protected final Validation v10; + + protected final Validation v11; + + protected final Validation v12; + + protected final Validation v13; + + protected final Validation v14; + + public Composing14(Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11, Validation v12, + Validation v13, Validation v14) { + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + this.v4 = v4; + this.v5 = v5; + this.v6 = v6; + this.v7 = v7; + this.v8 = v8; + this.v9 = v9; + this.v10 = v10; + this.v11 = v11; + this.v12 = v12; + this.v13 = v13; + this.v14 = v14; + } + + public Validation, R> apply( + Function14 f) { + return v14.apply(v13.apply(v12.apply(v11.apply( + v10.apply(v9.apply(v8.apply(v7.apply(v6.apply(v5.apply(v4.apply(v3.apply( + v2.apply(v1.apply(Validation.success(f.curried()))))))))))))))); + } + + public Composing15 compose( + Validation v15) { + return new Composing15<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, + v14, v15); + } +} diff --git a/src/main/java/am/ik/yavi/fn/Composing15.java b/src/main/java/am/ik/yavi/fn/Composing15.java new file mode 100644 index 00000000..ae599409 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Composing15.java @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Composing15 { + protected final Validation v1; + + protected final Validation v2; + + protected final Validation v3; + + protected final Validation v4; + + protected final Validation v5; + + protected final Validation v6; + + protected final Validation v7; + + protected final Validation v8; + + protected final Validation v9; + + protected final Validation v10; + + protected final Validation v11; + + protected final Validation v12; + + protected final Validation v13; + + protected final Validation v14; + + protected final Validation v15; + + public Composing15(Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11, Validation v12, + Validation v13, Validation v14, Validation v15) { + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + this.v4 = v4; + this.v5 = v5; + this.v6 = v6; + this.v7 = v7; + this.v8 = v8; + this.v9 = v9; + this.v10 = v10; + this.v11 = v11; + this.v12 = v12; + this.v13 = v13; + this.v14 = v14; + this.v15 = v15; + } + + public Validation, R> apply( + Function15 f) { + return v15.apply(v14.apply(v13.apply(v12.apply(v11.apply( + v10.apply(v9.apply(v8.apply(v7.apply(v6.apply(v5.apply(v4.apply(v3.apply( + v2.apply(v1.apply(Validation.success(f.curried())))))))))))))))); + } + + public Composing16 compose( + Validation v16) { + return new Composing16<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, + v14, v15, v16); + } +} diff --git a/src/main/java/am/ik/yavi/fn/Composing16.java b/src/main/java/am/ik/yavi/fn/Composing16.java new file mode 100644 index 00000000..bec9a167 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Composing16.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Composing16 { + protected final Validation v1; + + protected final Validation v2; + + protected final Validation v3; + + protected final Validation v4; + + protected final Validation v5; + + protected final Validation v6; + + protected final Validation v7; + + protected final Validation v8; + + protected final Validation v9; + + protected final Validation v10; + + protected final Validation v11; + + protected final Validation v12; + + protected final Validation v13; + + protected final Validation v14; + + protected final Validation v15; + + protected final Validation v16; + + public Composing16(Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11, Validation v12, + Validation v13, Validation v14, Validation v15, + Validation v16) { + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + this.v4 = v4; + this.v5 = v5; + this.v6 = v6; + this.v7 = v7; + this.v8 = v8; + this.v9 = v9; + this.v10 = v10; + this.v11 = v11; + this.v12 = v12; + this.v13 = v13; + this.v14 = v14; + this.v15 = v15; + this.v16 = v16; + } + + public Validation, R> apply( + Function16 f) { + return v16.apply(v15.apply(v14.apply(v13.apply(v12.apply(v11.apply( + v10.apply(v9.apply(v8.apply(v7.apply(v6.apply(v5.apply(v4.apply(v3.apply( + v2.apply(v1.apply(Validation.success(f.curried()))))))))))))))))); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Composing2.java b/src/main/java/am/ik/yavi/fn/Composing2.java new file mode 100644 index 00000000..b2d96695 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Composing2.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Composing2 { + protected final Validation v1; + + protected final Validation v2; + + public Composing2(Validation v1, Validation v2) { + this.v1 = v1; + this.v2 = v2; + } + + public Validation, R> apply(Function2 f) { + return v2.apply(v1.apply(Validation.success(f.curried()))); + } + + public Composing3 compose(Validation v3) { + return new Composing3<>(v1, v2, v3); + } +} diff --git a/src/main/java/am/ik/yavi/fn/Composing3.java b/src/main/java/am/ik/yavi/fn/Composing3.java new file mode 100644 index 00000000..a470ba3e --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Composing3.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Composing3 { + protected final Validation v1; + + protected final Validation v2; + + protected final Validation v3; + + public Composing3(Validation v1, Validation v2, Validation v3) { + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + } + + public Validation, R> apply(Function3 f) { + return v3.apply(v2.apply(v1.apply(Validation.success(f.curried())))); + } + + public Composing4 compose(Validation v4) { + return new Composing4<>(v1, v2, v3, v4); + } +} diff --git a/src/main/java/am/ik/yavi/fn/Composing4.java b/src/main/java/am/ik/yavi/fn/Composing4.java new file mode 100644 index 00000000..f84d8967 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Composing4.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Composing4 { + protected final Validation v1; + + protected final Validation v2; + + protected final Validation v3; + + protected final Validation v4; + + public Composing4(Validation v1, Validation v2, Validation v3, + Validation v4) { + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + this.v4 = v4; + } + + public Validation, R> apply(Function4 f) { + return v4.apply(v3.apply(v2.apply(v1.apply(Validation.success(f.curried()))))); + } + + public Composing5 compose(Validation v5) { + return new Composing5<>(v1, v2, v3, v4, v5); + } +} diff --git a/src/main/java/am/ik/yavi/fn/Composing5.java b/src/main/java/am/ik/yavi/fn/Composing5.java new file mode 100644 index 00000000..bd426ee1 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Composing5.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Composing5 { + protected final Validation v1; + + protected final Validation v2; + + protected final Validation v3; + + protected final Validation v4; + + protected final Validation v5; + + public Composing5(Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5) { + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + this.v4 = v4; + this.v5 = v5; + } + + public Validation, R> apply(Function5 f) { + return v5.apply( + v4.apply(v3.apply(v2.apply(v1.apply(Validation.success(f.curried())))))); + } + + public Composing6 compose(Validation v6) { + return new Composing6<>(v1, v2, v3, v4, v5, v6); + } +} diff --git a/src/main/java/am/ik/yavi/fn/Composing6.java b/src/main/java/am/ik/yavi/fn/Composing6.java new file mode 100644 index 00000000..bd80f862 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Composing6.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Composing6 { + protected final Validation v1; + + protected final Validation v2; + + protected final Validation v3; + + protected final Validation v4; + + protected final Validation v5; + + protected final Validation v6; + + public Composing6(Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6) { + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + this.v4 = v4; + this.v5 = v5; + this.v6 = v6; + } + + public Validation, R> apply(Function6 f) { + return v6.apply(v5.apply( + v4.apply(v3.apply(v2.apply(v1.apply(Validation.success(f.curried()))))))); + } + + public Composing7 compose(Validation v7) { + return new Composing7<>(v1, v2, v3, v4, v5, v6, v7); + } +} diff --git a/src/main/java/am/ik/yavi/fn/Composing7.java b/src/main/java/am/ik/yavi/fn/Composing7.java new file mode 100644 index 00000000..a47d5f3b --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Composing7.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Composing7 { + protected final Validation v1; + + protected final Validation v2; + + protected final Validation v3; + + protected final Validation v4; + + protected final Validation v5; + + protected final Validation v6; + + protected final Validation v7; + + public Composing7(Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7) { + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + this.v4 = v4; + this.v5 = v5; + this.v6 = v6; + this.v7 = v7; + } + + public Validation, R> apply(Function7 f) { + return v7.apply(v6.apply(v5.apply(v4 + .apply(v3.apply(v2.apply(v1.apply(Validation.success(f.curried())))))))); + } + + public Composing8 compose( + Validation v8) { + return new Composing8<>(v1, v2, v3, v4, v5, v6, v7, v8); + } +} diff --git a/src/main/java/am/ik/yavi/fn/Composing8.java b/src/main/java/am/ik/yavi/fn/Composing8.java new file mode 100644 index 00000000..084d87ca --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Composing8.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Composing8 { + protected final Validation v1; + + protected final Validation v2; + + protected final Validation v3; + + protected final Validation v4; + + protected final Validation v5; + + protected final Validation v6; + + protected final Validation v7; + + protected final Validation v8; + + public Composing8(Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8) { + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + this.v4 = v4; + this.v5 = v5; + this.v6 = v6; + this.v7 = v7; + this.v8 = v8; + } + + public Validation, R> apply( + Function8 f) { + return v8.apply(v7.apply(v6.apply(v5.apply(v4 + .apply(v3.apply(v2.apply(v1.apply(Validation.success(f.curried()))))))))); + } + + public Composing9 compose( + Validation v9) { + return new Composing9<>(v1, v2, v3, v4, v5, v6, v7, v8, v9); + } +} diff --git a/src/main/java/am/ik/yavi/fn/Composing9.java b/src/main/java/am/ik/yavi/fn/Composing9.java new file mode 100644 index 00000000..69e40f9b --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Composing9.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Composing9 { + protected final Validation v1; + + protected final Validation v2; + + protected final Validation v3; + + protected final Validation v4; + + protected final Validation v5; + + protected final Validation v6; + + protected final Validation v7; + + protected final Validation v8; + + protected final Validation v9; + + public Composing9(Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9) { + this.v1 = v1; + this.v2 = v2; + this.v3 = v3; + this.v4 = v4; + this.v5 = v5; + this.v6 = v6; + this.v7 = v7; + this.v8 = v8; + this.v9 = v9; + } + + public Validation, R> apply( + Function9 f) { + return v9.apply(v8.apply(v7.apply(v6.apply(v5.apply(v4.apply( + v3.apply(v2.apply(v1.apply(Validation.success(f.curried())))))))))); + } + + public Composing10 compose( + Validation v10) { + return new Composing10<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10); + } +} diff --git a/src/main/java/am/ik/yavi/fn/Function1.java b/src/main/java/am/ik/yavi/fn/Function1.java new file mode 100644 index 00000000..201e1706 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Function1.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface Function1 { + + R apply(T1 t1) throws Exception; + + default Function1 curried() { + return t1 -> this.apply(t1); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Function10.java b/src/main/java/am/ik/yavi/fn/Function10.java new file mode 100644 index 00000000..e7b059c8 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Function10.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface Function10 { + + R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10) + throws Exception; + + default Function1>>>>>>>>> curried() { + return t1 -> t2 -> t3 -> t4 -> t5 -> t6 -> t7 -> t8 -> t9 -> t10 -> this.apply(t1, + t2, t3, t4, t5, t6, t7, t8, t9, t10); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Function11.java b/src/main/java/am/ik/yavi/fn/Function11.java new file mode 100644 index 00000000..91ccb401 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Function11.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface Function11 { + + R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, + T11 t11) throws Exception; + + default Function1>>>>>>>>>> curried() { + return t1 -> t2 -> t3 -> t4 -> t5 -> t6 -> t7 -> t8 -> t9 -> t10 -> t11 -> this + .apply(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Function12.java b/src/main/java/am/ik/yavi/fn/Function12.java new file mode 100644 index 00000000..50896be8 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Function12.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface Function12 { + + R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, + T11 t11, T12 t12) throws Exception; + + default Function1>>>>>>>>>>> curried() { + return t1 -> t2 -> t3 -> t4 -> t5 -> t6 -> t7 -> t8 -> t9 -> t10 -> t11 -> t12 -> this + .apply(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Function13.java b/src/main/java/am/ik/yavi/fn/Function13.java new file mode 100644 index 00000000..bda69d28 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Function13.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface Function13 { + + R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, + T11 t11, T12 t12, T13 t13) throws Exception; + + default Function1>>>>>>>>>>>> curried() { + return t1 -> t2 -> t3 -> t4 -> t5 -> t6 -> t7 -> t8 -> t9 -> t10 -> t11 -> t12 -> t13 -> this + .apply(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Function14.java b/src/main/java/am/ik/yavi/fn/Function14.java new file mode 100644 index 00000000..1a200e96 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Function14.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface Function14 { + + R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, + T11 t11, T12 t12, T13 t13, T14 t14) throws Exception; + + default Function1>>>>>>>>>>>>> curried() { + return t1 -> t2 -> t3 -> t4 -> t5 -> t6 -> t7 -> t8 -> t9 -> t10 -> t11 -> t12 -> t13 -> t14 -> this + .apply(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Function15.java b/src/main/java/am/ik/yavi/fn/Function15.java new file mode 100644 index 00000000..b77046f3 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Function15.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface Function15 { + + R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, + T11 t11, T12 t12, T13 t13, T14 t14, T15 t15) throws Exception; + + default Function1>>>>>>>>>>>>>> curried() { + return t1 -> t2 -> t3 -> t4 -> t5 -> t6 -> t7 -> t8 -> t9 -> t10 -> t11 -> t12 -> t13 -> t14 -> t15 -> this + .apply(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Function16.java b/src/main/java/am/ik/yavi/fn/Function16.java new file mode 100644 index 00000000..1b8ee0aa --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Function16.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface Function16 { + + R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, + T11 t11, T12 t12, T13 t13, T14 t14, T15 t15, T16 t16) throws Exception; + + default Function1>>>>>>>>>>>>>>> curried() { + return t1 -> t2 -> t3 -> t4 -> t5 -> t6 -> t7 -> t8 -> t9 -> t10 -> t11 -> t12 -> t13 -> t14 -> t15 -> t16 -> this + .apply(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, + t16); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Function2.java b/src/main/java/am/ik/yavi/fn/Function2.java new file mode 100644 index 00000000..48ac79f4 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Function2.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface Function2 { + + R apply(T1 t1, T2 t2) throws Exception; + + default Function1> curried() { + return t1 -> t2 -> this.apply(t1, t2); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Function3.java b/src/main/java/am/ik/yavi/fn/Function3.java new file mode 100644 index 00000000..885235fe --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Function3.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface Function3 { + + R apply(T1 t1, T2 t2, T3 t3) throws Exception; + + default Function1>> curried() { + return t1 -> t2 -> t3 -> this.apply(t1, t2, t3); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Function4.java b/src/main/java/am/ik/yavi/fn/Function4.java new file mode 100644 index 00000000..2466ea03 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Function4.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface Function4 { + + R apply(T1 t1, T2 t2, T3 t3, T4 t4) throws Exception; + + default Function1>>> curried() { + return t1 -> t2 -> t3 -> t4 -> this.apply(t1, t2, t3, t4); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Function5.java b/src/main/java/am/ik/yavi/fn/Function5.java new file mode 100644 index 00000000..497cfac1 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Function5.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface Function5 { + + R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) throws Exception; + + default Function1>>>> curried() { + return t1 -> t2 -> t3 -> t4 -> t5 -> this.apply(t1, t2, t3, t4, t5); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Function6.java b/src/main/java/am/ik/yavi/fn/Function6.java new file mode 100644 index 00000000..ec8a6f0a --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Function6.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface Function6 { + + R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6) throws Exception; + + default Function1>>>>> curried() { + return t1 -> t2 -> t3 -> t4 -> t5 -> t6 -> this.apply(t1, t2, t3, t4, t5, t6); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Function7.java b/src/main/java/am/ik/yavi/fn/Function7.java new file mode 100644 index 00000000..132ccd23 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Function7.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface Function7 { + + R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7) throws Exception; + + default Function1>>>>>> curried() { + return t1 -> t2 -> t3 -> t4 -> t5 -> t6 -> t7 -> this.apply(t1, t2, t3, t4, t5, + t6, t7); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Function8.java b/src/main/java/am/ik/yavi/fn/Function8.java new file mode 100644 index 00000000..7177fe78 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Function8.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface Function8 { + + R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8) throws Exception; + + default Function1>>>>>>> curried() { + return t1 -> t2 -> t3 -> t4 -> t5 -> t6 -> t7 -> t8 -> this.apply(t1, t2, t3, t4, + t5, t6, t7, t8); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Function9.java b/src/main/java/am/ik/yavi/fn/Function9.java new file mode 100644 index 00000000..1ece0094 --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Function9.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public interface Function9 { + + R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9) + throws Exception; + + default Function1>>>>>>>> curried() { + return t1 -> t2 -> t3 -> t4 -> t5 -> t6 -> t7 -> t8 -> t9 -> this.apply(t1, t2, + t3, t4, t5, t6, t7, t8, t9); + } + +} diff --git a/src/main/java/am/ik/yavi/fn/Validation.java b/src/main/java/am/ik/yavi/fn/Validation.java new file mode 100644 index 00000000..e8f0037d --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Validation.java @@ -0,0 +1,222 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Objects; +import java.util.function.Consumer; +import java.util.function.Function; + +/** + * An implementation similar to Vavr's + * Validation control.
+ * The Validation control is an applicative functor and facilitates accumulating errors. + * + * @param value type in the case of invalid + * @param value type in the case of valid + * @since 0.6.0 + */ +public interface Validation extends Serializable { + boolean isValid(); + + /** + * Returns the value of this {@code Validation} if is a {@code Success} or throws if + * this is an {@code Failure}. + * + * @return The value of this {@code Validation} + * @throws NoSuchElementException if this is an {@code Failure} + */ + T value(); + + /** + * Returns the error of this {@code Validation} if it is an {@code Failure} or throws + * if this is a {@code Success}. + * + * @return The error, if present + * @throws NoSuchElementException if this is a {@code Success} + */ + E error(); + + @SuppressWarnings("unchecked") + default Validation map(Function mapper) { + return isValid() ? Validation.success(mapper.apply(value())) + : (Validation) this; + } + + @SuppressWarnings("unchecked") + default Validation flatMap(Function> mapper) { + return isValid() ? mapper.apply(value()) : (Validation) this; + } + + default Validation peek(Consumer consumer) { + if (isValid()) { + consumer.accept(value()); + } + return this; + } + + @SuppressWarnings("unchecked") + default Validation mapError(Function errorMapper) { + return isValid() ? (Validation) this + : Validation.failure(errorMapper.apply(error())); + } + + default Validation peekError(Consumer consumer) { + if (!isValid()) { + consumer.accept(error()); + } + return this; + } + + default U fold(Function errorMapper, Function mapper) { + return isValid() ? mapper.apply(value()) : errorMapper.apply(error()); + } + + default Validation, U> apply( + Validation, Function1> validation) { + if (isValid()) { + if (validation.isValid()) { + final Function1 f = validation.value(); + try { + final U u = f.apply(this.value()); + return Validation.success(u); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + else { + final List error = validation.error(); + return Validation.failure(error); + } + } + else { + final E error = this.error(); + if (validation.isValid()) { + return Validation.failure(Collections.singletonList(error)); + } + else { + final List errors = new ArrayList<>(validation.error()); + errors.add(error); + return Validation.failure(errors); + } + } + } + + default Either toEither() { + return isValid() ? Either.right(value()) : Either.left(error()); + } + + default Composing2 compose(Validation validation) { + return new Composing2<>(this, validation); + } + + static Validation fromEither(Either either) { + return either.fold(Validation::failure, Validation::success); + } + + static Validation success(T value) { + return new Success<>(value); + } + + static Validation failure(E error) { + return new Failure<>(error); + } + + class Success implements Validation { + private static final long serialVersionUID = 1L; + + public final T value; + + Success(T value) { + this.value = value; + } + + @Override + public boolean isValid() { + return true; + } + + @Override + public T value() { + return this.value; + } + + @Override + public E error() throws RuntimeException { + throw new NoSuchElementException("error of 'Success' Validation"); + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + Success success = (Success) o; + return value.equals(success.value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } + } + + class Failure implements Validation { + private static final long serialVersionUID = 1L; + + private final E error; + + Failure(E error) { + this.error = error; + } + + @Override + public boolean isValid() { + return false; + } + + @Override + public T value() { + throw new NoSuchElementException("get of 'Failure' Validation"); + } + + @Override + public E error() { + return this.error; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + Failure failure = (Failure) o; + return error.equals(failure.error); + } + + @Override + public int hashCode() { + return Objects.hash(error); + } + } +} diff --git a/src/main/java/am/ik/yavi/fn/Validations.java b/src/main/java/am/ik/yavi/fn/Validations.java new file mode 100644 index 00000000..260c6aea --- /dev/null +++ b/src/main/java/am/ik/yavi/fn/Validations.java @@ -0,0 +1,272 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +/** + * Generated by + * https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh + * + * @since 0.6.0 + */ +public class Validations { + public static Composing1 compose(Validation v1) { + return new Composing1<>(v1); + } + + public static Composing2 compose(Validation v1, + Validation v2) { + return new Composing2<>(v1, v2); + } + + public static Composing3 compose(Validation v1, + Validation v2, Validation v3) { + return new Composing3<>(v1, v2, v3); + } + + public static Composing4 compose( + Validation v1, Validation v2, Validation v3, + Validation v4) { + return new Composing4<>(v1, v2, v3, v4); + } + + public static Composing5 compose( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5) { + return new Composing5<>(v1, v2, v3, v4, v5); + } + + public static Composing6 compose( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6) { + return new Composing6<>(v1, v2, v3, v4, v5, v6); + } + + public static Composing7 compose( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7) { + return new Composing7<>(v1, v2, v3, v4, v5, v6, v7); + } + + public static Composing8 compose( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8) { + return new Composing8<>(v1, v2, v3, v4, v5, v6, v7, v8); + } + + public static Composing9 compose( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9) { + return new Composing9<>(v1, v2, v3, v4, v5, v6, v7, v8, v9); + } + + public static Composing10 compose( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10) { + return new Composing10<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10); + } + + public static Composing11 compose( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11) { + return new Composing11<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11); + } + + public static Composing12 compose( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11, Validation v12) { + return new Composing12<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12); + } + + public static Composing13 compose( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11, Validation v12, + Validation v13) { + return new Composing13<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13); + } + + public static Composing14 compose( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11, Validation v12, + Validation v13, Validation v14) { + return new Composing14<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, + v14); + } + + public static Composing15 compose( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11, Validation v12, + Validation v13, Validation v14, Validation v15) { + return new Composing15<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, + v14, v15); + } + + public static Composing16 compose( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11, Validation v12, + Validation v13, Validation v14, Validation v15, + Validation v16) { + return new Composing16<>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, + v14, v15, v16); + } + + public static Validation, R> apply(Validation v1, + Function1 f) { + return compose(v1).apply(f); + } + + public static Validation, R> apply(Validation v1, + Validation v2, Function2 f) { + return compose(v1, v2).apply(f); + } + + public static Validation, R> apply(Validation v1, + Validation v2, Validation v3, Function3 f) { + return compose(v1, v2, v3).apply(f); + } + + public static Validation, R> apply( + Validation v1, Validation v2, Validation v3, + Validation v4, Function4 f) { + return compose(v1, v2, v3, v4).apply(f); + } + + public static Validation, R> apply( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, + Function5 f) { + return compose(v1, v2, v3, v4, v5).apply(f); + } + + public static Validation, R> apply( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Function6 f) { + return compose(v1, v2, v3, v4, v5, v6).apply(f); + } + + public static Validation, R> apply( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Function7 f) { + return compose(v1, v2, v3, v4, v5, v6, v7).apply(f); + } + + public static Validation, R> apply( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, + Function8 f) { + return compose(v1, v2, v3, v4, v5, v6, v7, v8).apply(f); + } + + public static Validation, R> apply( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Function9 f) { + return compose(v1, v2, v3, v4, v5, v6, v7, v8, v9).apply(f); + } + + public static Validation, R> apply( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, + Function10 f) { + return compose(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10).apply(f); + } + + public static Validation, R> apply( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11, + Function11 f) { + return compose(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11).apply(f); + } + + public static Validation, R> apply( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11, Validation v12, + Function12 f) { + return compose(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12).apply(f); + } + + public static Validation, R> apply( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11, Validation v12, + Validation v13, + Function13 f) { + return compose(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13).apply(f); + } + + public static Validation, R> apply( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11, Validation v12, + Validation v13, Validation v14, + Function14 f) { + return compose(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) + .apply(f); + } + + public static Validation, R> apply( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11, Validation v12, + Validation v13, Validation v14, Validation v15, + Function15 f) { + return compose(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) + .apply(f); + } + + public static Validation, R> apply( + Validation v1, Validation v2, Validation v3, + Validation v4, Validation v5, Validation v6, + Validation v7, Validation v8, Validation v9, + Validation v10, Validation v11, Validation v12, + Validation v13, Validation v14, Validation v15, + Validation v16, + Function16 f) { + return compose(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, + v16).apply(f); + } +} diff --git a/src/test/java/am/ik/yavi/core/ApplicativeValidationTest.java b/src/test/java/am/ik/yavi/core/ApplicativeValidationTest.java new file mode 100644 index 00000000..a5157105 --- /dev/null +++ b/src/test/java/am/ik/yavi/core/ApplicativeValidationTest.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.core; + +import am.ik.yavi.Address; +import am.ik.yavi.Country; +import am.ik.yavi.PhoneNumber; +import am.ik.yavi.builder.ValidatorBuilder; +import am.ik.yavi.fn.Validation; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class ApplicativeValidationTest { + final ApplicativeValidator countryValidator = Country.validator() + .prefixed("country").applicative(); + + final ApplicativeValidator streetValidator = ValidatorBuilder.of(String.class) + ._string(s -> s, "street", c -> c.notBlank()).build().applicative(); + + final ApplicativeValidator phoneNumberValidator = PhoneNumber.validator() + .prefixed("phoneNumber").applicative(); + + @Test + void validated_valid() { + final Validation validation = countryValidator + .validate(new Country("jp")).compose(streetValidator.validate("xyz")) + .compose(phoneNumberValidator.validate(new PhoneNumber("12345678"))) + .apply(Address::new).mapError(ConstraintViolations::concat); + assertThat(validation.isValid()).isTrue(); + final Address address = validation.value(); + assertThat(address).isNotNull(); + assertThat(address.country().name()).isEqualTo("jp"); + assertThat(address.street()).isEqualTo("xyz"); + assertThat(address.phoneNumber().value()).isEqualTo("12345678"); + } + + @Test + void validated_invalid() { + final Validation validation = countryValidator + .validate(new Country("j")).compose(streetValidator.validate("")) + .compose(phoneNumberValidator.validate(new PhoneNumber("1234567"))) + .apply(Address::new).mapError(ConstraintViolations::concat); + assertThat(validation.isValid()).isFalse(); + final ConstraintViolations violations = validation.error(); + assertThat(violations).hasSize(3); + assertThat(violations.get(0).name()).isEqualTo("country.name"); + assertThat(violations.get(0).messageKey()) + .isEqualTo("container.greaterThanOrEqual"); + assertThat(violations.get(1).name()).isEqualTo("street"); + assertThat(violations.get(1).messageKey()).isEqualTo("charSequence.notBlank"); + assertThat(violations.get(2).name()).isEqualTo("phoneNumber.value"); + assertThat(violations.get(2).messageKey()) + .isEqualTo("container.greaterThanOrEqual"); + } +} \ No newline at end of file diff --git a/src/test/java/am/ik/yavi/core/ConstraintViolationsTest.java b/src/test/java/am/ik/yavi/core/ConstraintViolationsTest.java index 3274c0be..56dfb8b0 100644 --- a/src/test/java/am/ik/yavi/core/ConstraintViolationsTest.java +++ b/src/test/java/am/ik/yavi/core/ConstraintViolationsTest.java @@ -18,14 +18,35 @@ import java.util.Arrays; import java.util.Locale; -import org.junit.Test; +import am.ik.yavi.message.SimpleMessageFormatter; +import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; -import am.ik.yavi.message.SimpleMessageFormatter; - public class ConstraintViolationsTest { + @Test + public void concat() { + SimpleMessageFormatter messageFormatter = new SimpleMessageFormatter(); + ConstraintViolations violations1 = new ConstraintViolations(); + violations1.add(new ConstraintViolation("foo0", "abc0", "hello0", + new Object[] { 1 }, messageFormatter, Locale.getDefault())); + violations1.add(new ConstraintViolation("foo1", "abc1", "hello1", + new Object[] { 1 }, messageFormatter, Locale.getDefault())); + + ConstraintViolations violations2 = new ConstraintViolations(); + violations2.add(new ConstraintViolation("bar0", "abc0", "hello0", + new Object[] { 1 }, messageFormatter, Locale.getDefault())); + violations2.add(new ConstraintViolation("bar1", "abc1", "hello1", + new Object[] { 1 }, messageFormatter, Locale.getDefault())); + + final ConstraintViolations violations = ConstraintViolations + .concat(Arrays.asList(violations1, violations2)); + assertThat(violations).hasSize(4); + assertThat(violations).containsExactly(violations1.get(0), violations.get(1), + violations2.get(0), violations2.get(1)); + } + @Test public void apply() { SimpleMessageFormatter messageFormatter = new SimpleMessageFormatter(); diff --git a/src/test/java/am/ik/yavi/fn/ValidationTest.java b/src/test/java/am/ik/yavi/fn/ValidationTest.java new file mode 100644 index 00000000..26c17426 --- /dev/null +++ b/src/test/java/am/ik/yavi/fn/ValidationTest.java @@ -0,0 +1,237 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.Arrays; +import java.util.List; +import java.util.NoSuchElementException; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class ValidationTest { + + @Test + void success() { + final Validation, String> validation = Validation.success("test"); + assertThat(validation.isValid()).isTrue(); + assertThat(validation.value()).isEqualTo("test"); + assertThatThrownBy(validation::error).isInstanceOf(NoSuchElementException.class); + assertThat(validation.map(String::toUpperCase).value()).isEqualTo("TEST"); + assertThat(validation.mapError(List::size).value()).isEqualTo("test"); + assertThat(validation.flatMap(s -> Validation.success("hello " + s)).value()) + .isEqualTo("hello test"); + assertThat(validation).isEqualTo(Validation.success("test")); + assertThat(validation.hashCode()) + .isEqualTo(Validation.success("test").hashCode()); + } + + @Test + void failure() { + final Validation, String> validation = Validation + .failure(Arrays.asList("error1", "error2")); + assertThat(validation.isValid()).isFalse(); + assertThatThrownBy(validation::value).isInstanceOf(NoSuchElementException.class); + assertThat(validation.error()).isEqualTo(Arrays.asList("error1", "error2")); + assertThat(validation.map(String::toUpperCase).error()) + .isEqualTo(Arrays.asList("error1", "error2")); + assertThat(validation.mapError(x -> x.size()).error()).isEqualTo(2); + assertThat(validation.flatMap(s -> Validation.success("hello " + s)).error()) + .isEqualTo(Arrays.asList("error1", "error2")); + assertThat(validation) + .isEqualTo(Validation.failure(Arrays.asList("error1", "error2"))); + assertThat(validation.hashCode()).isEqualTo( + Validation.failure(Arrays.asList("error1", "error2")).hashCode()); + } + + @Test + void foldSuccess() { + final Validation, String> validation = Validation.success("test"); + final String fold = validation.fold(e -> String.join(",", e), + String::toUpperCase); + assertThat(fold).isEqualTo("TEST"); + } + + @Test + void foldFailure() { + final Validation, String> validation = Validation + .failure(Arrays.asList("error1", "error2")); + final String fold = validation.fold(e -> String.join(",", e), + String::toUpperCase); + assertThat(fold).isEqualTo("error1,error2"); + } + + @Test + void successToEither() { + final Validation, String> validation = Validation.success("test"); + final Either, String> either = validation.toEither(); + assertThat(either.isRight()).isTrue(); + assertThat(either.right().get()).isEqualTo("test"); + } + + @Test + void failureToEither() { + final Validation, String> validation = Validation + .failure(Arrays.asList("error1", "error2")); + final Either, String> either = validation.toEither(); + assertThat(either.isLeft()).isTrue(); + assertThat(either.left().get()).isEqualTo(Arrays.asList("error1", "error2")); + } + + @Test + void successFromEither() { + final Either, String> either = Either.right("test"); + final Validation, String> validation = Validation.fromEither(either); + assertThat(validation.isValid()).isTrue(); + assertThat(validation.value()).isEqualTo("test"); + } + + @Test + void failureFromEither() { + final Either, String> either = Either + .left(Arrays.asList("error1", "error2")); + final Validation, String> validation = Validation.fromEither(either); + assertThat(validation.isValid()).isFalse(); + assertThat(validation.error()).isEqualTo(Arrays.asList("error1", "error2")); + } + + @Test + void compose_all_valid() { + final Validation v1 = Validation.success("s1"); + final Validation v2 = Validation.success("s2"); + final Validation v3 = Validation.success("s3"); + final Validation v4 = Validation.success("s4"); + final Validation v5 = Validation.success("s5"); + final Validation v6 = Validation.success("s6"); + final Validation v7 = Validation.success("s7"); + final Validation v8 = Validation.success("s8"); + final Validation v9 = Validation.success("s9"); + final Validation v10 = Validation.success("s10"); + final Validation v11 = Validation.success("s11"); + final Validation v12 = Validation.success("s12"); + final Validation v13 = Validation.success("s13"); + final Validation v14 = Validation.success("s14"); + final Validation v15 = Validation.success("s15"); + final Validation v16 = Validation.success("s16"); + + final Validation, String> validation = v1.compose(v2).compose(v3) + .compose(v4).compose(v5).compose(v6).compose(v7).compose(v8).compose(v9) + .compose(v10).compose(v11).compose(v12).compose(v13).compose(v14) + .compose(v15).compose(v16) + .apply((s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, + s16) -> String.join(", ", s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, + s11, s12, s13, s14, s15, s16)); + assertThat(validation.isValid()).isTrue(); + assertThat(validation.value()).isEqualTo( + "s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16"); + } + + @Test + void compose_all_invalid() { + final Validation v1 = Validation.failure("f1"); + final Validation v2 = Validation.failure("f2"); + final Validation v3 = Validation.failure("f3"); + final Validation v4 = Validation.failure("f4"); + final Validation v5 = Validation.failure("f5"); + final Validation v6 = Validation.failure("f6"); + final Validation v7 = Validation.failure("f7"); + final Validation v8 = Validation.failure("f8"); + final Validation v9 = Validation.failure("f9"); + final Validation v10 = Validation.failure("f10"); + final Validation v11 = Validation.failure("f11"); + final Validation v12 = Validation.failure("f12"); + final Validation v13 = Validation.failure("f13"); + final Validation v14 = Validation.failure("f14"); + final Validation v15 = Validation.failure("f15"); + final Validation v16 = Validation.failure("f16"); + + final Validation, String> validation = v1.compose(v2).compose(v3) + .compose(v4).compose(v5).compose(v6).compose(v7).compose(v8).compose(v9) + .compose(v10).compose(v11).compose(v12).compose(v13).compose(v14) + .compose(v15).compose(v16) + .apply((s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, + s16) -> String.join(", ", s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, + s11, s12, s13, s14, s15, s16)); + + assertThat(validation.isValid()).isFalse(); + assertThat(validation.error()).containsExactly("f1", "f2", "f3", "f4", "f5", "f6", + "f7", "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15", "f16"); + } + + @Test + void compose_first_invalid() { + final Validation v1 = Validation.failure("f1"); + final Validation v2 = Validation.success("s2"); + final Validation v3 = Validation.success("s3"); + final Validation v4 = Validation.success("s4"); + final Validation v5 = Validation.success("s5"); + final Validation v6 = Validation.success("s6"); + final Validation v7 = Validation.success("s7"); + final Validation v8 = Validation.success("s8"); + final Validation v9 = Validation.success("s9"); + final Validation v10 = Validation.success("s10"); + final Validation v11 = Validation.success("s11"); + final Validation v12 = Validation.success("s12"); + final Validation v13 = Validation.success("s13"); + final Validation v14 = Validation.success("s14"); + final Validation v15 = Validation.success("s15"); + final Validation v16 = Validation.success("s16"); + + final Validation, String> validation = v1.compose(v2).compose(v3) + .compose(v4).compose(v5).compose(v6).compose(v7).compose(v8).compose(v9) + .compose(v10).compose(v11).compose(v12).compose(v13).compose(v14) + .compose(v15).compose(v16) + .apply((s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, + s16) -> String.join(", ", s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, + s11, s12, s13, s14, s15, s16)); + + assertThat(validation.isValid()).isFalse(); + assertThat(validation.error()).containsExactly("f1"); + } + + @Test + void compose_last_invalid() { + final Validation v1 = Validation.success("s1"); + final Validation v2 = Validation.success("s2"); + final Validation v3 = Validation.success("s3"); + final Validation v4 = Validation.success("s4"); + final Validation v5 = Validation.success("s5"); + final Validation v6 = Validation.success("s6"); + final Validation v7 = Validation.success("s7"); + final Validation v8 = Validation.success("s8"); + final Validation v9 = Validation.success("s9"); + final Validation v10 = Validation.success("s10"); + final Validation v11 = Validation.success("s11"); + final Validation v12 = Validation.success("s12"); + final Validation v13 = Validation.success("s13"); + final Validation v14 = Validation.success("s14"); + final Validation v15 = Validation.success("s15"); + final Validation v16 = Validation.failure("f16"); + + final Validation, String> validation = v1.compose(v2).compose(v3) + .compose(v4).compose(v5).compose(v6).compose(v7).compose(v8).compose(v9) + .compose(v10).compose(v11).compose(v12).compose(v13).compose(v14) + .compose(v15).compose(v16) + .apply((s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, + s16) -> String.join(", ", s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, + s11, s12, s13, s14, s15, s16)); + + assertThat(validation.isValid()).isFalse(); + assertThat(validation.error()).containsExactly("f16"); + } +} \ No newline at end of file diff --git a/src/test/java/am/ik/yavi/fn/ValidationsTest.java b/src/test/java/am/ik/yavi/fn/ValidationsTest.java new file mode 100644 index 00000000..6f6e0fb0 --- /dev/null +++ b/src/test/java/am/ik/yavi/fn/ValidationsTest.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2018-2021 Toshiaki Maki + * + * Licensed 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 am.ik.yavi.fn; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class ValidationsTest { + @Test + void compose() { + final Validation, String> v1 = Validation.success("s1"); + final Validation, String> v2 = Validation.success("s2"); + final Validation, String> v3 = Validation.success("s3"); + + final String s = Validations.compose(v1, v2, v3) + .apply((s1, s2, s3) -> String.join("-", s1, s2, s3)).value(); + assertThat(s).isEqualTo("s1-s2-s3"); + } + + @Test + void apply() { + final Validation, String> v1 = Validation.success("s1"); + final Validation, String> v2 = Validation.success("s2"); + final Validation, String> v3 = Validation.success("s3"); + + final String s = Validations + .apply(v1, v2, v3, (s1, s2, s3) -> String.join("-", s1, s2, s3)).value(); + assertThat(s).isEqualTo("s1-s2-s3"); + } +} \ No newline at end of file