├── README.md ├── bnd.bnd ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main └── java │ └── com │ └── liferay │ └── functional │ ├── Applicative.java │ ├── CheckedFunction1.java │ ├── CheckedFunction10.java │ ├── CheckedFunction11.java │ ├── CheckedFunction12.java │ ├── CheckedFunction13.java │ ├── CheckedFunction14.java │ ├── CheckedFunction15.java │ ├── CheckedFunction16.java │ ├── CheckedFunction17.java │ ├── CheckedFunction18.java │ ├── CheckedFunction19.java │ ├── CheckedFunction2.java │ ├── CheckedFunction20.java │ ├── CheckedFunction21.java │ ├── CheckedFunction22.java │ ├── CheckedFunction23.java │ ├── CheckedFunction24.java │ ├── CheckedFunction25.java │ ├── CheckedFunction26.java │ ├── CheckedFunction3.java │ ├── CheckedFunction4.java │ ├── CheckedFunction5.java │ ├── CheckedFunction6.java │ ├── CheckedFunction7.java │ ├── CheckedFunction8.java │ ├── CheckedFunction9.java │ ├── Function1.java │ ├── Function10.java │ ├── Function11.java │ ├── Function12.java │ ├── Function13.java │ ├── Function14.java │ ├── Function15.java │ ├── Function16.java │ ├── Function17.java │ ├── Function18.java │ ├── Function19.java │ ├── Function2.java │ ├── Function20.java │ ├── Function21.java │ ├── Function22.java │ ├── Function23.java │ ├── Function24.java │ ├── Function25.java │ ├── Function26.java │ ├── Function3.java │ ├── Function4.java │ ├── Function5.java │ ├── Function6.java │ ├── Function7.java │ ├── Function8.java │ ├── Function9.java │ ├── Monoid.java │ ├── fieldprovider │ ├── FieldFail.java │ ├── FieldProvider.java │ ├── MapStringProvider.java │ ├── SafeCast.java │ └── StringProvider.java │ └── validation │ ├── Composer.java │ ├── Fail.java │ ├── Validation.java │ └── Validator.java └── test └── java └── com └── liferay └── functional ├── fieldprovider └── FieldProviderTest.java └── validation ├── ValidationTest.java └── ValidatorTest.java /README.md: -------------------------------------------------------------------------------- 1 | # Typesafe Validation Library for Java 8 2 | 3 | This is a typesafe validation library with an aim on composition, reuse and safety. Tools are provided to create, compose or enrich validator while maintaining type safety. 4 | 5 | ## Getting started 6 | 7 | The project is still in _alpha_ state and it is not yet published as an artifact. First clone this repo and issue `./gradlew install` from the root of the project to install it in your local maven repository. 8 | 9 | To use it in your maven project add 10 | ``` 11 | 12 | com.liferay.functional 13 | java8-functional 14 | 0.0.1.SNAPSHOT 15 | 16 | ``` 17 | 18 | or in your gradle project: 19 | ``` 20 | compile 'com.liferay.functional:java8-functional:0.0.1.SNAPSHOT' 21 | ``` 22 | 23 | the project coordinates are very likely to change in the future. 24 | 25 | ## First validation 26 | 27 | The core of the library is the `Validation` interface. `T` parameter is the type contained in the validation while `F` parameter corresponds to the type of the failure. `F` is further constrained to implement `Monoid` since one of the features of the library is to collect as much validation errors as possible. 28 | 29 | `Validation` has some methods to check for the success of failure of the validation. Just like with `Optional` you can use `isSuccess()` to test for a success and then use either `get()` or `failures()` to collect the proper result. Using the incorrect method will lead to an `IllegalStateException`. You have been warned :-) 30 | 31 | Nevertheless, to get the most from `Validation`, it is best to use its functor, applicative and monadic nature to combine it in different ways. 32 | 33 | `Validator` interface is also provided to represent any `Function>`. `Validator` is also functor, applicative and monad. `Validator.predicate` is a helper function that helps in the creation of `Validator` from `Predicate`: 34 | ``` 35 | Validator notEmpty = predicate( 36 | (String s) -> s.length() > 0, s -> new Fail("must not be empty")); 37 | ``` 38 | 39 | and we can use this validator with any `String`: 40 | 41 | ``` 42 | Validation failure = notEmpty.validate(""); 43 | 44 | Validation success = notEmpty.validate("notEmpty"); 45 | ``` 46 | 47 | new `Validator`s can be created from the composition of other Validators: 48 | ``` 49 | Validator longerThan10 = predicate( 50 | (String s) -> s.length() > 10, s -> new Fail("must be longer than 10")); 51 | 52 | 53 | Validator composed = notBlank.compose(longerThan10); 54 | ``` 55 | 56 | we can create _safe casts_ by creating a validator that goes from one type to another: 57 | 58 | ``` 59 | Validator safeToInt = input -> { 60 | try { 61 | return Validation.just(Integer.parseInt(input)); 62 | } 63 | catch (NumberFormatException nfe) { 64 | return new Validation.Failure(new Fail("must be a number")); 65 | } 66 | } 67 | 68 | Validator positive = predicate( 69 | i -> i > 0, i -> new Fail("must be positive")); 70 | 71 | //Composer is a util to compose validator in a type safe way 72 | 73 | Composer.compose(notBlank, safeToInt, positive).validate("1"); 74 | ``` 75 | 76 | we can also compose validators using `and`: 77 | 78 | ``` 79 | Validator greaterThan(int min) { ... } 80 | Validator lowerThan(int max) { ... } 81 | 82 | Validator between(int min, int max) { 83 | return greaterThan(min).and(lowerThan(max)) 84 | } 85 | ``` 86 | 87 | ### Functional API 88 | 89 | You can use `map(Function fun)` to further modify the content of a successful computation. Just like with `Optional` a failure will just do nothing in this case: 90 | 91 | ``` 92 | Validation success; 93 | 94 | Validation plusOne = success.map(i -> i + 1); 95 | ``` 96 | 97 | It is possible to adapt an existing validator to work on a different model. Let's take our `between` and `notBlank` validators as example. If we want to use those validators to work on an existing model we can use `adapt`: 98 | 99 | ``` 100 | class User { 101 | String name; 102 | Integer age; 103 | 104 | public User(Integer age, String name) { 105 | this.age = age; 106 | this.name = name; 107 | } 108 | 109 | public Integer getAge() { 110 | return age; 111 | } 112 | 113 | public String getName() { 114 | return name; 115 | } 116 | } 117 | 118 | Validator safeUserAge = between(8, 99).adapt(User::getAge, f -> new FieldFail("age", f)); 119 | 120 | Validator safeUserName = notBlank.adapt(User::getName, f -> new FieldFail("name", f)); 121 | ``` 122 | 123 | so now we have two validators that operate on User. We can further adapt those so they return `User` instead of their respective types using `partials`. We can also use `FieldFail.fromFail` to save some characters: 124 | 125 | ``` 126 | Validator userValidator = Validator.partials( 127 | between(8, 99).adapt(User::getAge, fromFail("age")), 128 | notBlank.adapt(User::getName, fromFail("name")) 129 | ) 130 | ``` 131 | 132 | and now we can validate the user and collect either the result or the set of failures. 133 | 134 | ``` 135 | Validation validation = userValidator.validate(user); 136 | ``` 137 | 138 | If we don't have a user instance we can use the _Applicative Functor_ nature of `Validation` to create the user only when the fields pass validation. 139 | 140 | ``` 141 | Validation validation = Validation.apply( 142 | User::new, 143 | between(8, 99).validate(10), 144 | notBlank.validate("aName")); 145 | ); 146 | ``` 147 | 148 | ### Field Providers 149 | 150 | Since it is a very common situation to get input data from `Map` or `Map` like structures the library supports the concept of `FieldProvider`. This interface provides methods to work with validators more conveniently: 151 | 152 | ``` 153 | StringProvider sp; 154 | 155 | Adaptor adaptor = sp.getAdaptor(FieldFail::new); //Adapt the errors to contain field info 156 | 157 | Validation safeName = Adaptor.safeGet( 158 | adaptor, "name", 159 | compose(required(), notBlank)); 160 | 161 | Validation age = Adaptor.safeGet( 162 | adaptor, "age", 163 | compose(required(), isANumber(), between(8, 99))); 164 | 165 | ``` 166 | 167 | `FieldProvider` and `Adaptor` provide a way to traverse nested structures using `Adaptor.getAdaptor(String fieldName)`. You can also use `Adaptor.focus(String ... fields)` to point to a nested structure. In case of invalid structure the error will be returned in the `Validation` type. 168 | 169 | 170 | ### Forcing validation in business logic 171 | 172 | One use case for the validation library would be to force the validation of the parameters of a function invocation. We can force this by using specific types for the parameters. This types can have `package private` constructor visibility and may only be accessed by a `public static` function that returns a `Validation`. By using this idiom we can decouple the business logic from the validation logic and still be sure that the invocation of the function is going to be safe (unless mischevious uses of reflection and such): 173 | 174 | ``` 175 | 176 | public class Service { 177 | 178 | public Result doSomething(Name name, Age age) {} 179 | 180 | static class Name { 181 | String _name; 182 | 183 | Name(String name) { 184 | _name = name; 185 | } 186 | } 187 | 188 | static class Age { 189 | int _age; 190 | 191 | Age(int age) { 192 | _age = age; 193 | } 194 | } 195 | 196 | public static Validation name(String name) { 197 | //apply all validations here 198 | return notBlank. 199 | validate(name). 200 | mapFailures(FieldFail.fromFail("name")). 201 | map(Name::new); 202 | } 203 | 204 | public static Validation age(int age) { 205 | //apply all validations here 206 | return between(0, 99). 207 | validate(age). 208 | mapFailures(FieldFail.fromFail("age")). 209 | map(Age::new); 210 | } 211 | 212 | } 213 | 214 | ``` 215 | 216 | then we can invoke our service using: 217 | 218 | ``` 219 | 220 | Service service = new Service(); 221 | 222 | Validation result = Validation.apply( 223 | service::doSomething, Service.name("carlos"), Service.age(38)); 224 | 225 | ``` 226 | 227 | if we have unit tests in the package we can test the logic bypassing the validation step. 228 | -------------------------------------------------------------------------------- /bnd.bnd: -------------------------------------------------------------------------------- 1 | Bundle-SymbolicName: com.liferay.functional.validation 2 | Bundle-Version: 0.0.1.SNAPSHOT 3 | Export-Package: com.liferay.functional.* -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.liferay.functional' 2 | version '0.0.1.SNAPSHOT' 3 | 4 | apply plugin: 'biz.aQute.bnd.builder' 5 | apply plugin: 'java' 6 | apply plugin: 'maven' 7 | 8 | sourceCompatibility = 1.8 9 | targetCompatibility = 1.8 10 | 11 | buildscript { 12 | repositories { 13 | jcenter() 14 | } 15 | dependencies { 16 | classpath 'biz.aQute.bnd:biz.aQute.bnd.gradle:3.2.0' 17 | } 18 | } 19 | 20 | repositories { 21 | mavenLocal() 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | testCompile group: 'junit', name: 'junit', version: '4.11' 27 | } 28 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/csierra/java-functional-applicative-validation/d525408066177d5132b1c3b655696ab3c350f09d/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Oct 10 22:23:26 CEST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'java8-functional' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Applicative.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved. 3 | *

4 | * This library is free software; you can redistribute it and/or modify it under 5 | * the terms of the GNU Lesser General Public License as published by the Free 6 | * Software Foundation; either version 2.1 of the License, or (at your option) 7 | * any later version. 8 | *

9 | * This library is distributed in the hope that it will be useful, but WITHOUT 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 12 | * details. 13 | */ 14 | 15 | package com.liferay.functional; 16 | 17 | import java.util.function.Function; 18 | 19 | /** 20 | * @author Carlos Sierra Andrés 21 | */ 22 | public interface Applicative { 23 | 24 | Applicative apply(Applicative ap); 25 | 26 | Applicative flatApply(Applicative ap); 27 | 28 | Applicative map(Function fun); 29 | 30 | public static , A, B, RESULT> Applicative combine(Function2 fun, Applicative a, Applicative b) { 31 | return a.map(fun.curried()).apply(b); 32 | } 33 | 34 | public static , A, B, C, RESULT> Applicative combine(Function3 fun, Applicative a, Applicative b, Applicative c) { 35 | return a.map(fun.curried()).apply(b).apply(c); 36 | } 37 | 38 | public static , A, B, C, D, RESULT> Applicative combine(Function4 fun, Applicative a, Applicative b, Applicative c, Applicative d) { 39 | return a.map(fun.curried()).apply(b).apply(c).apply(d); 40 | } 41 | 42 | public static , A, B, C, D, E, RESULT> Applicative combine(Function5 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e) { 43 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e); 44 | } 45 | 46 | public static , A, B, C, D, E, F, RESULT> Applicative combine(Function6 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f) { 47 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f); 48 | } 49 | 50 | public static , A, B, C, D, E, F, G, RESULT> Applicative combine(Function7 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g) { 51 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g); 52 | } 53 | 54 | public static , A, B, C, D, E, F, G, H, RESULT> Applicative combine(Function8 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h) { 55 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h); 56 | } 57 | 58 | public static , A, B, C, D, E, F, G, H, I, RESULT> Applicative combine(Function9 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i) { 59 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i); 60 | } 61 | 62 | public static , A, B, C, D, E, F, G, H, I, J, RESULT> Applicative combine(Function10 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j) { 63 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j); 64 | } 65 | 66 | public static , A, B, C, D, E, F, G, H, I, J, K, RESULT> Applicative combine(Function11 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j, Applicative k) { 67 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j).apply(k); 68 | } 69 | 70 | public static , A, B, C, D, E, F, G, H, I, J, K, L, RESULT> Applicative combine(Function12 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j, Applicative k, Applicative l) { 71 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j).apply(k).apply(l); 72 | } 73 | 74 | public static , A, B, C, D, E, F, G, H, I, J, K, L, M, RESULT> Applicative combine(Function13 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j, Applicative k, Applicative l, Applicative m) { 75 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j).apply(k).apply(l).apply(m); 76 | } 77 | 78 | public static , A, B, C, D, E, F, G, H, I, J, K, L, M, N, RESULT> Applicative combine(Function14 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j, Applicative k, Applicative l, Applicative m, Applicative n) { 79 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j).apply(k).apply(l).apply(m).apply(n); 80 | } 81 | 82 | public static , A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, RESULT> Applicative combine(Function15 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j, Applicative k, Applicative l, Applicative m, Applicative n, Applicative o) { 83 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j).apply(k).apply(l).apply(m).apply(n).apply(o); 84 | } 85 | 86 | public static , A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, RESULT> Applicative combine(Function16 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j, Applicative k, Applicative l, Applicative m, Applicative n, Applicative o, Applicative p) { 87 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j).apply(k).apply(l).apply(m).apply(n).apply(o).apply(p); 88 | } 89 | 90 | public static , A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, RESULT> Applicative combine(Function17 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j, Applicative k, Applicative l, Applicative m, Applicative n, Applicative o, Applicative p, Applicative q) { 91 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j).apply(k).apply(l).apply(m).apply(n).apply(o).apply(p).apply(q); 92 | } 93 | 94 | public static , A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, RESULT> Applicative combine(Function18 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j, Applicative k, Applicative l, Applicative m, Applicative n, Applicative o, Applicative p, Applicative q, Applicative r) { 95 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j).apply(k).apply(l).apply(m).apply(n).apply(o).apply(p).apply(q).apply(r); 96 | } 97 | 98 | public static , A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, RESULT> Applicative combine(Function19 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j, Applicative k, Applicative l, Applicative m, Applicative n, Applicative o, Applicative p, Applicative q, Applicative r, Applicative s) { 99 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j).apply(k).apply(l).apply(m).apply(n).apply(o).apply(p).apply(q).apply(r).apply(s); 100 | } 101 | 102 | public static , A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, RESULT> Applicative combine(Function20 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j, Applicative k, Applicative l, Applicative m, Applicative n, Applicative o, Applicative p, Applicative q, Applicative r, Applicative s, Applicative t) { 103 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j).apply(k).apply(l).apply(m).apply(n).apply(o).apply(p).apply(q).apply(r).apply(s).apply(t); 104 | } 105 | 106 | public static , A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, RESULT> Applicative combine(Function21 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j, Applicative k, Applicative l, Applicative m, Applicative n, Applicative o, Applicative p, Applicative q, Applicative r, Applicative s, Applicative t, Applicative u) { 107 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j).apply(k).apply(l).apply(m).apply(n).apply(o).apply(p).apply(q).apply(r).apply(s).apply(t).apply(u); 108 | } 109 | 110 | public static , A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, RESULT> Applicative combine(Function22 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j, Applicative k, Applicative l, Applicative m, Applicative n, Applicative o, Applicative p, Applicative q, Applicative r, Applicative s, Applicative t, Applicative u, Applicative v) { 111 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j).apply(k).apply(l).apply(m).apply(n).apply(o).apply(p).apply(q).apply(r).apply(s).apply(t).apply(u).apply(v); 112 | } 113 | 114 | public static , A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, RESULT> Applicative combine(Function23 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j, Applicative k, Applicative l, Applicative m, Applicative n, Applicative o, Applicative p, Applicative q, Applicative r, Applicative s, Applicative t, Applicative u, Applicative v, Applicative w) { 115 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j).apply(k).apply(l).apply(m).apply(n).apply(o).apply(p).apply(q).apply(r).apply(s).apply(t).apply(u).apply(v).apply(w); 116 | } 117 | 118 | public static , A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, RESULT> Applicative combine(Function24 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j, Applicative k, Applicative l, Applicative m, Applicative n, Applicative o, Applicative p, Applicative q, Applicative r, Applicative s, Applicative t, Applicative u, Applicative v, Applicative w, Applicative x) { 119 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j).apply(k).apply(l).apply(m).apply(n).apply(o).apply(p).apply(q).apply(r).apply(s).apply(t).apply(u).apply(v).apply(w).apply(x); 120 | } 121 | 122 | public static , A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, RESULT> Applicative combine(Function25 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j, Applicative k, Applicative l, Applicative m, Applicative n, Applicative o, Applicative p, Applicative q, Applicative r, Applicative s, Applicative t, Applicative u, Applicative v, Applicative w, Applicative x, Applicative y) { 123 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j).apply(k).apply(l).apply(m).apply(n).apply(o).apply(p).apply(q).apply(r).apply(s).apply(t).apply(u).apply(v).apply(w).apply(x).apply(y); 124 | } 125 | 126 | public static , A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, RESULT> Applicative combine(Function26 fun, Applicative a, Applicative b, Applicative c, Applicative d, Applicative e, Applicative f, Applicative g, Applicative h, Applicative i, Applicative j, Applicative k, Applicative l, Applicative m, Applicative n, Applicative o, Applicative p, Applicative q, Applicative r, Applicative s, Applicative t, Applicative u, Applicative v, Applicative w, Applicative x, Applicative y, Applicative z) { 127 | return a.map(fun.curried()).apply(b).apply(c).apply(d).apply(e).apply(f).apply(g).apply(h).apply(i).apply(j).apply(k).apply(l).apply(m).apply(n).apply(o).apply(p).apply(q).apply(r).apply(s).apply(t).apply(u).apply(v).apply(w).apply(x).apply(y).apply(z); 128 | } 129 | 130 | } 131 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction1.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction1 { 8 | 9 | public RESULT apply(A a) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction10.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction10 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction11.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction11 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction12.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction12 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction13.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction13 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction14.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction14 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction15.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction15 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction16.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction16 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction17.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction17 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction18.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction18 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction19.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction19 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r,S s) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction2.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction2 { 8 | 9 | public RESULT apply(A a,B b) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction20.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction20 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r,S s,T t) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction21.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction21 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r,S s,T t,U u) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction22.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction22 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r,S s,T t,U u,V v) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction23.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction23 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r,S s,T t,U u,V v,W w) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction24.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction24 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r,S s,T t,U u,V v,W w,X x) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction25.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction25 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r,S s,T t,U u,V v,W w,X x,Y y) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction26.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction26 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r,S s,T t,U u,V v,W w,X x,Y y,Z z) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction3.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction3 { 8 | 9 | public RESULT apply(A a,B b,C c) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction4.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction4 { 8 | 9 | public RESULT apply(A a,B b,C c,D d) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction5.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction5 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction6.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction6 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction7.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction7 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction8.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction8 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/CheckedFunction9.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | /** 4 | * @generated 5 | */ 6 | @FunctionalInterface 7 | public interface CheckedFunction9 { 8 | 9 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i) throws Exception; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function1.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function1 { 10 | 11 | public RESULT apply(A a); 12 | 13 | default public Function curried() { 14 | return a -> apply(a); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function10.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function10 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j); 12 | 13 | default public Function>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> apply(a,b,c,d,e,f,g,h,i,j); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function11.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function11 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k); 12 | 13 | default public Function>>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> apply(a,b,c,d,e,f,g,h,i,j,k); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function12.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function12 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l); 12 | 13 | default public Function>>>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> apply(a,b,c,d,e,f,g,h,i,j,k,l); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function13.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function13 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m); 12 | 13 | default public Function>>>>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> apply(a,b,c,d,e,f,g,h,i,j,k,l,m); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function14.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function14 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n); 12 | 13 | default public Function>>>>>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> apply(a,b,c,d,e,f,g,h,i,j,k,l,m,n); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function15.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function15 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o); 12 | 13 | default public Function>>>>>>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> apply(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function16.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function16 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p); 12 | 13 | default public Function>>>>>>>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> apply(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function17.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function17 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q); 12 | 13 | default public Function>>>>>>>>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> apply(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function18.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function18 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r); 12 | 13 | default public Function>>>>>>>>>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> apply(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function19.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function19 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r,S s); 12 | 13 | default public Function>>>>>>>>>>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> s -> apply(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function2.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function2 { 10 | 11 | public RESULT apply(A a,B b); 12 | 13 | default public Function> curried() { 14 | return a -> b -> apply(a,b); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function20.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function20 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r,S s,T t); 12 | 13 | default public Function>>>>>>>>>>>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> s -> t -> apply(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function21.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function21 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r,S s,T t,U u); 12 | 13 | default public Function>>>>>>>>>>>>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> s -> t -> u -> apply(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function22.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function22 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r,S s,T t,U u,V v); 12 | 13 | default public Function>>>>>>>>>>>>>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> s -> t -> u -> v -> apply(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function23.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function23 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r,S s,T t,U u,V v,W w); 12 | 13 | default public Function>>>>>>>>>>>>>>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> s -> t -> u -> v -> w -> apply(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function24.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function24 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r,S s,T t,U u,V v,W w,X x); 12 | 13 | default public Function>>>>>>>>>>>>>>>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> s -> t -> u -> v -> w -> x -> apply(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function25.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function25 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r,S s,T t,U u,V v,W w,X x,Y y); 12 | 13 | default public Function>>>>>>>>>>>>>>>>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> s -> t -> u -> v -> w -> x -> y -> apply(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function26.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function26 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i,J j,K k,L l,M m,N n,O o,P p,Q q,R r,S s,T t,U u,V v,W w,X x,Y y,Z z); 12 | 13 | default public Function>>>>>>>>>>>>>>>>>>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> m -> n -> o -> p -> q -> r -> s -> t -> u -> v -> w -> x -> y -> z -> apply(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function3.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function3 { 10 | 11 | public RESULT apply(A a,B b,C c); 12 | 13 | default public Function>> curried() { 14 | return a -> b -> c -> apply(a,b,c); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function4.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function4 { 10 | 11 | public RESULT apply(A a,B b,C c,D d); 12 | 13 | default public Function>>> curried() { 14 | return a -> b -> c -> d -> apply(a,b,c,d); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function5.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function5 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e); 12 | 13 | default public Function>>>> curried() { 14 | return a -> b -> c -> d -> e -> apply(a,b,c,d,e); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function6.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function6 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f); 12 | 13 | default public Function>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> apply(a,b,c,d,e,f); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function7.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function7 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g); 12 | 13 | default public Function>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> apply(a,b,c,d,e,f,g); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function8.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function8 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h); 12 | 13 | default public Function>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> apply(a,b,c,d,e,f,g,h); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Function9.java: -------------------------------------------------------------------------------- 1 | package com.liferay.functional; 2 | 3 | import java.util.function.Function; 4 | 5 | /** 6 | * @generated 7 | */ 8 | @FunctionalInterface 9 | public interface Function9 { 10 | 11 | public RESULT apply(A a,B b,C c,D d,E e,F f,G g,H h,I i); 12 | 13 | default public Function>>>>>>>> curried() { 14 | return a -> b -> c -> d -> e -> f -> g -> h -> i -> apply(a,b,c,d,e,f,g,h,i); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/Monoid.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved. 3 | *

4 | * This library is free software; you can redistribute it and/or modify it under 5 | * the terms of the GNU Lesser General Public License as published by the Free 6 | * Software Foundation; either version 2.1 of the License, or (at your option) 7 | * any later version. 8 | *

9 | * This library is distributed in the hope that it will be useful, but WITHOUT 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 12 | * details. 13 | */ 14 | 15 | package com.liferay.functional; 16 | 17 | /** 18 | * @author Carlos Sierra Andrés 19 | */ 20 | public interface Monoid { 21 | 22 | Monoid mappend(Monoid other); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/fieldprovider/FieldFail.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2000-present Liferay, Inc. All rights reserved. 3 | *

4 | * This library is free software; you can redistribute it and/or modify it under 5 | * the terms of the GNU Lesser General Public License as published by the Free 6 | * Software Foundation; either version 2.1 of the License, or (at your option) 7 | * any later version. 8 | *

9 | * This library is distributed in the hope that it will be useful, but WITHOUT 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 12 | * details. 13 | */ 14 | 15 | package com.liferay.functional.fieldprovider; 16 | 17 | import com.liferay.functional.Monoid; 18 | import com.liferay.functional.validation.Fail; 19 | 20 | import java.util.Collection; 21 | import java.util.Collections; 22 | import java.util.HashMap; 23 | import java.util.Map; 24 | import java.util.Set; 25 | import java.util.function.Function; 26 | import java.util.stream.Collectors; 27 | import java.util.stream.Stream; 28 | 29 | /** 30 | * @author Carlos Sierra Andrés 31 | */ 32 | public class FieldFail implements Monoid { 33 | 34 | final Map _failures; 35 | 36 | public FieldFail(String field, Fail fail) { 37 | this(); 38 | 39 | _failures.put(field, fail); 40 | } 41 | 42 | public FieldFail() { 43 | _failures = new HashMap<>(); 44 | } 45 | 46 | public FieldFail(String fieldName, FieldFail nested) { 47 | this(); 48 | 49 | _failures.put(fieldName, nested); 50 | } 51 | 52 | FieldFail(Map failures) { 53 | _failures = failures; 54 | } 55 | 56 | @Override 57 | public boolean equals(Object o) { 58 | if (this == o) return true; 59 | if (o == null || getClass() != o.getClass()) return false; 60 | 61 | FieldFail fieldFail = (FieldFail) o; 62 | 63 | return _failures.equals(fieldFail._failures); 64 | 65 | } 66 | 67 | @Override 68 | public int hashCode() { 69 | return _failures.hashCode(); 70 | } 71 | 72 | public FieldFail(String field, String message) { 73 | this(field, new Fail(message)); 74 | } 75 | 76 | @Override 77 | public FieldFail mappend(Monoid other) { 78 | Map collect = Stream.of( 79 | ((FieldFail) other)._failures, _failures). 80 | map(Map::entrySet). 81 | flatMap(Collection::stream).collect(Collectors.toMap( 82 | Map.Entry::getKey, Map.Entry::getValue, Monoid::mappend 83 | )); 84 | 85 | return new FieldFail(collect); 86 | } 87 | 88 | @Override 89 | public String toString() { 90 | return "FieldFail{" + 91 | "_failures=" + _failures + 92 | '}'; 93 | } 94 | 95 | public static Function fromFail(String fieldName) { 96 | return (Fail fail) -> new FieldFail(fieldName, fail); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/fieldprovider/FieldProvider.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved. 3 | *

4 | * This library is free software; you can redistribute it and/or modify it under 5 | * the terms of the GNU Lesser General Public License as published by the Free 6 | * Software Foundation; either version 2.1 of the License, or (at your option) 7 | * any later version. 8 | *

9 | * This library is distributed in the hope that it will be useful, but WITHOUT 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 12 | * details. 13 | */ 14 | 15 | package com.liferay.functional.fieldprovider; 16 | 17 | import com.liferay.functional.Function2; 18 | import com.liferay.functional.Monoid; 19 | import com.liferay.functional.validation.Fail; 20 | import com.liferay.functional.validation.Validation; 21 | import com.liferay.functional.validation.Validation.Failure; 22 | import com.liferay.functional.validation.Validator; 23 | 24 | import java.util.ArrayList; 25 | import java.util.Collection; 26 | import java.util.Collections; 27 | import java.util.Optional; 28 | 29 | import static com.liferay.functional.validation.Validation.just; 30 | 31 | /** 32 | * @author Carlos Sierra Andrés 33 | */ 34 | public interface FieldProvider { 35 | 36 | Optional get(String name); 37 | 38 | Validator safeFieldProvider(); 39 | 40 | static SafeCast safeCast(Class clazz) { 41 | return input -> { 42 | try { 43 | return new Validation.Success<>(clazz.cast(input)); 44 | } 45 | catch (ClassCastException cce) { 46 | return new Failure<>( 47 | new Fail("can't be casted to " + clazz)); 48 | } 49 | }; 50 | } 51 | 52 | public static Validator, T, Fail> mandatory() { 53 | return input -> { 54 | if (input.isPresent()) { 55 | return just(input.get()); 56 | } 57 | else { 58 | return new Failure<>(new Fail("should not be empty")); 59 | } 60 | }; 61 | } 62 | 63 | public static Validator, Optional, Fail> optional() { 64 | return Validation::just; 65 | } 66 | 67 | public static Validator, Optional, Fail> ifPresent( 68 | Validator validator) { 69 | 70 | return input -> { 71 | if (input.isPresent()) { 72 | return validator.validate(input.get()).map( 73 | Optional::ofNullable); 74 | } 75 | else { 76 | return just(Optional.empty()); 77 | } 78 | }; 79 | 80 | } 81 | 82 | interface Adaptor> { 83 | Validation safeGet( 84 | String fieldName, Validator, R, F> validator); 85 | 86 | Validation, FieldFail> getAdaptor(String fieldName); 87 | 88 | static > 89 | Validation safeGet( 90 | Adaptor adaptor, String fieldName, 91 | Validator , R, F> validator) { 92 | 93 | return adaptor.safeGet(fieldName, validator); 94 | } 95 | 96 | static > 97 | Validation safeGet( 98 | Validation, FieldFail> adaptor, String fieldName, 99 | Validator, R, F> validator) { 100 | 101 | return adaptor.flatMap(a -> a.safeGet(fieldName, validator)); 102 | } 103 | 104 | static > 105 | Validation, FieldFail> focus( 106 | Adaptor adaptor, String ... fields) { 107 | 108 | if (fields.length == 0) { 109 | return just(adaptor); 110 | } 111 | 112 | Validation, FieldFail> current = adaptor.getAdaptor( 113 | fields[0]); 114 | 115 | if (fields.length == 1) { 116 | return current; 117 | } 118 | 119 | for (int i = 1; i < fields.length; i++) { 120 | String field = fields[i]; 121 | 122 | current = current.flatMap(a -> a.getAdaptor(field)); 123 | 124 | } 125 | 126 | return current; 127 | } 128 | } 129 | 130 | default > Adaptor getAdaptor( 131 | Function2 map) { 132 | 133 | return getAdaptor(map, Collections.emptyList()); 134 | } 135 | 136 | default > Adaptor getAdaptor( 137 | Function2 map, 138 | Collection stack) { 139 | 140 | return new Adaptor() { 141 | 142 | @Override 143 | public Validation safeGet( 144 | String fieldName, Validator, R, F> validator) { 145 | 146 | return validator.validate(get(fieldName)).mapFailures( 147 | map.curried().apply(fieldName)).mapFailures(f -> { 148 | 149 | for (String s : stack) { 150 | f = new FieldFail(s, f); 151 | } 152 | 153 | return f; 154 | }); 155 | } 156 | 157 | @Override 158 | public Validation, FieldFail> getAdaptor( 159 | String fieldName) { 160 | 161 | ArrayList objects = new ArrayList<>(stack); 162 | 163 | objects.add(0, fieldName); 164 | 165 | return mandatory(). 166 | compose(safeFieldProvider()). 167 | validate(get(fieldName)). 168 | map(fp -> fp.getAdaptor(map, objects)).mapFailures( 169 | f -> new FieldFail(fieldName, f)); 170 | } 171 | 172 | }; 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/fieldprovider/MapStringProvider.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2000-present Liferay, Inc. All rights reserved. 3 | *

4 | * This library is free software; you can redistribute it and/or modify it under 5 | * the terms of the GNU Lesser General Public License as published by the Free 6 | * Software Foundation; either version 2.1 of the License, or (at your option) 7 | * any later version. 8 | *

9 | * This library is distributed in the hope that it will be useful, but WITHOUT 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 12 | * details. 13 | */ 14 | 15 | package com.liferay.functional.fieldprovider; 16 | 17 | import com.liferay.functional.validation.Fail; 18 | import com.liferay.functional.validation.Validator; 19 | 20 | import java.util.Map; 21 | import java.util.Optional; 22 | 23 | /** 24 | * @author Carlos Sierra Andrés 25 | */ 26 | public class MapStringProvider implements StringProvider { 27 | 28 | private Map _map; 29 | 30 | public MapStringProvider(Map map) { 31 | _map = map; 32 | } 33 | 34 | @Override 35 | public Optional get(String name) { 36 | return Optional.ofNullable(_map.get(name)); 37 | } 38 | 39 | @Override 40 | public Validator safeFieldProvider() { 41 | return FieldProvider.safeCast(Map.class).map(MapStringProvider::new); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/fieldprovider/SafeCast.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved. 3 | *

4 | * This library is free software; you can redistribute it and/or modify it under 5 | * the terms of the GNU Lesser General Public License as published by the Free 6 | * Software Foundation; either version 2.1 of the License, or (at your option) 7 | * any later version. 8 | *

9 | * This library is distributed in the hope that it will be useful, but WITHOUT 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 12 | * details. 13 | */ 14 | 15 | package com.liferay.functional.fieldprovider; 16 | 17 | import com.liferay.functional.Monoid; 18 | import com.liferay.functional.validation.Validator; 19 | 20 | /** 21 | * @author Carlos Sierra Andrés 22 | */ 23 | public interface SafeCast> 24 | extends Validator { 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/fieldprovider/StringProvider.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved. 3 | *

4 | * This library is free software; you can redistribute it and/or modify it under 5 | * the terms of the GNU Lesser General Public License as published by the Free 6 | * Software Foundation; either version 2.1 of the License, or (at your option) 7 | * any later version. 8 | *

9 | * This library is distributed in the hope that it will be useful, but WITHOUT 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 12 | * details. 13 | */ 14 | 15 | package com.liferay.functional.fieldprovider; 16 | 17 | import java.util.Optional; 18 | 19 | /** 20 | * @author Carlos Sierra Andrés 21 | */ 22 | public interface StringProvider extends FieldProvider { 23 | 24 | @Override 25 | Optional get(String name); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/validation/Composer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2000-present Liferay, Inc. All rights reserved. 3 | *

4 | * This library is free software; you can redistribute it and/or modify it under 5 | * the terms of the GNU Lesser General Public License as published by the Free 6 | * Software Foundation; either version 2.1 of the License, or (at your option) 7 | * any later version. 8 | *

9 | * This library is distributed in the hope that it will be useful, but WITHOUT 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 12 | * details. 13 | */ 14 | 15 | package com.liferay.functional.validation; 16 | 17 | import com.liferay.functional.Monoid; 18 | 19 | /** 20 | * @author Carlos Sierra Andrés 21 | */ 22 | public interface Composer { 23 | 24 | static > Validator compose( 25 | Validator a, Validator b) { 26 | 27 | return a.compose(b); 28 | } 29 | 30 | static > Validator compose( 31 | Validator a, Validator b, Validator c) { 32 | 33 | return a.compose(b).compose(c); 34 | } 35 | 36 | static > Validator compose( 37 | Validator a, Validator b, Validator c, Validator d) { 38 | 39 | return a.compose(b).compose(c).compose(d); 40 | } 41 | 42 | static > Validator compose( 43 | Validator a, Validator b, Validator c, Validator d, Validator e) { 44 | 45 | return a.compose(b).compose(c).compose(d).compose(e); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/validation/Fail.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2000-present Liferay, Inc. All rights reserved. 3 | *

4 | * This library is free software; you can redistribute it and/or modify it under 5 | * the terms of the GNU Lesser General Public License as published by the Free 6 | * Software Foundation; either version 2.1 of the License, or (at your option) 7 | * any later version. 8 | *

9 | * This library is distributed in the hope that it will be useful, but WITHOUT 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 12 | * details. 13 | */ 14 | 15 | package com.liferay.functional.validation; 16 | 17 | import com.liferay.functional.Monoid; 18 | 19 | import java.util.Arrays; 20 | import java.util.Collections; 21 | import java.util.HashSet; 22 | import java.util.Set; 23 | import java.util.stream.Collectors; 24 | import java.util.stream.Stream; 25 | 26 | /** 27 | * @author Carlos Sierra Andrés 28 | */ 29 | public class Fail implements Monoid { 30 | 31 | final Set _failures; 32 | 33 | public Fail(Set failures) { 34 | _failures = failures; 35 | } 36 | 37 | public Fail(String... failures) { 38 | _failures = new HashSet<>(Arrays.asList(failures)); 39 | } 40 | 41 | public Fail prependAll(String prefix) { 42 | return new Fail(_failures.stream().map(s -> prefix + s).collect( 43 | Collectors.toSet())); 44 | } 45 | 46 | public Fail(String error) { 47 | _failures = Collections.singleton(error); 48 | } 49 | 50 | @Override 51 | public Fail mappend(Monoid other) { 52 | Set otherFailures = ((Fail) other)._failures; 53 | 54 | Stream stream = Stream.concat( 55 | _failures.stream(), otherFailures.stream()); 56 | 57 | return new Fail(stream.collect(Collectors.toSet())); 58 | } 59 | 60 | @Override 61 | public boolean equals(Object o) { 62 | if (this == o) return true; 63 | if (o == null || getClass() != o.getClass()) return false; 64 | 65 | Fail fail = (Fail) o; 66 | 67 | return _failures.equals(fail._failures); 68 | } 69 | 70 | @Override 71 | public int hashCode() { 72 | return _failures.hashCode(); 73 | } 74 | 75 | @Override 76 | public String toString() { 77 | return "Fail{" + 78 | "_failures=" + _failures + 79 | '}'; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/com/liferay/functional/validation/Validator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved. 3 | *

4 | * This library is free software; you can redistribute it and/or modify it under 5 | * the terms of the GNU Lesser General Public License as published by the Free 6 | * Software Foundation; either version 2.1 of the License, or (at your option) 7 | * any later version. 8 | *

9 | * This library is distributed in the hope that it will be useful, but WITHOUT 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 12 | * details. 13 | */ 14 | 15 | package com.liferay.functional.validation; 16 | 17 | import com.liferay.functional.Function10; 18 | import com.liferay.functional.Function11; 19 | import com.liferay.functional.Function12; 20 | import com.liferay.functional.Function13; 21 | import com.liferay.functional.Function14; 22 | import com.liferay.functional.Function15; 23 | import com.liferay.functional.Function16; 24 | import com.liferay.functional.Function17; 25 | import com.liferay.functional.Function18; 26 | import com.liferay.functional.Function19; 27 | import com.liferay.functional.Function2; 28 | import com.liferay.functional.Function20; 29 | import com.liferay.functional.Function21; 30 | import com.liferay.functional.Function22; 31 | import com.liferay.functional.Function23; 32 | import com.liferay.functional.Function24; 33 | import com.liferay.functional.Function25; 34 | import com.liferay.functional.Function26; 35 | import com.liferay.functional.Function3; 36 | import com.liferay.functional.Function4; 37 | import com.liferay.functional.Function5; 38 | import com.liferay.functional.Function6; 39 | import com.liferay.functional.Function7; 40 | import com.liferay.functional.Function8; 41 | import com.liferay.functional.Function9; 42 | import com.liferay.functional.Monoid; 43 | import com.liferay.functional.validation.Validation.Failure; 44 | import com.liferay.functional.validation.Validation.Success; 45 | 46 | import java.util.Arrays; 47 | import java.util.function.Function; 48 | import java.util.function.Predicate; 49 | 50 | /** 51 | * @author Carlos Sierra Andrés 52 | */ 53 | public interface Validator> 54 | extends Function> { 55 | 56 | Validation validate(T input); 57 | 58 | default Validator and(Validator other) { 59 | return input -> 60 | Validation.apply( 61 | (x, y) -> x, validate(input), other.validate(input)); 62 | } 63 | 64 | @Override 65 | default Validation apply(T t) { 66 | return validate(t); 67 | } 68 | 69 | default Validator map(Function fun) { 70 | 71 | return input -> { 72 | Validation result = validate(input); 73 | 74 | return (Validation)result.map(fun); 75 | }; 76 | } 77 | 78 | static > Validator partial( 79 | Validator validator) { 80 | 81 | return input -> validator.validate(input).map(ign -> input); 82 | } 83 | 84 | @SafeVarargs 85 | static > Validator partials( 86 | Validator ... validators) { 87 | 88 | return Arrays.stream(validators).map(Validator::partial).reduce( 89 | (Validation::just), Validator::and); 90 | } 91 | 92 | default Validator applyTo(Validator, F> ap) { 93 | return ap.flatMap(this::map); 94 | } 95 | 96 | default public Validator flatMap( 97 | Function> fun) { 98 | 99 | return input -> validate(input).flatMap( 100 | fun.andThen(v -> v.validate(input))); 101 | } 102 | 103 | default Validator compose(Validator validator) { 104 | return input -> (Validation) validate(input).flatMap( 105 | validator::validate); 106 | } 107 | 108 | default > Validator adapt( 109 | Function val, Function errors) { 110 | 111 | return input -> validate(val.apply(input)).mapFailures(errors); 112 | } 113 | 114 | default > Validator flatAdapt( 115 | Function> val, Function errors) { 116 | 117 | return input -> val.apply(input).flatMap(this::validate).mapFailures( 118 | errors); 119 | } 120 | 121 | static > Validator predicate( 122 | Predicate predicate, Function error) { 123 | 124 | return input -> { 125 | if (predicate.test(input)) { 126 | return new Success<>(input); 127 | } 128 | else { 129 | return new Failure<>(error.apply(input)); 130 | } 131 | }; 132 | } 133 | 134 | public static > Validator just(R r) { 135 | return input -> Validation.just(r); 136 | } 137 | 138 | public static > Validator apply(Function2 fun, Validator a, Validator b) { 139 | return b.applyTo(a.applyTo(just((A aa) -> fun.curried().apply(aa)))); 140 | } 141 | 142 | public static > Validator apply(Function3 fun, Validator a, Validator b, Validator c) { 143 | return c.applyTo(Validator.apply((A aa, B bb) -> fun.curried().apply(aa).apply(bb), a, b)); 144 | } 145 | 146 | public static > Validator apply(Function4 fun, Validator a, Validator b, Validator c, Validator d) { 147 | return d.applyTo(Validator.apply((A aa, B bb, C cc) -> fun.curried().apply(aa).apply(bb).apply(cc), a, b, c)); 148 | } 149 | 150 | public static > Validator apply(Function5 fun, Validator a, Validator b, Validator c, Validator d, Validator e) { 151 | return e.applyTo(Validator.apply((A aa, B bb, C cc, D dd) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd), a, b, c, d)); 152 | } 153 | 154 | public static > Validator apply(Function6 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f) { 155 | return f.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee), a, b, c, d, e)); 156 | } 157 | 158 | public static > Validator apply(Function7 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g) { 159 | return g.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff), a, b, c, d, e, f)); 160 | } 161 | 162 | public static > Validator apply(Function8 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h) { 163 | return h.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg), a, b, c, d, e, f, g)); 164 | } 165 | 166 | public static > Validator apply(Function9 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i) { 167 | return i.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh), a, b, c, d, e, f, g, h)); 168 | } 169 | 170 | public static > Validator apply(Function10 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j) { 171 | return j.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii), a, b, c, d, e, f, g, h, i)); 172 | } 173 | 174 | public static > Validator apply(Function11 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j, Validator k) { 175 | return k.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj), a, b, c, d, e, f, g, h, i, j)); 176 | } 177 | 178 | public static > Validator apply(Function12 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j, Validator k, Validator l) { 179 | return l.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk), a, b, c, d, e, f, g, h, i, j, k)); 180 | } 181 | 182 | public static > Validator apply(Function13 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j, Validator k, Validator l, Validator m) { 183 | return m.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll), a, b, c, d, e, f, g, h, i, j, k, l)); 184 | } 185 | 186 | public static > Validator apply(Function14 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j, Validator k, Validator l, Validator m, Validator n) { 187 | return n.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm), a, b, c, d, e, f, g, h, i, j, k, l, m)); 188 | } 189 | 190 | public static > Validator apply(Function15 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j, Validator k, Validator l, Validator m, Validator n, Validator o) { 191 | return o.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn), a, b, c, d, e, f, g, h, i, j, k, l, m, n)); 192 | } 193 | 194 | public static > Validator apply(Function16 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j, Validator k, Validator l, Validator m, Validator n, Validator o, Validator p) { 195 | return p.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)); 196 | } 197 | 198 | public static > Validator apply(Function17 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j, Validator k, Validator l, Validator m, Validator n, Validator o, Validator p, Validator q) { 199 | return q.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)); 200 | } 201 | 202 | public static > Validator apply(Function18 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j, Validator k, Validator l, Validator m, Validator n, Validator o, Validator p, Validator q, Validator r) { 203 | return r.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q)); 204 | } 205 | 206 | public static > Validator apply(Function19 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j, Validator k, Validator l, Validator m, Validator n, Validator o, Validator p, Validator q, Validator r, Validator s) { 207 | return s.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq, R rr) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq).apply(rr), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r)); 208 | } 209 | 210 | public static > Validator apply(Function20 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j, Validator k, Validator l, Validator m, Validator n, Validator o, Validator p, Validator q, Validator r, Validator s, Validator t) { 211 | return t.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq, R rr, S ss) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq).apply(rr).apply(ss), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s)); 212 | } 213 | 214 | public static > Validator apply(Function21 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j, Validator k, Validator l, Validator m, Validator n, Validator o, Validator p, Validator q, Validator r, Validator s, Validator t, Validator u) { 215 | return u.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq, R rr, S ss, T tt) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq).apply(rr).apply(ss).apply(tt), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t)); 216 | } 217 | 218 | public static > Validator apply(Function22 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j, Validator k, Validator l, Validator m, Validator n, Validator o, Validator p, Validator q, Validator r, Validator s, Validator t, Validator u, Validator v) { 219 | return v.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq, R rr, S ss, T tt, U uu) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq).apply(rr).apply(ss).apply(tt).apply(uu), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u)); 220 | } 221 | 222 | public static > Validator apply(Function23 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j, Validator k, Validator l, Validator m, Validator n, Validator o, Validator p, Validator q, Validator r, Validator s, Validator t, Validator u, Validator v, Validator w) { 223 | return w.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq, R rr, S ss, T tt, U uu, V vv) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq).apply(rr).apply(ss).apply(tt).apply(uu).apply(vv), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v)); 224 | } 225 | 226 | public static > Validator apply(Function24 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j, Validator k, Validator l, Validator m, Validator n, Validator o, Validator p, Validator q, Validator r, Validator s, Validator t, Validator u, Validator v, Validator w, Validator x) { 227 | return x.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq, R rr, S ss, T tt, U uu, V vv, W ww) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq).apply(rr).apply(ss).apply(tt).apply(uu).apply(vv).apply(ww), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w)); 228 | } 229 | 230 | public static > Validator apply(Function25 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j, Validator k, Validator l, Validator m, Validator n, Validator o, Validator p, Validator q, Validator r, Validator s, Validator t, Validator u, Validator v, Validator w, Validator x, Validator y) { 231 | return y.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq, R rr, S ss, T tt, U uu, V vv, W ww, X xx) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq).apply(rr).apply(ss).apply(tt).apply(uu).apply(vv).apply(ww).apply(xx), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x)); 232 | } 233 | 234 | public static > Validator apply(Function26 fun, Validator a, Validator b, Validator c, Validator d, Validator e, Validator f, Validator g, Validator h, Validator i, Validator j, Validator k, Validator l, Validator m, Validator n, Validator o, Validator p, Validator q, Validator r, Validator s, Validator t, Validator u, Validator v, Validator w, Validator x, Validator y, Validator z) { 235 | return z.applyTo(Validator.apply((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq, R rr, S ss, T tt, U uu, V vv, W ww, X xx, Y yy) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq).apply(rr).apply(ss).apply(tt).apply(uu).apply(vv).apply(ww).apply(xx).apply(yy), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y)); 236 | } 237 | 238 | // static Validator hasLength(int length) { 239 | // return predicate( 240 | // input -> input.length() == length, 241 | // input -> input + " must have " + length + " letters"); 242 | // } 243 | // 244 | // static Validator longerThan(int length) { 245 | // return predicate( 246 | // input -> input.length() > length, 247 | // input -> input + " must have " + length + " letters"); 248 | // } 249 | // 250 | // Validator isANumber = predicate( 251 | // input -> { 252 | // try { 253 | // Integer.parseInt(input); 254 | // 255 | // return true; 256 | // } 257 | // catch (Exception e) { 258 | // return false; 259 | // } 260 | // }, input -> input + " is not a number"); 261 | // 262 | // static Validator greaterThan(int min) { 263 | // return predicate( 264 | // input -> input > min, 265 | // input -> input + " must be greater than" + min 266 | // ); 267 | // } 268 | // 269 | // static Validator lowerThan(int max) { 270 | // return predicate( 271 | // input -> input < max, 272 | // input -> input + " should be lower than " + max 273 | // ); 274 | // } 275 | // 276 | // static Validator startsWith(String prefix) { 277 | // return predicate( 278 | // input -> input.startsWith(prefix), 279 | // input -> input + " should start with " + prefix 280 | // ); 281 | // } 282 | // 283 | // static Validator endsWith(String suffix) { 284 | // return predicate( 285 | // input -> input.endsWith(suffix), 286 | // input -> input + " should start with " + suffix 287 | // ); 288 | // } 289 | // 290 | // static Validator, Optional> ifPresent( 291 | // Validator validator) { 292 | // 293 | // return (Optional input) -> { 294 | // if (!input.isPresent()) { 295 | // return new Success<>(input); 296 | // } 297 | // else { 298 | // return (Validation>) 299 | // validator.validate(input.get()).map(Optional::of); 300 | // } 301 | // }; 302 | // } 303 | // 304 | // Validator safeInt = isANumber.map(Integer::parseInt); 305 | // 306 | // static Validator, T> notEmpty() { 307 | // return input -> { 308 | // if (input.isPresent()) { 309 | // return new Success<>(input.get()); 310 | // } 311 | // else { 312 | // return new Failure<>( 313 | // Collections.singleton("Input is empty")); 314 | // } 315 | // }; 316 | // } 317 | 318 | } 319 | -------------------------------------------------------------------------------- /src/test/java/com/liferay/functional/fieldprovider/FieldProviderTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2000-present Liferay, Inc. All rights reserved. 3 | *

4 | * This library is free software; you can redistribute it and/or modify it under 5 | * the terms of the GNU Lesser General Public License as published by the Free 6 | * Software Foundation; either version 2.1 of the License, or (at your option) 7 | * any later version. 8 | *

9 | * This library is distributed in the hope that it will be useful, but WITHOUT 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 12 | * details. 13 | */ 14 | 15 | package com.liferay.functional.fieldprovider; 16 | 17 | import com.liferay.functional.Monoid; 18 | import com.liferay.functional.fieldprovider.FieldProvider.Adaptor; 19 | import com.liferay.functional.validation.Fail; 20 | import com.liferay.functional.validation.Validation; 21 | import com.liferay.functional.validation.Validation.Failure; 22 | import com.liferay.functional.validation.Validator; 23 | import org.junit.Assert; 24 | import org.junit.Test; 25 | 26 | import java.util.HashMap; 27 | import java.util.Map; 28 | import java.util.Optional; 29 | 30 | import static com.liferay.functional.fieldprovider.FieldProvider.Adaptor.focus; 31 | import static com.liferay.functional.fieldprovider.FieldProvider.mandatory; 32 | import static com.liferay.functional.fieldprovider.FieldProvider.safeCast; 33 | import static com.liferay.functional.validation.Composer.compose; 34 | import static com.liferay.functional.validation.Validation.apply; 35 | import static com.liferay.functional.validation.Validation.just; 36 | import static com.liferay.functional.validation.Validator.predicate; 37 | 38 | /** 39 | * @author Carlos Sierra Andrés 40 | */ 41 | public class FieldProviderTest { 42 | 43 | static class MapFieldProvider implements FieldProvider { 44 | 45 | private Map _map; 46 | 47 | public MapFieldProvider(Map map) { 48 | _map = map; 49 | } 50 | 51 | @Override 52 | public Optional get(String name) { 53 | return Optional.ofNullable((T)_map.get(name)); 54 | } 55 | 56 | @Override 57 | public Validator safeFieldProvider() { 58 | return FieldProvider.safeCast(Map.class).map(MapFieldProvider::new); 59 | } 60 | 61 | } 62 | 63 | @Test 64 | public void testFieldProvider() { 65 | HashMap map = new HashMap<>(); 66 | 67 | map.put("test", "testValueLongerThan10"); 68 | 69 | MapFieldProvider fieldProvider = new MapFieldProvider(map); 70 | 71 | Validator longerThan10 = 72 | predicate( 73 | s -> s.length() > 10, s -> new Fail("must be longer than 10")); 74 | 75 | Adaptor adaptor = fieldProvider.getAdaptor(FieldFail::new); 76 | 77 | Validation validation = adaptor.safeGet( 78 | "test", compose(mandatory(), safeCast(String.class), longerThan10)); 79 | 80 | Assert.assertEquals(just("testValueLongerThan10"), validation); 81 | } 82 | 83 | @Test 84 | public void testFieldProviderWhenNull() { 85 | HashMap map = new HashMap<>(); 86 | 87 | MapFieldProvider fieldProvider = new MapFieldProvider(map); 88 | 89 | Validator longerThan10 = 90 | predicate( 91 | s -> s.length() > 10, s -> new Fail("must be longer than 10")); 92 | 93 | Adaptor adaptor = fieldProvider.getAdaptor(FieldFail::new); 94 | 95 | Validation validation = 96 | adaptor.safeGet( 97 | "test", 98 | compose(mandatory(), safeCast(String.class), longerThan10)); 99 | 100 | Assert.assertEquals( 101 | new Failure( 102 | new FieldFail("test", "should not be empty")), validation); 103 | } 104 | 105 | @Test 106 | public void testFieldProviderWhenFailure() { 107 | HashMap map = new HashMap<>(); 108 | 109 | map.put("test", "testValue"); 110 | 111 | MapFieldProvider fieldProvider = new MapFieldProvider(map); 112 | 113 | Validator longerThan10 = 114 | predicate( 115 | s -> s.length() > 10, s -> new Fail("must be longer than 10")); 116 | 117 | Adaptor adaptor = fieldProvider.getAdaptor(FieldFail::new); 118 | 119 | Validation validation = 120 | adaptor.safeGet( 121 | "test", 122 | compose(mandatory(), safeCast(String.class), longerThan10)); 123 | 124 | Assert.assertEquals( 125 | new Failure( 126 | new FieldFail("test", "must be longer than 10")), 127 | validation); 128 | } 129 | 130 | @Test 131 | public void testFieldProviderWhenNested() { 132 | HashMap map = new HashMap<>(); 133 | 134 | HashMap nested = new HashMap<>(); 135 | 136 | map.put("nested", nested); 137 | 138 | nested.put("test", "testValueLongerThan10"); 139 | 140 | FieldProvider fieldProvider = new MapFieldProvider(map); 141 | 142 | Validator longerThan10 = 143 | predicate( 144 | s -> s.length() > 10, s -> new Fail("must be longer than 10")); 145 | 146 | Adaptor adaptor = fieldProvider.getAdaptor(FieldFail::new); 147 | 148 | Assert.assertEquals( 149 | just("testValueLongerThan10"), 150 | adaptor.getAdaptor("nested").flatMap(nestedAdaptor -> 151 | nestedAdaptor.safeGet( 152 | "test", 153 | compose( 154 | mandatory(), safeCast(String.class), longerThan10)))); 155 | } 156 | 157 | @Test 158 | public void testFieldProviderWhenNestedIsNull() { 159 | HashMap map = new HashMap<>(); 160 | 161 | FieldProvider fieldProvider = new MapFieldProvider(map); 162 | 163 | Validator longerThan10 = 164 | predicate( 165 | s -> s.length() > 10, s -> new Fail("must be longer than 10")); 166 | 167 | Adaptor adaptor = fieldProvider.getAdaptor(FieldFail::new); 168 | 169 | Validation validation = 170 | adaptor.getAdaptor("nested").flatMap(nestedAdaptor -> 171 | nestedAdaptor.safeGet( 172 | "test", 173 | compose( 174 | mandatory(), safeCast(String.class), longerThan10))); 175 | 176 | Assert.assertEquals( 177 | new Failure<>( 178 | new FieldFail("nested", "should not be empty")), validation); 179 | } 180 | 181 | @Test 182 | public void testFieldProviderWhenNestedhasWrongType() { 183 | HashMap map = new HashMap<>(); 184 | 185 | map.put("nested", "wrong"); 186 | 187 | FieldProvider fieldProvider = new MapFieldProvider(map); 188 | 189 | Validator longerThan10 = 190 | predicate( 191 | s -> s.length() > 10, s -> new Fail("must be longer than 10")); 192 | 193 | Adaptor adaptor = fieldProvider.getAdaptor(FieldFail::new); 194 | 195 | Validation validation = 196 | adaptor.getAdaptor("nested").flatMap(nestedAdaptor -> 197 | nestedAdaptor.safeGet( 198 | "test", 199 | compose( 200 | mandatory(), safeCast(String.class), longerThan10))); 201 | 202 | Assert.assertEquals( 203 | new Failure<>( 204 | new FieldFail( 205 | "nested", "can't be casted to interface java.util.Map")), 206 | validation); 207 | } 208 | 209 | @Test 210 | public void testFailureFieldProviderWhenDeepNested() { 211 | HashMap map = new HashMap<>(); 212 | 213 | HashMap nestedMap2 = new HashMap<>(); 214 | 215 | nestedMap2.put("test", "test"); 216 | 217 | HashMap nestedMap = new HashMap<>(); 218 | 219 | nestedMap.put("nested2", nestedMap2); 220 | 221 | map.put("nested", nestedMap); 222 | 223 | FieldProvider fieldProvider = new MapFieldProvider(map); 224 | 225 | Validator longerThan10 = 226 | predicate( 227 | s -> s.length() > 10, s -> new Fail("must be longer than 10")); 228 | 229 | Adaptor adaptor = fieldProvider.getAdaptor(FieldFail::new); 230 | 231 | Validation, FieldFail> nested2 = focus( 232 | adaptor, "nested", "nested2"); 233 | 234 | Validation validation = 235 | Adaptor.safeGet( 236 | nested2, "test", 237 | compose(mandatory(), safeCast(String.class), longerThan10)); 238 | 239 | Assert.assertEquals( 240 | new Failure( 241 | new FieldFail("nested", 242 | new FieldFail( 243 | "nested2", new FieldFail("test", "must be longer than 10")))), 244 | validation); 245 | } 246 | 247 | @Test 248 | public void testFieldProviderWhenDeepNested() { 249 | HashMap map = new HashMap<>(); 250 | 251 | HashMap nestedMap2 = new HashMap<>(); 252 | 253 | nestedMap2.put("test", "stringlongerthan10"); 254 | 255 | HashMap nestedMap = new HashMap<>(); 256 | 257 | nestedMap.put("nested2", nestedMap2); 258 | 259 | map.put("nested", nestedMap); 260 | 261 | FieldProvider fieldProvider = new MapFieldProvider(map); 262 | 263 | Validator longerThan10 = 264 | predicate( 265 | s -> s.length() > 10, s -> new Fail("must be longer than 10")); 266 | 267 | Adaptor adaptor = fieldProvider.getAdaptor(FieldFail::new); 268 | 269 | Validation, FieldFail> nested = focus( 270 | adaptor, "nested", "nested2"); 271 | 272 | Validation validation = 273 | Adaptor.safeGet( 274 | nested, "test", 275 | compose(mandatory(), safeCast(String.class), longerThan10)); 276 | 277 | Assert.assertEquals(just("stringlongerthan10"), validation); 278 | } 279 | 280 | @Test 281 | public void testFailureFieldProviderWhenProgressiveNesting() { 282 | HashMap map = new HashMap<>(); 283 | 284 | HashMap nestedMap2 = new HashMap<>(); 285 | 286 | nestedMap2.put("test", "test"); 287 | 288 | HashMap nestedMap = new HashMap<>(); 289 | 290 | nestedMap.put("nested2", nestedMap2); 291 | 292 | map.put("nested", nestedMap); 293 | 294 | FieldProvider fieldProvider = new MapFieldProvider(map); 295 | 296 | Validator longerThan10 = 297 | predicate( 298 | s -> s.length() > 10, s -> new Fail("must be longer than 10")); 299 | 300 | Adaptor adaptor = fieldProvider.getAdaptor(FieldFail::new); 301 | 302 | Validation, FieldFail> nested = focus( 303 | adaptor, "nested"); 304 | 305 | Validation, FieldFail> nested2 = focus( 306 | adaptor, "nested", "nested2"); 307 | 308 | Validation validation = Adaptor.safeGet( 309 | nested, "test", 310 | compose(mandatory(), safeCast(String.class), longerThan10)); 311 | 312 | Validation validation2 = Adaptor.safeGet( 313 | nested2, "test", 314 | compose(mandatory(), safeCast(String.class), longerThan10)); 315 | 316 | Assert.assertEquals( 317 | new Failure( 318 | new FieldFail("nested", 319 | new FieldFail( 320 | new HashMap(){{ 321 | put("test", new Fail("should not be empty")); 322 | put("nested2", new FieldFail("test", "must be longer than 10")); 323 | }}))), 324 | apply(String::concat, validation, validation2)); 325 | } 326 | 327 | 328 | } 329 | -------------------------------------------------------------------------------- /src/test/java/com/liferay/functional/validation/ValidationTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2000-present Liferay, Inc. All rights reserved. 3 | *

4 | * This library is free software; you can redistribute it and/or modify it under 5 | * the terms of the GNU Lesser General Public License as published by the Free 6 | * Software Foundation; either version 2.1 of the License, or (at your option) 7 | * any later version. 8 | *

9 | * This library is distributed in the hope that it will be useful, but WITHOUT 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 12 | * details. 13 | */ 14 | 15 | package com.liferay.functional.validation; 16 | 17 | import com.liferay.functional.validation.Validation.Failure; 18 | import com.liferay.functional.validation.Validation.Success; 19 | import org.junit.Test; 20 | 21 | import java.util.Arrays; 22 | import java.util.Collections; 23 | import java.util.HashSet; 24 | import java.util.stream.Collectors; 25 | 26 | import static com.liferay.functional.validation.Validation.apply; 27 | import static org.junit.Assert.assertEquals; 28 | import static org.junit.Assert.assertFalse; 29 | import static org.junit.Assert.assertTrue; 30 | import static org.junit.Assert.fail; 31 | 32 | /** 33 | * @author Carlos Sierra Andrés 34 | */ 35 | public class ValidationTest { 36 | 37 | @Test 38 | public void testSuccess() { 39 | Validation validation = new Success<>("test"); 40 | 41 | assertTrue(validation.isSuccess()); 42 | 43 | try { 44 | validation.failures(); 45 | 46 | fail("Invocation of failures() in Success must throw " + 47 | "IllegalStateException"); 48 | } 49 | catch (IllegalStateException ise) { 50 | } 51 | 52 | assertEquals("test", validation.get()); 53 | 54 | assertEquals("test", validation.mapFailures(x -> x).get()); 55 | 56 | assertEquals("test2", validation.map(s -> s + "2").get()); 57 | 58 | assertEquals( 59 | "test-other", 60 | validation.flatMap(s -> new Success<>(s + "-other")).get()); 61 | 62 | assertEquals( 63 | new Success<>("test-other2"), 64 | apply( 65 | String::concat, 66 | new Success("test"), 67 | new Success<>("-other2"))); 68 | } 69 | 70 | @Test 71 | public void testFaiure() { 72 | Validation validation = new Failure<>( 73 | new Fail(Collections.singleton("error"))); 74 | 75 | assertFalse(validation.isSuccess()); 76 | 77 | assertEquals( 78 | new Fail(Collections.singleton("error")), validation.failures()); 79 | 80 | try { 81 | validation.get(); 82 | 83 | fail("Invocation of get on Failure must throw " + 84 | "IllegalStateException"); 85 | } 86 | catch (IllegalStateException ise) { 87 | } 88 | 89 | assertEquals(validation, validation.map(x -> x + "append")); 90 | 91 | Failure failure = new Failure<>( 92 | new Fail(Collections.singleton("otherError"))); 93 | 94 | assertEquals(validation, validation.flatMap(x -> failure)); 95 | 96 | assertEquals( 97 | new Failure<>( 98 | new Fail(new HashSet<>(Arrays.asList("error", "otherError")))), 99 | apply(String::concat, validation, failure)); 100 | 101 | assertEquals( 102 | new Failure<>( 103 | new Fail( 104 | new HashSet<>(Arrays.asList("error2", "otherError2")))), 105 | apply(String::concat, validation, failure).mapFailures( 106 | f -> new Fail( 107 | f._failures.stream().map(s -> s + "2").collect( 108 | Collectors.toSet())))); 109 | } 110 | 111 | @Test 112 | public void testApply() { 113 | assertEquals( 114 | new Success<>(new Data("test", 1)), 115 | apply( 116 | Data::new, 117 | new Success("test"), new Success<>(1))); 118 | 119 | assertEquals( 120 | new Failure<>(new Fail("error")), 121 | apply( 122 | Data::new, new Success<>("test"), 123 | new Failure<>(new Fail("error")))); 124 | 125 | assertEquals( 126 | new Failure<>(new Fail("error")), 127 | apply( 128 | Data::new, new Failure<>(new Fail("error")), 129 | new Success<>(10))); 130 | 131 | assertEquals( 132 | new Failure<>(new Fail("error", "error2")), 133 | apply( 134 | Data::new, new Failure<>(new Fail("error")), 135 | new Failure<>(new Fail("error2")))); 136 | } 137 | 138 | class Data { 139 | private final String a; 140 | private final Integer b; 141 | 142 | public Data(String a, Integer b) { 143 | this.a = a; 144 | this.b = b; 145 | } 146 | 147 | @Override 148 | public boolean equals(Object o) { 149 | if (this == o) return true; 150 | if (o == null || getClass() != o.getClass()) return false; 151 | 152 | Data data = (Data) o; 153 | 154 | if (!a.equals(data.a)) return false; 155 | return b.equals(data.b); 156 | } 157 | 158 | @Override 159 | public int hashCode() { 160 | int result = a.hashCode(); 161 | result = 31 * result + b.hashCode(); 162 | return result; 163 | } 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /src/test/java/com/liferay/functional/validation/ValidatorTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2000-present Liferay, Inc. All rights reserved. 3 | *

4 | * This library is free software; you can redistribute it and/or modify it under 5 | * the terms of the GNU Lesser General Public License as published by the Free 6 | * Software Foundation; either version 2.1 of the License, or (at your option) 7 | * any later version. 8 | *

9 | * This library is distributed in the hope that it will be useful, but WITHOUT 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 12 | * details. 13 | */ 14 | 15 | package com.liferay.functional.validation; 16 | 17 | import com.liferay.functional.validation.Validation.Failure; 18 | import com.liferay.functional.validation.Validation.Success; 19 | import org.junit.Test; 20 | 21 | import java.util.function.Function; 22 | 23 | import static com.liferay.functional.validation.Validator.partials; 24 | import static com.liferay.functional.validation.Validator.predicate; 25 | import static com.liferay.functional.validation.ValidatorTest.Data.adapt; 26 | import static org.junit.Assert.assertEquals; 27 | 28 | /** 29 | * @author Carlos Sierra Andrés 30 | */ 31 | public class ValidatorTest { 32 | 33 | @Test 34 | public void testValidate() { 35 | Validator stringValidator = predicate( 36 | (String s) -> s.length() > 5, 37 | s -> new Fail("length should be longer than 5")); 38 | 39 | assertEquals( 40 | new Failure<>(new Fail("length should be longer than 5")), 41 | stringValidator.validate("hello")); 42 | 43 | assertEquals( 44 | new Success<>("hello world"), 45 | stringValidator.validate("hello world")); 46 | 47 | assertEquals( 48 | new Failure<>(new Fail("length should be longer than 5")), 49 | stringValidator.validate("hello").map(String::length)); 50 | 51 | assertEquals( 52 | new Success<>(11), 53 | stringValidator.validate("hello world").map(String::length)); 54 | 55 | Validator intValidator = predicate( 56 | (Integer i) -> i > 5, 57 | i -> new Fail("number should be greater than 5")); 58 | 59 | assertEquals( 60 | new Success<>(11), 61 | stringValidator.map(String::length).compose( 62 | intValidator).validate("hello world")); 63 | } 64 | 65 | @Test 66 | public void testAdaptAndPartial() { 67 | Validator notBlankValidator = predicate( 68 | (String s) -> !s.isEmpty(), s -> new Fail("should not be empty")); 69 | 70 | Validator positiveValidator = predicate( 71 | (Integer i) -> i > 0, i -> new Fail("should be positive")); 72 | 73 | Validator dataValidator = 74 | partials( 75 | adapt(d -> d.name, "name: ", notBlankValidator), 76 | adapt(d -> d.number, "number: ", positiveValidator) 77 | ); 78 | 79 | assertEquals( 80 | new Failure<>( 81 | new Fail( 82 | "name: should not be empty", 83 | "number: should be positive")), 84 | dataValidator.validate(new Data("", -1))); 85 | 86 | assertEquals( 87 | new Success(new Data("hello", 1)), 88 | dataValidator.validate(new Data("hello", 1))); 89 | } 90 | 91 | static class Data { 92 | private String name; 93 | private Integer number; 94 | 95 | public Data(String name, Integer number) { 96 | this.number = number; 97 | this.name = name; 98 | } 99 | 100 | @Override 101 | public boolean equals(Object o) { 102 | if (this == o) return true; 103 | if (o == null || getClass() != o.getClass()) return false; 104 | 105 | Data data = (Data) o; 106 | 107 | return name.equals(data.name) && number.equals(data.number); 108 | 109 | } 110 | 111 | @Override 112 | public int hashCode() { 113 | int result = name.hashCode(); 114 | result = 31 * result + number.hashCode(); 115 | return result; 116 | } 117 | 118 | static Validator adapt( 119 | Function adaptor, String prefix, 120 | Validator validator) { 121 | 122 | return (Validator)validator.adapt( 123 | adaptor, (Fail f) -> f.prependAll(prefix)); 124 | } 125 | } 126 | 127 | } 128 | --------------------------------------------------------------------------------