├── .gitignore ├── ASL-2.0.txt ├── LGPL-3.0.txt ├── LICENSE ├── README.md ├── RELEASE-NOTES.md ├── build.gradle ├── dorelease.sh ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src ├── main ├── java │ └── com │ │ └── github │ │ └── fge │ │ └── lambdas │ │ ├── Chainer.java │ │ ├── Throwing.java │ │ ├── ThrownByLambdaException.java │ │ ├── comparators │ │ ├── ComparatorChainer.java │ │ └── ThrowingComparator.java │ │ ├── consumers │ │ ├── BiConsumerChainer.java │ │ ├── ConsumerChainer.java │ │ ├── DoubleConsumerChainer.java │ │ ├── IntConsumerChainer.java │ │ ├── LongConsumerChainer.java │ │ ├── ObjDoubleConsumerChainer.java │ │ ├── ObjIntConsumerChainer.java │ │ ├── ObjLongConsumerChainer.java │ │ ├── ThrowingBiConsumer.java │ │ ├── ThrowingConsumer.java │ │ ├── ThrowingDoubleConsumer.java │ │ ├── ThrowingIntConsumer.java │ │ ├── ThrowingLongConsumer.java │ │ ├── ThrowingObjDoubleConsumer.java │ │ ├── ThrowingObjIntConsumer.java │ │ └── ThrowingObjLongConsumer.java │ │ ├── functions │ │ ├── BiFunctionChainer.java │ │ ├── FunctionChainer.java │ │ ├── ThrowingBiFunction.java │ │ ├── ThrowingFunction.java │ │ ├── ThrowingToDoubleFunction.java │ │ ├── ThrowingToIntFunction.java │ │ ├── ThrowingToLongFunction.java │ │ ├── ToDoubleFunctionChainer.java │ │ ├── ToIntFunctionChainer.java │ │ ├── ToLongFunctionChainer.java │ │ ├── doublefunctions │ │ │ ├── DoubleFunctionChainer.java │ │ │ ├── DoubleToIntFunctionChainer.java │ │ │ ├── DoubleToLongFunctionChainer.java │ │ │ ├── ThrowingDoubleFunction.java │ │ │ ├── ThrowingDoubleToIntFunction.java │ │ │ └── ThrowingDoubleToLongFunction.java │ │ ├── intfunctions │ │ │ ├── IntFunctionChainer.java │ │ │ ├── IntToDoubleFunctionChainer.java │ │ │ ├── IntToLongFunctionChainer.java │ │ │ ├── ThrowingIntFunction.java │ │ │ ├── ThrowingIntToDoubleFunction.java │ │ │ └── ThrowingIntToLongFunction.java │ │ ├── longfunctions │ │ │ ├── LongFunctionChainer.java │ │ │ ├── LongToDoubleFunctionChainer.java │ │ │ ├── LongToIntFunctionChainer.java │ │ │ ├── ThrowingLongFunction.java │ │ │ ├── ThrowingLongToDoubleFunction.java │ │ │ └── ThrowingLongToIntFunction.java │ │ └── operators │ │ │ ├── BinaryOperatorChainer.java │ │ │ ├── DoubleBinaryOperatorChainer.java │ │ │ ├── DoubleUnaryOperatorChainer.java │ │ │ ├── IntBinaryOperatorChainer.java │ │ │ ├── IntUnaryOperatorChainer.java │ │ │ ├── LongBinaryOperatorChainer.java │ │ │ ├── LongUnaryOperatorChainer.java │ │ │ ├── ThrowingBinaryOperator.java │ │ │ ├── ThrowingDoubleBinaryOperator.java │ │ │ ├── ThrowingDoubleUnaryOperator.java │ │ │ ├── ThrowingIntBinaryOperator.java │ │ │ ├── ThrowingIntUnaryOperator.java │ │ │ ├── ThrowingLongBinaryOperator.java │ │ │ ├── ThrowingLongUnaryOperator.java │ │ │ ├── ThrowingUnaryOperator.java │ │ │ └── UnaryOperatorChainer.java │ │ ├── predicates │ │ ├── DoublePredicateChainer.java │ │ ├── IntPredicateChainer.java │ │ ├── LongPredicateChainer.java │ │ ├── PredicateChainer.java │ │ ├── ThrowingDoublePredicate.java │ │ ├── ThrowingIntPredicate.java │ │ ├── ThrowingLongPredicate.java │ │ └── ThrowingPredicate.java │ │ ├── runnable │ │ ├── RunnableChainer.java │ │ └── ThrowingRunnable.java │ │ └── supplier │ │ ├── DoubleSupplierChainer.java │ │ ├── IntSupplierChainer.java │ │ ├── LongSupplierChainer.java │ │ ├── SupplierChainer.java │ │ ├── ThrowingDoubleSupplier.java │ │ ├── ThrowingIntSupplier.java │ │ ├── ThrowingLongSupplier.java │ │ └── ThrowingSupplier.java └── resources │ └── META-INF │ ├── ASL-2.0.txt │ ├── LGPL-3.0.txt │ └── LICENSE └── test └── java └── com └── github └── fge └── lambdas ├── ChainerRethrowTest.java ├── ChainerTest.java ├── comparators └── ComparatorChainerTest.java ├── consumers ├── BiConsumerChainerTest.java ├── ConsumerChainerTest.java ├── DoubleConsumerChainerTest.java ├── IntConsumerChainerTest.java ├── LongConsumerChainerTest.java ├── ObjDoubleConsumerChainerTest.java ├── ObjIntConsumerChainerTest.java └── ObjLongConsumerChainerTest.java ├── functions ├── BiFunctionChainerTest.java ├── FunctionChainerTest.java ├── ToDoubleFunctionChainerTest.java ├── ToIntFunctionChainerTest.java ├── ToLongFunctionChainerTest.java ├── doublefunctions │ ├── DoubleFunctionChainerTest.java │ ├── DoubleToIntFunctionChainerTest.java │ └── DoubleToLongFunctionChainerTest.java ├── intfunctions │ ├── IntFunctionChainerTest.java │ ├── IntToDoubleFunctionChainerTest.java │ └── IntToLongFunctionChainerTest.java ├── longfunctions │ ├── LongFunctionChainerTest.java │ ├── LongToDoubleFunctionChainerTest.java │ └── LongToIntFunctionChainerTest.java └── operators │ ├── BinaryOperatorChainerTest.java │ ├── DoubleBinaryOperatorChainerTest.java │ ├── DoubleUnaryOperatorChainerTest.java │ ├── IntBinaryOperatorChainerTest.java │ ├── IntUnaryOperatorChainerTest.java │ ├── LongBinaryOperatorChainerTest.java │ ├── LongUnaryOperatorChainerTest.java │ └── UnaryOperatorChainerTest.java ├── helpers ├── CustomAssertions.java ├── MyException.java ├── Type1.java ├── Type2.java └── Type3.java ├── predicates ├── DoublePredicateChainerTest.java ├── IntPredicateChainerTest.java ├── LongPredicateChainerTest.java └── PredicateChainerTest.java ├── runnable └── RunnableChainerTest.java └── supplier ├── DoubleSupplierChainerTest.java ├── IntSupplierChainerTest.java ├── LongSupplierChainerTest.java └── SupplierChainerTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | target 3 | .idea 4 | build 5 | .gradle 6 | out 7 | classes 8 | 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This software is dual-licensed under: 2 | 3 | - the Lesser General Public License (LGPL) version 3.0 or, at your option, any 4 | later version; 5 | - the Apache Software License (ASL) version 2.0. 6 | 7 | The text of both licenses is included (under the names LGPL-3.0.txt and 8 | ASL-2.0.txt respectively). 9 | 10 | Direct link to the sources: 11 | 12 | - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt 13 | - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt 14 | 15 | -------------------------------------------------------------------------------- /RELEASE-NOTES.md: -------------------------------------------------------------------------------- 1 | ## 0.5.0 2 | 3 | * Implement sneaky throws. 4 | * Add ThrowingRunnable. 5 | 6 | ## 0.4.0 7 | 8 | * Refactor to use an abstract class as the base. 9 | * ThrowingFunctionalInterface has disappeared: it is now up to the abstract 10 | class to chain. 11 | * Introduce Throwing.*() to replace Functions, Comparators etc etc. 12 | 13 | ## 0.3.0 14 | 15 | * Add new .tryWith() methods. 16 | * Add new ThrowingFunctionalInterface interface; make all interfaces extend it. 17 | All interfaces now inherit .orTryWith(), .orThrow() and .fallbackTo() from it. 18 | * Javadoc. Incomplete. 19 | * Rewrite all tests. 20 | * Fix ThrowablesFactory exception handling. 21 | 22 | ## 0.2.0 23 | 24 | * Add new .wrap() methods. 25 | * Add .orThrow() methods to Throwing* interfaces. 26 | * Add .or{Return*,DoNothing}() methods to Throwing* interfaces. 27 | 28 | ## 0.1.0 29 | 30 | * First version 31 | -------------------------------------------------------------------------------- /dorelease.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 4 | # This will build everything that is needed and push to Maven central. 5 | # 6 | 7 | ./gradlew --recompile-scripts clean test uploadArchives 8 | 9 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fge/throwing-lambdas/9a6b47df7a5f0de27bdb13b9e89b07a024d95439/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Mar 05 16:40:11 CET 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=http\://services.gradle.org/distributions/gradle-2.3-all.zip 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/ThrownByLambdaException.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas; 2 | 3 | /** 4 | * Default exception thrown when a lambda throws a checked exception 5 | */ 6 | public final class ThrownByLambdaException 7 | extends RuntimeException 8 | { 9 | public ThrownByLambdaException(final Throwable cause) 10 | { 11 | super(cause); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/comparators/ComparatorChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.comparators; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.Comparator; 6 | 7 | public class ComparatorChainer 8 | extends Chainer, ThrowingComparator, ComparatorChainer> 9 | implements ThrowingComparator 10 | { 11 | public ComparatorChainer(final ThrowingComparator throwing) 12 | { 13 | super(throwing); 14 | } 15 | 16 | @Override 17 | public int doCompare(final T o1, final T o2) 18 | throws Throwable 19 | { 20 | return throwing.doCompare(o1, o2); 21 | } 22 | 23 | @Override 24 | public ComparatorChainer orTryWith(final ThrowingComparator other) 25 | { 26 | final ThrowingComparator comparator = (o1, o2) -> { 27 | try { 28 | return throwing.doCompare(o1, o2); 29 | } catch (Error | RuntimeException e) { 30 | throw e; 31 | } catch (Throwable throwable) { 32 | return other.doCompare(o1, o2); 33 | } 34 | }; 35 | 36 | return new ComparatorChainer<>(comparator); 37 | } 38 | 39 | @Override 40 | public ThrowingComparator orThrow( 41 | final Class exclass) 42 | { 43 | return (o1, o2) -> { 44 | try { 45 | return throwing.doCompare(o1, o2); 46 | } catch (Error | RuntimeException e) { 47 | throw e; 48 | } catch (Throwable throwable) { 49 | throw rethrow(exclass, throwable); 50 | } 51 | }; 52 | } 53 | 54 | @Override 55 | public Comparator fallbackTo(final Comparator fallback) 56 | { 57 | return (o1, o2) -> { 58 | try { 59 | return throwing.doCompare(o1, o2); 60 | } catch (Error | RuntimeException e) { 61 | throw e; 62 | } catch (Throwable ignored) { 63 | return fallback.compare(o1, o2); 64 | } 65 | }; 66 | } 67 | 68 | @Override 69 | public Comparator sneakyThrow() 70 | { 71 | return (o1, o2) -> { 72 | try { 73 | return throwing.doCompare(o1, o2); 74 | } catch (Error | RuntimeException e) { 75 | throw e; 76 | } catch (Throwable throwable) { 77 | throw doSneakyThrow(throwable); 78 | } 79 | }; 80 | } 81 | 82 | public Comparator orReturn(final int retval) 83 | { 84 | return (o1, o2) -> { 85 | try { 86 | return throwing.doCompare(o1, o2); 87 | } catch (Error | RuntimeException e) { 88 | throw e; 89 | } catch (Throwable ignored) { 90 | return retval; 91 | } 92 | }; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/comparators/ThrowingComparator.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.comparators; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.Comparator; 6 | 7 | /** 8 | * A throwing {@link Comparator} 9 | * 10 | * @param type parameter of the argument 11 | */ 12 | @FunctionalInterface 13 | public interface ThrowingComparator 14 | extends Comparator 15 | { 16 | int doCompare(T o1, T o2) 17 | throws Throwable; 18 | 19 | @Override 20 | default int compare(T o1, T o2) 21 | { 22 | try { 23 | return doCompare(o1, o2); 24 | } catch (Error | RuntimeException e) { 25 | throw e; 26 | } catch (Throwable throwable) { 27 | throw new ThrownByLambdaException(throwable); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/consumers/BiConsumerChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.BiConsumer; 6 | 7 | public class BiConsumerChainer 8 | extends Chainer, ThrowingBiConsumer, BiConsumerChainer> 9 | implements ThrowingBiConsumer 10 | { 11 | public BiConsumerChainer(final ThrowingBiConsumer throwing) 12 | { 13 | super(throwing); 14 | } 15 | 16 | @Override 17 | public void doAccept(final T t, final U u) 18 | throws Throwable 19 | { 20 | throwing.doAccept(t, u); 21 | } 22 | 23 | @Override 24 | public BiConsumerChainer orTryWith(final ThrowingBiConsumer other) 25 | { 26 | final ThrowingBiConsumer consumer = (t, u) -> { 27 | try { 28 | throwing.doAccept(t, u); 29 | } catch (Error | RuntimeException e) { 30 | throw e; 31 | } catch (Throwable ignored) { 32 | other.doAccept(t, u); 33 | } 34 | }; 35 | 36 | return new BiConsumerChainer<>(consumer); 37 | } 38 | 39 | @Override 40 | public ThrowingBiConsumer orThrow( 41 | final Class exclass) 42 | { 43 | return (t, u) -> { 44 | try { 45 | throwing.doAccept(t, u); 46 | } catch (Error | RuntimeException e) { 47 | throw e; 48 | } catch (Throwable throwable) { 49 | throw rethrow(exclass, throwable); 50 | } 51 | }; 52 | } 53 | 54 | @Override 55 | public BiConsumer fallbackTo(final BiConsumer fallback) 56 | { 57 | return (t, u) -> { 58 | try { 59 | throwing.doAccept(t, u); 60 | } catch (Error | RuntimeException e) { 61 | throw e; 62 | } catch (Throwable ignored) { 63 | fallback.accept(t, u); 64 | } 65 | }; 66 | } 67 | 68 | @Override 69 | public BiConsumer sneakyThrow() 70 | { 71 | return (t, u) -> { 72 | try { 73 | throwing.doAccept(t, u); 74 | } catch (Error | RuntimeException e) { 75 | throw e; 76 | } catch (Throwable throwable) { 77 | throw doSneakyThrow(throwable); 78 | } 79 | }; 80 | } 81 | 82 | public BiConsumer orDoNothing() 83 | { 84 | return (t, u) -> { 85 | try { 86 | throwing.doAccept(t, u); 87 | } catch (Error | RuntimeException e) { 88 | throw e; 89 | } catch (Throwable ignored) { 90 | // nothing 91 | } 92 | }; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/consumers/ConsumerChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.Consumer; 6 | 7 | public class ConsumerChainer 8 | extends Chainer, ThrowingConsumer, ConsumerChainer> 9 | implements ThrowingConsumer 10 | { 11 | public ConsumerChainer(final ThrowingConsumer throwing) 12 | { 13 | super(throwing); 14 | } 15 | 16 | @Override 17 | public void doAccept(final T t) 18 | throws Throwable 19 | { 20 | throwing.doAccept(t); 21 | } 22 | 23 | @Override 24 | public ConsumerChainer orTryWith(final ThrowingConsumer other) 25 | { 26 | final ThrowingConsumer consumer = t -> { 27 | try { 28 | throwing.doAccept(t); 29 | } catch (Error | RuntimeException e) { 30 | throw e; 31 | } catch (Throwable ignored) { 32 | other.doAccept(t); 33 | } 34 | }; 35 | 36 | return new ConsumerChainer<>(consumer); 37 | } 38 | 39 | @Override 40 | public ThrowingConsumer orThrow( 41 | final Class exclass) 42 | { 43 | return t -> { 44 | try { 45 | throwing.doAccept(t); 46 | } catch (Error | RuntimeException e) { 47 | throw e; 48 | } catch (Throwable throwable) { 49 | throw rethrow(exclass, throwable); 50 | } 51 | }; 52 | } 53 | 54 | @Override 55 | public Consumer fallbackTo(final Consumer fallback) 56 | { 57 | return t -> { 58 | try { 59 | throwing.doAccept(t); 60 | } catch (Error | RuntimeException e) { 61 | throw e; 62 | } catch (Throwable ignored) { 63 | fallback.accept(t); 64 | } 65 | }; 66 | } 67 | 68 | @Override 69 | public Consumer sneakyThrow() 70 | { 71 | return t -> { 72 | try { 73 | throwing.doAccept(t); 74 | } catch (Error | RuntimeException e) { 75 | throw e; 76 | } catch (Throwable throwable) { 77 | throw doSneakyThrow(throwable); 78 | } 79 | }; 80 | } 81 | 82 | public Consumer orDoNothing() 83 | { 84 | return t -> { 85 | try { 86 | throwing.doAccept(t); 87 | } catch (Error | RuntimeException e) { 88 | throw e; 89 | } catch (Throwable ignored) { 90 | // nothing 91 | } 92 | }; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/consumers/DoubleConsumerChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.DoubleConsumer; 6 | 7 | public class DoubleConsumerChainer 8 | extends Chainer 9 | implements ThrowingDoubleConsumer 10 | { 11 | public DoubleConsumerChainer(final ThrowingDoubleConsumer throwing) 12 | { 13 | super(throwing); 14 | } 15 | 16 | @Override 17 | public void doAccept(final double value) 18 | throws Throwable 19 | { 20 | throwing.doAccept(value); 21 | } 22 | 23 | @Override 24 | public DoubleConsumerChainer orTryWith( 25 | final ThrowingDoubleConsumer other) 26 | { 27 | final ThrowingDoubleConsumer consumer = value -> { 28 | try { 29 | throwing.doAccept(value); 30 | } catch (Error | RuntimeException e) { 31 | throw e; 32 | } catch (Throwable ignored) { 33 | other.doAccept(value); 34 | } 35 | }; 36 | 37 | return new DoubleConsumerChainer(consumer); 38 | } 39 | 40 | @Override 41 | public ThrowingDoubleConsumer orThrow( 42 | final Class exclass) 43 | { 44 | return value -> { 45 | try { 46 | throwing.doAccept(value); 47 | } catch (Error | RuntimeException e) { 48 | throw e; 49 | } catch (Throwable throwable) { 50 | throw rethrow(exclass, throwable); 51 | } 52 | }; 53 | } 54 | 55 | @Override 56 | public DoubleConsumer fallbackTo(final DoubleConsumer fallback) 57 | { 58 | return value -> { 59 | try { 60 | throwing.doAccept(value); 61 | } catch (Error | RuntimeException e) { 62 | throw e; 63 | } catch (Throwable ignored) { 64 | fallback.accept(value); 65 | } 66 | }; 67 | } 68 | 69 | @Override 70 | public DoubleConsumer sneakyThrow() 71 | { 72 | return value -> { 73 | try { 74 | throwing.doAccept(value); 75 | } catch (Error | RuntimeException e) { 76 | throw e; 77 | } catch (Throwable throwable) { 78 | throw doSneakyThrow(throwable); 79 | } 80 | }; 81 | } 82 | 83 | public DoubleConsumer orDoNothing() 84 | { 85 | return value -> { 86 | try { 87 | throwing.doAccept(value); 88 | } catch (Error | RuntimeException e) { 89 | throw e; 90 | } catch (Throwable ignored) { 91 | // nothing 92 | } 93 | }; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/consumers/IntConsumerChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.IntConsumer; 6 | 7 | public class IntConsumerChainer 8 | extends Chainer 9 | implements ThrowingIntConsumer 10 | { 11 | public IntConsumerChainer(final ThrowingIntConsumer throwing) 12 | { 13 | super(throwing); 14 | } 15 | 16 | @Override 17 | public void doAccept(final int value) 18 | throws Throwable 19 | { 20 | throwing.doAccept(value); 21 | } 22 | 23 | @Override 24 | public IntConsumerChainer orTryWith( 25 | final ThrowingIntConsumer other) 26 | { 27 | final ThrowingIntConsumer consumer = value -> { 28 | try { 29 | throwing.doAccept(value); 30 | } catch (Error | RuntimeException e) { 31 | throw e; 32 | } catch (Throwable ignored) { 33 | other.doAccept(value); 34 | } 35 | }; 36 | 37 | return new IntConsumerChainer(consumer); 38 | } 39 | 40 | @Override 41 | public ThrowingIntConsumer orThrow( 42 | final Class exclass) 43 | { 44 | return value -> { 45 | try { 46 | throwing.doAccept(value); 47 | } catch (Error | RuntimeException e) { 48 | throw e; 49 | } catch (Throwable throwable) { 50 | throw rethrow(exclass, throwable); 51 | } 52 | }; 53 | } 54 | 55 | @Override 56 | public IntConsumer fallbackTo(final IntConsumer fallback) 57 | { 58 | return value -> { 59 | try { 60 | throwing.doAccept(value); 61 | } catch (Error | RuntimeException e) { 62 | throw e; 63 | } catch (Throwable ignored) { 64 | fallback.accept(value); 65 | } 66 | }; 67 | } 68 | 69 | @Override 70 | public IntConsumer sneakyThrow() 71 | { 72 | return value -> { 73 | try { 74 | throwing.doAccept(value); 75 | } catch (Error | RuntimeException e) { 76 | throw e; 77 | } catch (Throwable throwable) { 78 | throw doSneakyThrow(throwable); 79 | } 80 | }; 81 | } 82 | 83 | public IntConsumer orDoNothing() 84 | { 85 | return value -> { 86 | try { 87 | throwing.doAccept(value); 88 | } catch (Error | RuntimeException e) { 89 | throw e; 90 | } catch (Throwable ignored) { 91 | // nothing 92 | } 93 | }; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/consumers/LongConsumerChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.LongConsumer; 6 | 7 | public class LongConsumerChainer 8 | extends Chainer 9 | implements ThrowingLongConsumer 10 | { 11 | public LongConsumerChainer(final ThrowingLongConsumer throwing) 12 | { 13 | super(throwing); 14 | } 15 | 16 | @Override 17 | public void doAccept(final long value) 18 | throws Throwable 19 | { 20 | throwing.doAccept(value); 21 | } 22 | 23 | @Override 24 | public LongConsumerChainer orTryWith( 25 | final ThrowingLongConsumer other) 26 | { 27 | final ThrowingLongConsumer consumer = value -> { 28 | try { 29 | throwing.doAccept(value); 30 | } catch (Error | RuntimeException e) { 31 | throw e; 32 | } catch (Throwable ignored) { 33 | other.doAccept(value); 34 | } 35 | }; 36 | 37 | return new LongConsumerChainer(consumer); 38 | } 39 | 40 | @Override 41 | public ThrowingLongConsumer orThrow( 42 | final Class exclass) 43 | { 44 | return value -> { 45 | try { 46 | throwing.doAccept(value); 47 | } catch (Error | RuntimeException e) { 48 | throw e; 49 | } catch (Throwable throwable) { 50 | throw rethrow(exclass, throwable); 51 | } 52 | }; 53 | } 54 | 55 | @Override 56 | public LongConsumer fallbackTo(final LongConsumer fallback) 57 | { 58 | return value -> { 59 | try { 60 | throwing.doAccept(value); 61 | } catch (Error | RuntimeException e) { 62 | throw e; 63 | } catch (Throwable ignored) { 64 | fallback.accept(value); 65 | } 66 | }; 67 | } 68 | 69 | @Override 70 | public LongConsumer sneakyThrow() 71 | { 72 | return value -> { 73 | try { 74 | throwing.doAccept(value); 75 | } catch (Error | RuntimeException e) { 76 | throw e; 77 | } catch (Throwable throwable) { 78 | throw doSneakyThrow(throwable); 79 | } 80 | }; 81 | } 82 | 83 | public LongConsumer orDoNothing() 84 | { 85 | return value -> { 86 | try { 87 | throwing.doAccept(value); 88 | } catch (Error | RuntimeException e) { 89 | throw e; 90 | } catch (Throwable ignored) { 91 | // nothing 92 | } 93 | }; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/consumers/ObjDoubleConsumerChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.ObjDoubleConsumer; 6 | 7 | public class ObjDoubleConsumerChainer 8 | extends Chainer, ThrowingObjDoubleConsumer, ObjDoubleConsumerChainer> 9 | implements ThrowingObjDoubleConsumer 10 | { 11 | public ObjDoubleConsumerChainer( 12 | final ThrowingObjDoubleConsumer throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public void doAccept(final T t, final double value) 19 | throws Throwable 20 | { 21 | throwing.doAccept(t, value); 22 | } 23 | 24 | @Override 25 | public ObjDoubleConsumerChainer orTryWith( 26 | final ThrowingObjDoubleConsumer other) 27 | { 28 | final ThrowingObjDoubleConsumer consumer = (t, value) -> { 29 | try { 30 | throwing.doAccept(t, value); 31 | } catch (Error | RuntimeException e) { 32 | throw e; 33 | } catch (Throwable ignored) { 34 | other.doAccept(t, value); 35 | } 36 | }; 37 | 38 | return new ObjDoubleConsumerChainer<>(consumer); 39 | } 40 | 41 | @Override 42 | public ThrowingObjDoubleConsumer orThrow( 43 | final Class exclass) 44 | { 45 | return (t, value) -> { 46 | try { 47 | throwing.doAccept(t, value); 48 | } catch (Error | RuntimeException e) { 49 | throw e; 50 | } catch (Throwable throwable) { 51 | throw rethrow(exclass, throwable); 52 | } 53 | }; 54 | } 55 | 56 | @Override 57 | public ObjDoubleConsumer fallbackTo(final ObjDoubleConsumer fallback) 58 | { 59 | return (t, value) -> { 60 | try { 61 | throwing.doAccept(t, value); 62 | } catch (Error | RuntimeException e) { 63 | throw e; 64 | } catch (Throwable ignored) { 65 | fallback.accept(t, value); 66 | } 67 | }; 68 | } 69 | 70 | @Override 71 | public ObjDoubleConsumer sneakyThrow() 72 | { 73 | return (t, value) -> { 74 | try { 75 | throwing.doAccept(t, value); 76 | } catch (Error | RuntimeException e) { 77 | throw e; 78 | } catch (Throwable throwable) { 79 | throw doSneakyThrow(throwable); 80 | } 81 | }; 82 | } 83 | 84 | public ObjDoubleConsumer orDoNothing() 85 | { 86 | return (t, value) -> { 87 | try { 88 | throwing.doAccept(t, value); 89 | } catch (Error | RuntimeException e) { 90 | throw e; 91 | } catch (Throwable ignored) { 92 | // nothing 93 | } 94 | }; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/consumers/ObjIntConsumerChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.ObjIntConsumer; 6 | 7 | public class ObjIntConsumerChainer 8 | extends Chainer, ThrowingObjIntConsumer, ObjIntConsumerChainer> 9 | implements ThrowingObjIntConsumer 10 | { 11 | public ObjIntConsumerChainer( 12 | final ThrowingObjIntConsumer throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public void doAccept(final T t, final int value) 19 | throws Throwable 20 | { 21 | throwing.doAccept(t, value); 22 | } 23 | 24 | @Override 25 | public ObjIntConsumerChainer orTryWith( 26 | final ThrowingObjIntConsumer other) 27 | { 28 | final ThrowingObjIntConsumer consumer = (t, value) -> { 29 | try { 30 | throwing.doAccept(t, value); 31 | } catch (Error | RuntimeException e) { 32 | throw e; 33 | } catch (Throwable ignored) { 34 | other.doAccept(t, value); 35 | } 36 | }; 37 | 38 | return new ObjIntConsumerChainer<>(consumer); 39 | } 40 | 41 | @Override 42 | public ThrowingObjIntConsumer orThrow( 43 | final Class exclass) 44 | { 45 | return (t, value) -> { 46 | try { 47 | throwing.doAccept(t, value); 48 | } catch (Error | RuntimeException e) { 49 | throw e; 50 | } catch (Throwable throwable) { 51 | throw rethrow(exclass, throwable); 52 | } 53 | }; 54 | } 55 | 56 | @Override 57 | public ObjIntConsumer fallbackTo(final ObjIntConsumer fallback) 58 | { 59 | return (t, value) -> { 60 | try { 61 | throwing.doAccept(t, value); 62 | } catch (Error | RuntimeException e) { 63 | throw e; 64 | } catch (Throwable ignored) { 65 | fallback.accept(t, value); 66 | } 67 | }; 68 | } 69 | 70 | @Override 71 | public ObjIntConsumer sneakyThrow() 72 | { 73 | return (t, value) -> { 74 | try { 75 | throwing.doAccept(t, value); 76 | } catch (Error | RuntimeException e) { 77 | throw e; 78 | } catch (Throwable throwable) { 79 | throw doSneakyThrow(throwable); 80 | } 81 | }; 82 | } 83 | 84 | public ObjIntConsumer orDoNothing() 85 | { 86 | return (t, value) -> { 87 | try { 88 | throwing.doAccept(t, value); 89 | } catch (Error | RuntimeException e) { 90 | throw e; 91 | } catch (Throwable ignored) { 92 | // nothing 93 | } 94 | }; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/consumers/ObjLongConsumerChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.ObjLongConsumer; 6 | 7 | public class ObjLongConsumerChainer 8 | extends Chainer, ThrowingObjLongConsumer, ObjLongConsumerChainer> 9 | implements ThrowingObjLongConsumer 10 | { 11 | public ObjLongConsumerChainer( 12 | final ThrowingObjLongConsumer throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public void doAccept(final T t, final long value) 19 | throws Throwable 20 | { 21 | throwing.doAccept(t, value); 22 | } 23 | 24 | @Override 25 | public ObjLongConsumerChainer orTryWith( 26 | final ThrowingObjLongConsumer other) 27 | { 28 | final ThrowingObjLongConsumer consumer = (t, value) -> { 29 | try { 30 | throwing.doAccept(t, value); 31 | } catch (Error | RuntimeException e) { 32 | throw e; 33 | } catch (Throwable ignored) { 34 | other.doAccept(t, value); 35 | } 36 | }; 37 | 38 | return new ObjLongConsumerChainer<>(consumer); 39 | } 40 | 41 | @Override 42 | public ThrowingObjLongConsumer orThrow( 43 | final Class exclass) 44 | { 45 | return (t, value) -> { 46 | try { 47 | throwing.doAccept(t, value); 48 | } catch (Error | RuntimeException e) { 49 | throw e; 50 | } catch (Throwable throwable) { 51 | throw rethrow(exclass, throwable); 52 | } 53 | }; 54 | } 55 | 56 | @Override 57 | public ObjLongConsumer fallbackTo(final ObjLongConsumer fallback) 58 | { 59 | return (t, value) -> { 60 | try { 61 | throwing.doAccept(t, value); 62 | } catch (Error | RuntimeException e) { 63 | throw e; 64 | } catch (Throwable ignored) { 65 | fallback.accept(t, value); 66 | } 67 | }; 68 | } 69 | 70 | @Override 71 | public ObjLongConsumer sneakyThrow() 72 | { 73 | return (t, value) -> { 74 | try { 75 | throwing.doAccept(t, value); 76 | } catch (Error | RuntimeException e) { 77 | throw e; 78 | } catch (Throwable throwable) { 79 | throw doSneakyThrow(throwable); 80 | } 81 | }; 82 | } 83 | 84 | public ObjLongConsumer orDoNothing() 85 | { 86 | return (t, value) -> { 87 | try { 88 | throwing.doAccept(t, value); 89 | } catch (Error | RuntimeException e) { 90 | throw e; 91 | } catch (Throwable ignored) { 92 | // nothing 93 | } 94 | }; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/consumers/ThrowingBiConsumer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.BiConsumer; 6 | 7 | /** 8 | * A throwing {@link BiConsumer} 9 | * 10 | * @param type parameter of the first argument 11 | * @param type parameter of the second argument 12 | */ 13 | @FunctionalInterface 14 | public interface ThrowingBiConsumer 15 | extends BiConsumer 16 | { 17 | void doAccept(T t, U u) 18 | throws Throwable; 19 | 20 | @Override 21 | default void accept(T t, U u) 22 | { 23 | try { 24 | doAccept(t, u); 25 | } catch (Error | RuntimeException e) { 26 | throw e; 27 | } catch (Throwable throwable) { 28 | throw new ThrownByLambdaException(throwable); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/consumers/ThrowingConsumer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.Consumer; 6 | 7 | /** 8 | * A throwing {@link Consumer} 9 | * 10 | * @param type parameter of the argument 11 | */ 12 | @FunctionalInterface 13 | public interface ThrowingConsumer 14 | extends Consumer 15 | { 16 | void doAccept(T t) 17 | throws Throwable; 18 | 19 | default void accept(T t) 20 | { 21 | try { 22 | doAccept(t); 23 | } catch (Error | RuntimeException e) { 24 | throw e; 25 | } catch (Throwable throwable) { 26 | throw new ThrownByLambdaException(throwable); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/consumers/ThrowingDoubleConsumer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.DoubleConsumer; 6 | 7 | /** 8 | * A throwing {@link DoubleConsumer} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingDoubleConsumer 12 | extends DoubleConsumer 13 | { 14 | void doAccept(double value) 15 | throws Throwable; 16 | 17 | @Override 18 | default void accept(double value) 19 | { 20 | try { 21 | doAccept(value); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable throwable) { 25 | throw new ThrownByLambdaException(throwable); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/consumers/ThrowingIntConsumer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.IntConsumer; 6 | 7 | /** 8 | * A throwing {@link IntConsumer} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingIntConsumer 12 | extends IntConsumer 13 | { 14 | void doAccept(int value) 15 | throws Throwable; 16 | 17 | @Override 18 | default void accept(int value) 19 | { 20 | try { 21 | doAccept(value); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable throwable) { 25 | throw new ThrownByLambdaException(throwable); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/consumers/ThrowingLongConsumer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.LongConsumer; 6 | 7 | /** 8 | * A throwing {@link LongConsumer} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingLongConsumer 12 | extends LongConsumer 13 | { 14 | void doAccept(long value) 15 | throws Throwable; 16 | 17 | @Override 18 | default void accept(long value) 19 | { 20 | try { 21 | doAccept(value); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable throwable) { 25 | throw new ThrownByLambdaException(throwable); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/consumers/ThrowingObjDoubleConsumer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.ObjDoubleConsumer; 6 | 7 | /** 8 | * A throwing {@link ObjDoubleConsumer} 9 | * 10 | * @param type parameter of the argument 11 | */ 12 | @FunctionalInterface 13 | public interface ThrowingObjDoubleConsumer 14 | extends ObjDoubleConsumer 15 | { 16 | void doAccept(T t, double value) 17 | throws Throwable; 18 | 19 | @Override 20 | default void accept(T t, double value) 21 | { 22 | try { 23 | doAccept(t, value); 24 | } catch (Error | RuntimeException e) { 25 | throw e; 26 | } catch (Throwable tooBad) { 27 | throw new ThrownByLambdaException(tooBad); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/consumers/ThrowingObjIntConsumer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.ObjDoubleConsumer; 6 | import java.util.function.ObjIntConsumer; 7 | 8 | /** 9 | * A throwing {@link ObjDoubleConsumer} 10 | * 11 | * @param type parameter of the argument 12 | */ 13 | @FunctionalInterface 14 | public interface ThrowingObjIntConsumer 15 | extends ObjIntConsumer 16 | { 17 | void doAccept(T t, int value) 18 | throws Throwable; 19 | 20 | @Override 21 | default void accept(T t, int value) 22 | { 23 | try { 24 | doAccept(t, value); 25 | } catch (Error | RuntimeException e) { 26 | throw e; 27 | } catch (Throwable tooBad) { 28 | throw new ThrownByLambdaException(tooBad); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/consumers/ThrowingObjLongConsumer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.ObjDoubleConsumer; 6 | import java.util.function.ObjLongConsumer; 7 | 8 | /** 9 | * A throwing {@link ObjDoubleConsumer} 10 | * 11 | * @param type parameter of the argument 12 | */ 13 | @FunctionalInterface 14 | public interface ThrowingObjLongConsumer 15 | extends ObjLongConsumer 16 | { 17 | void doAccept(T t, long value) 18 | throws Throwable; 19 | 20 | @Override 21 | default void accept(T t, long value) 22 | { 23 | try { 24 | doAccept(t, value); 25 | } catch (Error | RuntimeException e) { 26 | throw e; 27 | } catch (Throwable tooBad) { 28 | throw new ThrownByLambdaException(tooBad); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/BiFunctionChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.BiFunction; 6 | 7 | public class BiFunctionChainer 8 | extends Chainer, ThrowingBiFunction, BiFunctionChainer> 9 | implements ThrowingBiFunction 10 | { 11 | public BiFunctionChainer(final ThrowingBiFunction throwing) 12 | { 13 | super(throwing); 14 | } 15 | 16 | @Override 17 | public R doApply(final T t, final U u) 18 | throws Throwable 19 | { 20 | return throwing.doApply(t, u); 21 | } 22 | 23 | @Override 24 | public BiFunctionChainer orTryWith( 25 | final ThrowingBiFunction other) 26 | { 27 | final ThrowingBiFunction biFunction = (t, u) -> { 28 | try { 29 | return throwing.doApply(t, u); 30 | } catch (Error | RuntimeException e) { 31 | throw e; 32 | } catch (Throwable ignored) { 33 | return other.doApply(t, u); 34 | } 35 | }; 36 | 37 | return new BiFunctionChainer<>(biFunction); 38 | } 39 | 40 | @Override 41 | public ThrowingBiFunction orThrow( 42 | final Class exclass) 43 | { 44 | return (t, u) -> { 45 | try { 46 | return throwing.doApply(t, u); 47 | } catch (Error | RuntimeException e) { 48 | throw e; 49 | } catch (Throwable throwable) { 50 | throw rethrow(exclass, throwable); 51 | } 52 | }; 53 | } 54 | 55 | @Override 56 | public BiFunction fallbackTo(final BiFunction fallback) 57 | { 58 | return (t, u) -> { 59 | try { 60 | return throwing.doApply(t, u); 61 | } catch (Error | RuntimeException e) { 62 | throw e; 63 | } catch (Throwable ignored) { 64 | return fallback.apply(t, u); 65 | } 66 | }; 67 | } 68 | 69 | @Override 70 | public BiFunction sneakyThrow() 71 | { 72 | return (t, u) -> { 73 | try { 74 | return throwing.doApply(t, u); 75 | } catch (Error | RuntimeException e) { 76 | throw e; 77 | } catch (Throwable throwable) { 78 | throw doSneakyThrow(throwable); 79 | } 80 | }; 81 | } 82 | 83 | public BiFunction orReturn(final R retval) 84 | { 85 | return (t, u) -> { 86 | try { 87 | return throwing.doApply(t, u); 88 | } catch (Error | RuntimeException e) { 89 | throw e; 90 | } catch (Throwable ignored) { 91 | return retval; 92 | } 93 | }; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/FunctionChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.Function; 6 | 7 | public class FunctionChainer 8 | extends Chainer, ThrowingFunction, FunctionChainer> 9 | implements ThrowingFunction 10 | { 11 | public FunctionChainer(final ThrowingFunction function) 12 | { 13 | super(function); 14 | } 15 | 16 | @Override 17 | public R doApply(final T t) 18 | throws Throwable 19 | { 20 | return throwing.doApply(t); 21 | } 22 | 23 | @Override 24 | public FunctionChainer orTryWith( 25 | final ThrowingFunction other) 26 | { 27 | final ThrowingFunction function = t -> { 28 | try { 29 | return throwing.doApply(t); 30 | } catch (Error | RuntimeException e) { 31 | throw e; 32 | } catch (Throwable ignored) { 33 | return other.doApply(t); 34 | } 35 | }; 36 | 37 | return new FunctionChainer<>(function); 38 | } 39 | 40 | @Override 41 | public ThrowingFunction orThrow( 42 | final Class exclass) 43 | { 44 | return t -> { 45 | try { 46 | return throwing.doApply(t); 47 | } catch (Error | RuntimeException e) { 48 | throw e; 49 | } catch (Throwable throwable) { 50 | throw rethrow(exclass, throwable); 51 | } 52 | }; 53 | } 54 | 55 | @Override 56 | public Function fallbackTo(final Function fallback) 57 | { 58 | return t -> { 59 | try { 60 | return doApply(t); 61 | } catch (Error | RuntimeException e) { 62 | throw e; 63 | } catch (Throwable ignored) { 64 | return fallback.apply(t); 65 | } 66 | }; 67 | } 68 | 69 | @Override 70 | public Function sneakyThrow() 71 | { 72 | return t -> { 73 | try { 74 | return throwing.doApply(t); 75 | } catch (Error | RuntimeException e) { 76 | throw e; 77 | } catch (Throwable throwable) { 78 | throw doSneakyThrow(throwable); 79 | } 80 | }; 81 | } 82 | 83 | public Function orReturn(final R retval) 84 | { 85 | return t -> { 86 | try { 87 | return doApply(t); 88 | } catch (Error | RuntimeException e) { 89 | throw e; 90 | } catch (Throwable ignored) { 91 | return retval; 92 | } 93 | }; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/ThrowingBiFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.BiFunction; 6 | 7 | /** 8 | * A throwing {@link BiFunction} 9 | * 10 | * @param parameter type of the first argument of this bifunction 11 | * @param parameter type of the second argument of this bifunction 12 | * @param parameter type of the return value of this bifunction 13 | */ 14 | @FunctionalInterface 15 | public interface ThrowingBiFunction 16 | extends BiFunction 17 | { 18 | R doApply(T t, U u) 19 | throws Throwable; 20 | 21 | @Override 22 | default R apply(T t, U u) 23 | { 24 | try { 25 | return doApply(t, u); 26 | } catch (Error | RuntimeException e) { 27 | throw e; 28 | } catch (Throwable throwable) { 29 | throw new ThrownByLambdaException(throwable); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/ThrowingFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.Function; 6 | 7 | @FunctionalInterface 8 | public interface ThrowingFunction 9 | extends Function 10 | { 11 | R doApply(T t) 12 | throws Throwable; 13 | 14 | default R apply(T t) 15 | { 16 | try { 17 | return doApply(t); 18 | } catch (Error | RuntimeException e) { 19 | throw e; 20 | } catch (Throwable throwable) { 21 | throw new ThrownByLambdaException(throwable); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/ThrowingToDoubleFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.ToDoubleFunction; 6 | 7 | /** 8 | * A throwing {@link ToDoubleFunction} 9 | * 10 | * @param type parameter for the argument to that function 11 | */ 12 | @FunctionalInterface 13 | public interface ThrowingToDoubleFunction 14 | extends ToDoubleFunction 15 | { 16 | double doApplyAsDouble(T value) 17 | throws Throwable; 18 | 19 | @Override 20 | default double applyAsDouble(T value) 21 | { 22 | try { 23 | return doApplyAsDouble(value); 24 | } catch (Error | RuntimeException e) { 25 | throw e; 26 | } catch (Throwable throwable) { 27 | throw new ThrownByLambdaException(throwable); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/ThrowingToIntFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.ToDoubleFunction; 6 | import java.util.function.ToIntFunction; 7 | 8 | /** 9 | * A throwing {@link ToDoubleFunction} 10 | * 11 | * @param type parameter for the argument to that function 12 | */ 13 | @FunctionalInterface 14 | public interface ThrowingToIntFunction 15 | extends ToIntFunction 16 | { 17 | int doApplyAsInt(T value) 18 | throws Throwable; 19 | 20 | @Override 21 | default int applyAsInt(T value) 22 | { 23 | try { 24 | return doApplyAsInt(value); 25 | } catch (Error | RuntimeException e) { 26 | throw e; 27 | } catch (Throwable throwable) { 28 | throw new ThrownByLambdaException(throwable); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/ThrowingToLongFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.ToDoubleFunction; 6 | import java.util.function.ToLongFunction; 7 | 8 | /** 9 | * A throwing {@link ToDoubleFunction} 10 | * 11 | * @param type parameter for the argument to that function 12 | */ 13 | @FunctionalInterface 14 | public interface ThrowingToLongFunction 15 | extends ToLongFunction 16 | { 17 | long doApplyAsLong(T value) 18 | throws Throwable; 19 | 20 | @Override 21 | default long applyAsLong(T value) 22 | { 23 | try { 24 | return doApplyAsLong(value); 25 | } catch (Error | RuntimeException e) { 26 | throw e; 27 | } catch (Throwable throwable) { 28 | throw new ThrownByLambdaException(throwable); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/ToDoubleFunctionChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.ToDoubleFunction; 6 | 7 | public class ToDoubleFunctionChainer 8 | extends Chainer, ThrowingToDoubleFunction, ToDoubleFunctionChainer> 9 | implements ThrowingToDoubleFunction 10 | { 11 | public ToDoubleFunctionChainer( 12 | final ThrowingToDoubleFunction throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public double doApplyAsDouble(final T value) 19 | throws Throwable 20 | { 21 | return throwing.doApplyAsDouble(value); 22 | } 23 | 24 | @Override 25 | public ToDoubleFunctionChainer orTryWith( 26 | final ThrowingToDoubleFunction other) 27 | { 28 | final ThrowingToDoubleFunction toDoubleFunction = value -> { 29 | try { 30 | return throwing.doApplyAsDouble(value); 31 | } catch (Error | RuntimeException e) { 32 | throw e; 33 | } catch (Throwable ignored) { 34 | return other.doApplyAsDouble(value); 35 | } 36 | }; 37 | 38 | return new ToDoubleFunctionChainer<>(toDoubleFunction); 39 | } 40 | 41 | @Override 42 | public ThrowingToDoubleFunction orThrow( 43 | final Class exclass) 44 | { 45 | return value -> { 46 | try { 47 | return throwing.doApplyAsDouble(value); 48 | } catch (Error | RuntimeException e) { 49 | throw e; 50 | } catch (Throwable throwable) { 51 | throw rethrow(exclass, throwable); 52 | } 53 | }; 54 | } 55 | 56 | @Override 57 | public ToDoubleFunction fallbackTo(final ToDoubleFunction fallback) 58 | { 59 | return value -> { 60 | try { 61 | return throwing.doApplyAsDouble(value); 62 | } catch (Error | RuntimeException e) { 63 | throw e; 64 | } catch (Throwable ignored) { 65 | return fallback.applyAsDouble(value); 66 | } 67 | }; 68 | } 69 | 70 | @Override 71 | public ToDoubleFunction sneakyThrow() 72 | { 73 | return value -> { 74 | try { 75 | return throwing.doApplyAsDouble(value); 76 | } catch (Error | RuntimeException e) { 77 | throw e; 78 | } catch (Throwable throwable) { 79 | throw doSneakyThrow(throwable); 80 | } 81 | }; 82 | } 83 | 84 | public ToDoubleFunction orReturn(final double retval) 85 | { 86 | return value -> { 87 | try { 88 | return throwing.doApplyAsDouble(value); 89 | } catch (Error | RuntimeException e) { 90 | throw e; 91 | } catch (Throwable ignored) { 92 | return retval; 93 | } 94 | }; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/ToIntFunctionChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.ToIntFunction; 6 | 7 | public class ToIntFunctionChainer 8 | extends Chainer, ThrowingToIntFunction, ToIntFunctionChainer> 9 | implements ThrowingToIntFunction 10 | { 11 | public ToIntFunctionChainer( 12 | final ThrowingToIntFunction throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public int doApplyAsInt(final T value) 19 | throws Throwable 20 | { 21 | return throwing.doApplyAsInt(value); 22 | } 23 | 24 | @Override 25 | public ToIntFunctionChainer orTryWith( 26 | final ThrowingToIntFunction other) 27 | { 28 | final ThrowingToIntFunction toIntFunction = value -> { 29 | try { 30 | return throwing.doApplyAsInt(value); 31 | } catch (Error | RuntimeException e) { 32 | throw e; 33 | } catch (Throwable ignored) { 34 | return other.doApplyAsInt(value); 35 | } 36 | }; 37 | 38 | return new ToIntFunctionChainer<>(toIntFunction); 39 | } 40 | 41 | @Override 42 | public ThrowingToIntFunction orThrow( 43 | final Class exclass) 44 | { 45 | return value -> { 46 | try { 47 | return throwing.doApplyAsInt(value); 48 | } catch (Error | RuntimeException e) { 49 | throw e; 50 | } catch (Throwable throwable) { 51 | throw rethrow(exclass, throwable); 52 | } 53 | }; 54 | } 55 | 56 | @Override 57 | public ToIntFunction fallbackTo(final ToIntFunction fallback) 58 | { 59 | return value -> { 60 | try { 61 | return throwing.doApplyAsInt(value); 62 | } catch (Error | RuntimeException e) { 63 | throw e; 64 | } catch (Throwable ignored) { 65 | return fallback.applyAsInt(value); 66 | } 67 | }; 68 | } 69 | 70 | @Override 71 | public ToIntFunction sneakyThrow() 72 | { 73 | return value -> { 74 | try { 75 | return throwing.doApplyAsInt(value); 76 | } catch (Error | RuntimeException e) { 77 | throw e; 78 | } catch (Throwable throwable) { 79 | throw doSneakyThrow(throwable); 80 | } 81 | }; 82 | } 83 | 84 | public ToIntFunction orReturn(final int retval) 85 | { 86 | return value -> { 87 | try { 88 | return throwing.doApplyAsInt(value); 89 | } catch (Error | RuntimeException e) { 90 | throw e; 91 | } catch (Throwable ignored) { 92 | return retval; 93 | } 94 | }; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/ToLongFunctionChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.ToLongFunction; 6 | 7 | public class ToLongFunctionChainer 8 | extends Chainer, ThrowingToLongFunction, ToLongFunctionChainer> 9 | implements ThrowingToLongFunction 10 | { 11 | public ToLongFunctionChainer( 12 | final ThrowingToLongFunction throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public long doApplyAsLong(final T value) 19 | throws Throwable 20 | { 21 | return throwing.doApplyAsLong(value); 22 | } 23 | 24 | @Override 25 | public ToLongFunctionChainer orTryWith( 26 | final ThrowingToLongFunction other) 27 | { 28 | final ThrowingToLongFunction toLongFunction = value -> { 29 | try { 30 | return throwing.doApplyAsLong(value); 31 | } catch (Error | RuntimeException e) { 32 | throw e; 33 | } catch (Throwable ignored) { 34 | return other.doApplyAsLong(value); 35 | } 36 | }; 37 | 38 | return new ToLongFunctionChainer<>(toLongFunction); 39 | } 40 | 41 | @Override 42 | public ThrowingToLongFunction orThrow( 43 | final Class exclass) 44 | { 45 | return value -> { 46 | try { 47 | return throwing.doApplyAsLong(value); 48 | } catch (Error | RuntimeException e) { 49 | throw e; 50 | } catch (Throwable throwable) { 51 | throw rethrow(exclass, throwable); 52 | } 53 | }; 54 | } 55 | 56 | @Override 57 | public ToLongFunction fallbackTo(final ToLongFunction fallback) 58 | { 59 | return value -> { 60 | try { 61 | return throwing.doApplyAsLong(value); 62 | } catch (Error | RuntimeException e) { 63 | throw e; 64 | } catch (Throwable ignored) { 65 | return fallback.applyAsLong(value); 66 | } 67 | }; 68 | } 69 | 70 | @Override 71 | public ToLongFunction sneakyThrow() 72 | { 73 | return value -> { 74 | try { 75 | return throwing.doApplyAsLong(value); 76 | } catch (Error | RuntimeException e) { 77 | throw e; 78 | } catch (Throwable throwable) { 79 | throw doSneakyThrow(throwable); 80 | } 81 | }; 82 | } 83 | 84 | public ToLongFunction orReturn(final long retval) 85 | { 86 | return value -> { 87 | try { 88 | return throwing.doApplyAsLong(value); 89 | } catch (Error | RuntimeException e) { 90 | throw e; 91 | } catch (Throwable ignored) { 92 | return retval; 93 | } 94 | }; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/doublefunctions/DoubleFunctionChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.doublefunctions; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.DoubleFunction; 6 | 7 | public class DoubleFunctionChainer 8 | extends Chainer, ThrowingDoubleFunction, DoubleFunctionChainer> 9 | implements ThrowingDoubleFunction 10 | { 11 | public DoubleFunctionChainer( 12 | final ThrowingDoubleFunction throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public R doApply(final double value) 19 | throws Throwable 20 | { 21 | return throwing.doApply(value); 22 | } 23 | 24 | @Override 25 | public DoubleFunctionChainer orTryWith( 26 | final ThrowingDoubleFunction other) 27 | { 28 | final ThrowingDoubleFunction doubleFunction = value -> { 29 | try { 30 | return throwing.doApply(value); 31 | } catch (Error | RuntimeException e) { 32 | throw e; 33 | } catch (Throwable ignored) { 34 | return other.doApply(value); 35 | } 36 | }; 37 | 38 | return new DoubleFunctionChainer<>(doubleFunction); 39 | } 40 | 41 | @Override 42 | public ThrowingDoubleFunction orThrow( 43 | final Class exclass) 44 | { 45 | return value -> { 46 | try { 47 | return throwing.doApply(value); 48 | } catch (Error | RuntimeException e) { 49 | throw e; 50 | } catch (Throwable throwable) { 51 | throw rethrow(exclass, throwable); 52 | } 53 | }; 54 | } 55 | 56 | @Override 57 | public DoubleFunction fallbackTo(final DoubleFunction fallback) 58 | { 59 | return value -> { 60 | try { 61 | return throwing.doApply(value); 62 | } catch (Error | RuntimeException e) { 63 | throw e; 64 | } catch (Throwable ignored) { 65 | return fallback.apply(value); 66 | } 67 | }; 68 | } 69 | 70 | @Override 71 | public DoubleFunction sneakyThrow() 72 | { 73 | return value -> { 74 | try { 75 | return throwing.doApply(value); 76 | } catch (Error | RuntimeException e) { 77 | throw e; 78 | } catch (Throwable throwable) { 79 | throw doSneakyThrow(throwable); 80 | } 81 | }; 82 | } 83 | 84 | public DoubleFunction orReturn(final R retval) 85 | { 86 | return value -> { 87 | try { 88 | return throwing.doApply(value); 89 | } catch (Error | RuntimeException e) { 90 | throw e; 91 | } catch (Throwable ignored) { 92 | return retval; 93 | } 94 | }; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/doublefunctions/DoubleToIntFunctionChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.doublefunctions; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.DoubleToIntFunction; 6 | 7 | public class DoubleToIntFunctionChainer 8 | extends Chainer 9 | implements ThrowingDoubleToIntFunction 10 | { 11 | public DoubleToIntFunctionChainer( 12 | final ThrowingDoubleToIntFunction throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public int doApplyAsInt(final double value) 19 | throws Throwable 20 | { 21 | return throwing.doApplyAsInt(value); 22 | } 23 | 24 | @Override 25 | public DoubleToIntFunctionChainer orTryWith( 26 | final ThrowingDoubleToIntFunction other) 27 | { 28 | final ThrowingDoubleToIntFunction doubleToIntFunction = value -> { 29 | try { 30 | return throwing.doApplyAsInt(value); 31 | } catch (Error | RuntimeException e) { 32 | throw e; 33 | } catch (Throwable ignored) { 34 | return other.doApplyAsInt(value); 35 | } 36 | }; 37 | 38 | return new DoubleToIntFunctionChainer(doubleToIntFunction); 39 | } 40 | 41 | @Override 42 | public ThrowingDoubleToIntFunction orThrow( 43 | final Class exclass) 44 | { 45 | return value -> { 46 | try { 47 | return throwing.doApplyAsInt(value); 48 | } catch (Error | RuntimeException e) { 49 | throw e; 50 | } catch (Throwable throwable) { 51 | throw rethrow(exclass, throwable); 52 | } 53 | }; 54 | } 55 | 56 | @Override 57 | public DoubleToIntFunction fallbackTo(final DoubleToIntFunction fallback) 58 | { 59 | return value -> { 60 | try { 61 | return throwing.doApplyAsInt(value); 62 | } catch (Error | RuntimeException e) { 63 | throw e; 64 | } catch (Throwable ignored) { 65 | return fallback.applyAsInt(value); 66 | } 67 | }; 68 | } 69 | 70 | @Override 71 | public DoubleToIntFunction sneakyThrow() 72 | { 73 | return value -> { 74 | try { 75 | return throwing.doApplyAsInt(value); 76 | } catch (Error | RuntimeException e) { 77 | throw e; 78 | } catch (Throwable throwable) { 79 | throw doSneakyThrow(throwable); 80 | } 81 | }; 82 | } 83 | 84 | public DoubleToIntFunction orReturn(final int retval) 85 | { 86 | return value -> { 87 | try { 88 | return throwing.doApplyAsInt(value); 89 | } catch (Error | RuntimeException e) { 90 | throw e; 91 | } catch (Throwable ignored) { 92 | return retval; 93 | } 94 | }; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/doublefunctions/DoubleToLongFunctionChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.doublefunctions; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.DoubleToLongFunction; 6 | 7 | public class DoubleToLongFunctionChainer 8 | extends Chainer 9 | implements ThrowingDoubleToLongFunction 10 | { 11 | public DoubleToLongFunctionChainer( 12 | final ThrowingDoubleToLongFunction throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public long doApplyAsLong(final double value) 19 | throws Throwable 20 | { 21 | return throwing.doApplyAsLong(value); 22 | } 23 | 24 | @Override 25 | public DoubleToLongFunctionChainer orTryWith( 26 | final ThrowingDoubleToLongFunction other) 27 | { 28 | final ThrowingDoubleToLongFunction doubleToLongFunction = value -> { 29 | try { 30 | return throwing.doApplyAsLong(value); 31 | } catch (Error | RuntimeException e) { 32 | throw e; 33 | } catch (Throwable ignored) { 34 | return other.doApplyAsLong(value); 35 | } 36 | }; 37 | 38 | return new DoubleToLongFunctionChainer(doubleToLongFunction); 39 | } 40 | 41 | @Override 42 | public ThrowingDoubleToLongFunction orThrow( 43 | final Class exclass) 44 | { 45 | return value -> { 46 | try { 47 | return throwing.doApplyAsLong(value); 48 | } catch (Error | RuntimeException e) { 49 | throw e; 50 | } catch (Throwable throwable) { 51 | throw rethrow(exclass, throwable); 52 | } 53 | }; 54 | } 55 | 56 | @Override 57 | public DoubleToLongFunction fallbackTo(final DoubleToLongFunction fallback) 58 | { 59 | return value -> { 60 | try { 61 | return throwing.doApplyAsLong(value); 62 | } catch (Error | RuntimeException e) { 63 | throw e; 64 | } catch (Throwable ignored) { 65 | return fallback.applyAsLong(value); 66 | } 67 | }; 68 | } 69 | 70 | @Override 71 | public DoubleToLongFunction sneakyThrow() 72 | { 73 | return value -> { 74 | try { 75 | return throwing.doApplyAsLong(value); 76 | } catch (Error | RuntimeException e) { 77 | throw e; 78 | } catch (Throwable throwable) { 79 | throw doSneakyThrow(throwable); 80 | } 81 | }; 82 | } 83 | 84 | public DoubleToLongFunction orReturn(final long retval) 85 | { 86 | return value -> { 87 | try { 88 | return throwing.doApplyAsLong(value); 89 | } catch (Error | RuntimeException e) { 90 | throw e; 91 | } catch (Throwable ignored) { 92 | return retval; 93 | } 94 | }; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/doublefunctions/ThrowingDoubleFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.doublefunctions; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.DoubleFunction; 6 | 7 | /** 8 | * A throwing {@link DoubleFunction} 9 | * 10 | * @param parameter type of the return value of this function 11 | */ 12 | @FunctionalInterface 13 | public interface ThrowingDoubleFunction 14 | extends DoubleFunction 15 | { 16 | R doApply(double value) 17 | throws Throwable; 18 | 19 | @Override 20 | default R apply(double value) 21 | { 22 | try { 23 | return doApply(value); 24 | } catch (Error | RuntimeException e) { 25 | throw e; 26 | } catch (Throwable throwable) { 27 | throw new ThrownByLambdaException(throwable); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/doublefunctions/ThrowingDoubleToIntFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.doublefunctions; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.DoubleToIntFunction; 6 | 7 | /** 8 | * A throwing {@link DoubleToIntFunction} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingDoubleToIntFunction 12 | extends DoubleToIntFunction 13 | { 14 | int doApplyAsInt(double value) 15 | throws Throwable; 16 | 17 | @Override 18 | default int applyAsInt(double value) 19 | { 20 | try { 21 | return doApplyAsInt(value); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable throwable) { 25 | throw new ThrownByLambdaException(throwable); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/doublefunctions/ThrowingDoubleToLongFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.doublefunctions; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.DoubleToIntFunction; 6 | import java.util.function.DoubleToLongFunction; 7 | 8 | /** 9 | * A throwing {@link DoubleToIntFunction} 10 | */ 11 | @FunctionalInterface 12 | public interface ThrowingDoubleToLongFunction 13 | extends DoubleToLongFunction 14 | { 15 | long doApplyAsLong(double value) 16 | throws Throwable; 17 | 18 | @Override 19 | default long applyAsLong(double value) 20 | { 21 | try { 22 | return doApplyAsLong(value); 23 | } catch (Error | RuntimeException e) { 24 | throw e; 25 | } catch (Throwable throwable) { 26 | throw new ThrownByLambdaException(throwable); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/intfunctions/IntFunctionChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.intfunctions; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.IntFunction; 6 | 7 | public class IntFunctionChainer 8 | extends Chainer, ThrowingIntFunction, IntFunctionChainer> 9 | implements ThrowingIntFunction 10 | { 11 | public IntFunctionChainer( 12 | final ThrowingIntFunction throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public R doApply(final int value) 19 | throws Throwable 20 | { 21 | return throwing.doApply(value); 22 | } 23 | 24 | @Override 25 | public IntFunctionChainer orTryWith( 26 | final ThrowingIntFunction other) 27 | { 28 | final ThrowingIntFunction doubleFunction = value -> { 29 | try { 30 | return throwing.doApply(value); 31 | } catch (Error | RuntimeException e) { 32 | throw e; 33 | } catch (Throwable ignored) { 34 | return other.doApply(value); 35 | } 36 | }; 37 | 38 | return new IntFunctionChainer<>(doubleFunction); 39 | } 40 | 41 | @Override 42 | public ThrowingIntFunction orThrow( 43 | final Class exclass) 44 | { 45 | return value -> { 46 | try { 47 | return throwing.doApply(value); 48 | } catch (Error | RuntimeException e) { 49 | throw e; 50 | } catch (Throwable throwable) { 51 | throw rethrow(exclass, throwable); 52 | } 53 | }; 54 | } 55 | 56 | @Override 57 | public IntFunction fallbackTo(final IntFunction fallback) 58 | { 59 | return value -> { 60 | try { 61 | return throwing.doApply(value); 62 | } catch (Error | RuntimeException e) { 63 | throw e; 64 | } catch (Throwable ignored) { 65 | return fallback.apply(value); 66 | } 67 | }; 68 | } 69 | 70 | @Override 71 | public IntFunction sneakyThrow() 72 | { 73 | return value -> { 74 | try { 75 | return throwing.doApply(value); 76 | } catch (Error | RuntimeException e) { 77 | throw e; 78 | } catch (Throwable throwable) { 79 | throw doSneakyThrow(throwable); 80 | } 81 | }; 82 | } 83 | 84 | public IntFunction orReturn(final R retval) 85 | { 86 | return value -> { 87 | try { 88 | return throwing.doApply(value); 89 | } catch (Error | RuntimeException e) { 90 | throw e; 91 | } catch (Throwable ignored) { 92 | return retval; 93 | } 94 | }; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/intfunctions/IntToDoubleFunctionChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.intfunctions; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.IntToDoubleFunction; 6 | 7 | public class IntToDoubleFunctionChainer 8 | extends Chainer 9 | implements ThrowingIntToDoubleFunction 10 | { 11 | public IntToDoubleFunctionChainer( 12 | final ThrowingIntToDoubleFunction throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public double doApplyAsDouble(final int value) 19 | throws Throwable 20 | { 21 | return throwing.doApplyAsDouble(value); 22 | } 23 | 24 | @Override 25 | public IntToDoubleFunctionChainer orTryWith( 26 | final ThrowingIntToDoubleFunction other) 27 | { 28 | final ThrowingIntToDoubleFunction intToDoubleFunction = value -> { 29 | try { 30 | return throwing.doApplyAsDouble(value); 31 | } catch (Error | RuntimeException e) { 32 | throw e; 33 | } catch (Throwable ignored) { 34 | return other.doApplyAsDouble(value); 35 | } 36 | }; 37 | 38 | return new IntToDoubleFunctionChainer(intToDoubleFunction); 39 | } 40 | 41 | @Override 42 | public ThrowingIntToDoubleFunction orThrow( 43 | final Class exclass) 44 | { 45 | return value -> { 46 | try { 47 | return throwing.doApplyAsDouble(value); 48 | } catch (Error | RuntimeException e) { 49 | throw e; 50 | } catch (Throwable throwable) { 51 | throw rethrow(exclass, throwable); 52 | } 53 | }; 54 | } 55 | 56 | @Override 57 | public IntToDoubleFunction fallbackTo(final IntToDoubleFunction fallback) 58 | { 59 | return value -> { 60 | try { 61 | return throwing.doApplyAsDouble(value); 62 | } catch (Error | RuntimeException e) { 63 | throw e; 64 | } catch (Throwable ignored) { 65 | return fallback.applyAsDouble(value); 66 | } 67 | }; 68 | } 69 | 70 | @Override 71 | public IntToDoubleFunction sneakyThrow() 72 | { 73 | return value -> { 74 | try { 75 | return throwing.doApplyAsDouble(value); 76 | } catch (Error | RuntimeException e) { 77 | throw e; 78 | } catch (Throwable throwable) { 79 | throw doSneakyThrow(throwable); 80 | } 81 | }; 82 | } 83 | 84 | public IntToDoubleFunction orReturn(final double retval) 85 | { 86 | return value -> { 87 | try { 88 | return throwing.doApplyAsDouble(value); 89 | } catch (Error | RuntimeException e) { 90 | throw e; 91 | } catch (Throwable ignored) { 92 | return retval; 93 | } 94 | }; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/intfunctions/IntToLongFunctionChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.intfunctions; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.IntToLongFunction; 6 | 7 | public class IntToLongFunctionChainer 8 | extends Chainer 9 | implements ThrowingIntToLongFunction 10 | { 11 | public IntToLongFunctionChainer( 12 | final ThrowingIntToLongFunction throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public long doApplyAsLong(final int value) 19 | throws Throwable 20 | { 21 | return throwing.doApplyAsLong(value); 22 | } 23 | 24 | @Override 25 | public IntToLongFunctionChainer orTryWith( 26 | final ThrowingIntToLongFunction other) 27 | { 28 | final ThrowingIntToLongFunction intToLongFunction = value -> { 29 | try { 30 | return throwing.doApplyAsLong(value); 31 | } catch (Error | RuntimeException e) { 32 | throw e; 33 | } catch (Throwable ignored) { 34 | return other.doApplyAsLong(value); 35 | } 36 | }; 37 | 38 | return new IntToLongFunctionChainer(intToLongFunction); 39 | } 40 | 41 | @Override 42 | public ThrowingIntToLongFunction orThrow( 43 | final Class exclass) 44 | { 45 | return value -> { 46 | try { 47 | return throwing.doApplyAsLong(value); 48 | } catch (Error | RuntimeException e) { 49 | throw e; 50 | } catch (Throwable throwable) { 51 | throw rethrow(exclass, throwable); 52 | } 53 | }; 54 | } 55 | 56 | @Override 57 | public IntToLongFunction fallbackTo(final IntToLongFunction fallback) 58 | { 59 | return value -> { 60 | try { 61 | return throwing.doApplyAsLong(value); 62 | } catch (Error | RuntimeException e) { 63 | throw e; 64 | } catch (Throwable ignored) { 65 | return fallback.applyAsLong(value); 66 | } 67 | }; 68 | } 69 | 70 | @Override 71 | public IntToLongFunction sneakyThrow() 72 | { 73 | return value -> { 74 | try { 75 | return throwing.doApplyAsLong(value); 76 | } catch (Error | RuntimeException e) { 77 | throw e; 78 | } catch (Throwable throwable) { 79 | throw doSneakyThrow(throwable); 80 | } 81 | }; 82 | } 83 | 84 | public IntToLongFunction orReturn(final long retval) 85 | { 86 | return value -> { 87 | try { 88 | return throwing.doApplyAsLong(value); 89 | } catch (Error | RuntimeException e) { 90 | throw e; 91 | } catch (Throwable ignored) { 92 | return retval; 93 | } 94 | }; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/intfunctions/ThrowingIntFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.intfunctions; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.IntFunction; 6 | 7 | /** 8 | * A throwing {@link IntFunction} 9 | * 10 | * @param parameter type of the return value of this function 11 | */ 12 | @FunctionalInterface 13 | public interface ThrowingIntFunction 14 | extends IntFunction 15 | { 16 | R doApply(int value) 17 | throws Throwable; 18 | 19 | @Override 20 | default R apply(int value) 21 | { 22 | try { 23 | return doApply(value); 24 | } catch (Error | RuntimeException e) { 25 | throw e; 26 | } catch (Throwable throwable) { 27 | throw new ThrownByLambdaException(throwable); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/intfunctions/ThrowingIntToDoubleFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.intfunctions; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.IntToDoubleFunction; 6 | 7 | /** 8 | * A throwing {@link IntToDoubleFunction} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingIntToDoubleFunction 12 | extends IntToDoubleFunction 13 | { 14 | double doApplyAsDouble(int value) 15 | throws Throwable; 16 | 17 | @Override 18 | default double applyAsDouble(int value) 19 | { 20 | try { 21 | return doApplyAsDouble(value); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable throwable) { 25 | throw new ThrownByLambdaException(throwable); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/intfunctions/ThrowingIntToLongFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.intfunctions; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.IntToDoubleFunction; 6 | import java.util.function.IntToLongFunction; 7 | 8 | /** 9 | * A throwing {@link IntToDoubleFunction} 10 | */ 11 | @FunctionalInterface 12 | public interface ThrowingIntToLongFunction 13 | extends IntToLongFunction 14 | { 15 | long doApplyAsLong(int value) 16 | throws Throwable; 17 | 18 | @Override 19 | default long applyAsLong(int value) 20 | { 21 | try { 22 | return doApplyAsLong(value); 23 | } catch (Error | RuntimeException e) { 24 | throw e; 25 | } catch (Throwable throwable) { 26 | throw new ThrownByLambdaException(throwable); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/longfunctions/LongFunctionChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.longfunctions; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.LongFunction; 6 | 7 | public class LongFunctionChainer 8 | extends Chainer, ThrowingLongFunction, LongFunctionChainer> 9 | implements ThrowingLongFunction 10 | { 11 | public LongFunctionChainer( 12 | final ThrowingLongFunction throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public R doApply(final long value) 19 | throws Throwable 20 | { 21 | return throwing.doApply(value); 22 | } 23 | 24 | @Override 25 | public LongFunctionChainer orTryWith(final ThrowingLongFunction other) 26 | { 27 | final ThrowingLongFunction longFunction = value -> { 28 | try { 29 | return throwing.doApply(value); 30 | } catch (Error | RuntimeException e) { 31 | throw e; 32 | } catch (Throwable ignored) { 33 | return other.doApply(value); 34 | } 35 | }; 36 | 37 | return new LongFunctionChainer<>(longFunction); 38 | } 39 | 40 | @Override 41 | public ThrowingLongFunction orThrow( 42 | final Class exclass) 43 | { 44 | return value -> { 45 | try { 46 | return throwing.doApply(value); 47 | } catch (Error | RuntimeException e) { 48 | throw e; 49 | } catch (Throwable throwable) { 50 | throw rethrow(exclass, throwable); 51 | } 52 | }; 53 | } 54 | 55 | @Override 56 | public LongFunction fallbackTo(final LongFunction fallback) 57 | { 58 | return value -> { 59 | try { 60 | return throwing.doApply(value); 61 | } catch (Error | RuntimeException e) { 62 | throw e; 63 | } catch (Throwable ignored) { 64 | return fallback.apply(value); 65 | } 66 | }; 67 | } 68 | 69 | @Override 70 | public LongFunction sneakyThrow() 71 | { 72 | return value -> { 73 | try { 74 | return throwing.doApply(value); 75 | } catch (Error | RuntimeException e) { 76 | throw e; 77 | } catch (Throwable throwable) { 78 | throw doSneakyThrow(throwable); 79 | } 80 | }; 81 | } 82 | 83 | public LongFunction orReturn(final R retval) 84 | { 85 | return value -> { 86 | try { 87 | return throwing.doApply(value); 88 | } catch (Error | RuntimeException e) { 89 | throw e; 90 | } catch (Throwable ignored) { 91 | return retval; 92 | } 93 | }; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/longfunctions/LongToDoubleFunctionChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.longfunctions; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.LongToDoubleFunction; 6 | 7 | public class LongToDoubleFunctionChainer 8 | extends Chainer 9 | implements ThrowingLongToDoubleFunction 10 | { 11 | public LongToDoubleFunctionChainer( 12 | final ThrowingLongToDoubleFunction throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public double doApplyAsDouble(final long value) 19 | throws Throwable 20 | { 21 | return throwing.doApplyAsDouble(value); 22 | } 23 | 24 | @Override 25 | public LongToDoubleFunctionChainer orTryWith( 26 | final ThrowingLongToDoubleFunction other) 27 | { 28 | final ThrowingLongToDoubleFunction longToDoubleFunction = value -> { 29 | try { 30 | return throwing.doApplyAsDouble(value); 31 | } catch (Error | RuntimeException e) { 32 | throw e; 33 | } catch (Throwable ignored) { 34 | return other.doApplyAsDouble(value); 35 | } 36 | }; 37 | 38 | return new LongToDoubleFunctionChainer(longToDoubleFunction); 39 | } 40 | 41 | @Override 42 | public ThrowingLongToDoubleFunction orThrow( 43 | final Class exclass) 44 | { 45 | return value -> { 46 | try { 47 | return throwing.doApplyAsDouble(value); 48 | } catch (Error | RuntimeException e) { 49 | throw e; 50 | } catch (Throwable throwable) { 51 | throw rethrow(exclass, throwable); 52 | } 53 | }; 54 | } 55 | 56 | @Override 57 | public LongToDoubleFunction fallbackTo(final LongToDoubleFunction fallback) 58 | { 59 | return value -> { 60 | try { 61 | return throwing.doApplyAsDouble(value); 62 | } catch (Error | RuntimeException e) { 63 | throw e; 64 | } catch (Throwable ignored) { 65 | return fallback.applyAsDouble(value); 66 | } 67 | }; 68 | } 69 | 70 | @Override 71 | public LongToDoubleFunction sneakyThrow() 72 | { 73 | return value -> { 74 | try { 75 | return throwing.doApplyAsDouble(value); 76 | } catch (Error | RuntimeException e) { 77 | throw e; 78 | } catch (Throwable throwable) { 79 | throw doSneakyThrow(throwable); 80 | } 81 | }; 82 | } 83 | 84 | public LongToDoubleFunction orReturn(final double retval) { 85 | return value -> { 86 | try { 87 | return throwing.doApplyAsDouble(value); 88 | } catch (Error | RuntimeException e) { 89 | throw e; 90 | } catch (Throwable ignored) { 91 | return retval; 92 | } 93 | }; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/longfunctions/LongToIntFunctionChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.longfunctions; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.LongToDoubleFunction; 6 | import java.util.function.LongToIntFunction; 7 | 8 | public class LongToIntFunctionChainer 9 | extends Chainer 10 | implements ThrowingLongToIntFunction 11 | { 12 | public LongToIntFunctionChainer( 13 | final ThrowingLongToIntFunction throwing) 14 | { 15 | super(throwing); 16 | } 17 | 18 | @Override 19 | public int doApplyAsInt(final long value) 20 | throws Throwable 21 | { 22 | return throwing.doApplyAsInt(value); 23 | } 24 | 25 | @Override 26 | public LongToIntFunctionChainer orTryWith( 27 | final ThrowingLongToIntFunction other) 28 | { 29 | final ThrowingLongToIntFunction longToIntFunction = value -> { 30 | try { 31 | return throwing.doApplyAsInt(value); 32 | } catch (Error | RuntimeException e) { 33 | throw e; 34 | } catch (Throwable ignored) { 35 | return other.doApplyAsInt(value); 36 | } 37 | }; 38 | 39 | return new LongToIntFunctionChainer(longToIntFunction); 40 | } 41 | 42 | @Override 43 | public ThrowingLongToIntFunction orThrow( 44 | final Class exclass) 45 | { 46 | return value -> { 47 | try { 48 | return throwing.doApplyAsInt(value); 49 | } catch (Error | RuntimeException e) { 50 | throw e; 51 | } catch (Throwable throwable) { 52 | throw rethrow(exclass, throwable); 53 | } 54 | }; 55 | } 56 | 57 | @Override 58 | public LongToIntFunction fallbackTo(final LongToIntFunction fallback) 59 | { 60 | return value -> { 61 | try { 62 | return throwing.doApplyAsInt(value); 63 | } catch (Error | RuntimeException e) { 64 | throw e; 65 | } catch (Throwable ignored) { 66 | return fallback.applyAsInt(value); 67 | } 68 | }; 69 | } 70 | 71 | @Override 72 | public LongToIntFunction sneakyThrow() 73 | { 74 | return value -> { 75 | try { 76 | return throwing.doApplyAsInt(value); 77 | } catch (Error | RuntimeException e) { 78 | throw e; 79 | } catch (Throwable throwable) { 80 | throw doSneakyThrow(throwable); 81 | } 82 | }; 83 | } 84 | 85 | public LongToIntFunction orReturn(final int retval) { 86 | return value -> { 87 | try { 88 | return throwing.doApplyAsInt(value); 89 | } catch (Error | RuntimeException e) { 90 | throw e; 91 | } catch (Throwable ignored) { 92 | return retval; 93 | } 94 | }; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/longfunctions/ThrowingLongFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.longfunctions; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.LongFunction; 6 | 7 | /** 8 | * A throwing {@link LongFunction} 9 | * 10 | * @param parameter type of the return value of this function 11 | */ 12 | @FunctionalInterface 13 | public interface ThrowingLongFunction 14 | extends LongFunction 15 | { 16 | R doApply(long value) 17 | throws Throwable; 18 | 19 | @Override 20 | default R apply(long value) 21 | { 22 | try { 23 | return doApply(value); 24 | } catch (Error | RuntimeException e) { 25 | throw e; 26 | } catch (Throwable throwable) { 27 | throw new ThrownByLambdaException(throwable); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/longfunctions/ThrowingLongToDoubleFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.longfunctions; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.LongToDoubleFunction; 6 | 7 | /** 8 | * A throwing {@link LongToDoubleFunction} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingLongToDoubleFunction 12 | extends LongToDoubleFunction 13 | { 14 | double doApplyAsDouble(long value) 15 | throws Throwable; 16 | 17 | @Override 18 | default double applyAsDouble(long value) 19 | { 20 | try { 21 | return doApplyAsDouble(value); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable throwable) { 25 | throw new ThrownByLambdaException(throwable); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/longfunctions/ThrowingLongToIntFunction.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.longfunctions; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.LongToIntFunction; 6 | 7 | /** 8 | * A throwing {@link LongToIntFunction} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingLongToIntFunction 12 | extends LongToIntFunction 13 | { 14 | int doApplyAsInt(long value) 15 | throws Throwable; 16 | 17 | @Override 18 | default int applyAsInt(long value) 19 | { 20 | try { 21 | return doApplyAsInt(value); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable throwable) { 25 | throw new ThrownByLambdaException(throwable); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/operators/ThrowingBinaryOperator.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.operators; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.BinaryOperator; 6 | 7 | /** 8 | * A throwing {@link BinaryOperator} 9 | * 10 | * @param parameter type of the two arguments, and return type, of this 11 | * binary operator 12 | */ 13 | @FunctionalInterface 14 | public interface ThrowingBinaryOperator 15 | extends BinaryOperator 16 | { 17 | T doApply(T t, T u) 18 | throws Throwable; 19 | 20 | @Override 21 | default T apply(T t, T u) 22 | { 23 | try { 24 | return doApply(t, u); 25 | } catch (Error | RuntimeException e) { 26 | throw e; 27 | } catch (Throwable throwable) { 28 | throw new ThrownByLambdaException(throwable); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/operators/ThrowingDoubleBinaryOperator.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.operators; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.DoubleBinaryOperator; 6 | 7 | /** 8 | * A throwing {@link DoubleBinaryOperator} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingDoubleBinaryOperator 12 | extends DoubleBinaryOperator 13 | { 14 | double doApplyAsDouble(double left, double right) 15 | throws Throwable; 16 | 17 | @Override 18 | default double applyAsDouble(double left, double right) 19 | { 20 | try { 21 | return doApplyAsDouble(left, right); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable throwable) { 25 | throw new ThrownByLambdaException(throwable); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/operators/ThrowingDoubleUnaryOperator.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.operators; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.DoubleUnaryOperator; 6 | 7 | /** 8 | * A throwing {@link DoubleUnaryOperator} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingDoubleUnaryOperator 12 | extends DoubleUnaryOperator 13 | { 14 | double doApplyAsDouble(double operand) 15 | throws Throwable; 16 | 17 | @Override 18 | default double applyAsDouble(double operand) 19 | { 20 | try { 21 | return doApplyAsDouble(operand); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable throwable) { 25 | throw new ThrownByLambdaException(throwable); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/operators/ThrowingIntBinaryOperator.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.operators; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.IntBinaryOperator; 6 | 7 | /** 8 | * A throwing {@link IntBinaryOperator} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingIntBinaryOperator 12 | extends IntBinaryOperator 13 | { 14 | int doApplyAsInt(int left, int right) 15 | throws Throwable; 16 | 17 | @Override 18 | default int applyAsInt(int left, int right) 19 | { 20 | try { 21 | return doApplyAsInt(left, right); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable throwable) { 25 | throw new ThrownByLambdaException(throwable); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/operators/ThrowingIntUnaryOperator.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.operators; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.IntUnaryOperator; 6 | 7 | /** 8 | * A throwing {@link IntUnaryOperator} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingIntUnaryOperator 12 | extends IntUnaryOperator 13 | { 14 | int doApplyAsInt(int operand) 15 | throws Throwable; 16 | 17 | @Override 18 | default int applyAsInt(int operand) 19 | { 20 | try { 21 | return doApplyAsInt(operand); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable throwable) { 25 | throw new ThrownByLambdaException(throwable); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/operators/ThrowingLongBinaryOperator.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.operators; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.LongBinaryOperator; 6 | 7 | /** 8 | * A throwing {@link LongBinaryOperator} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingLongBinaryOperator 12 | extends LongBinaryOperator 13 | { 14 | long doApplyAsLong(long left, long right) 15 | throws Throwable; 16 | 17 | @Override 18 | default long applyAsLong(long left, long right) 19 | { 20 | try { 21 | return doApplyAsLong(left, right); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable throwable) { 25 | throw new ThrownByLambdaException(throwable); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/operators/ThrowingLongUnaryOperator.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.operators; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.LongUnaryOperator; 6 | 7 | /** 8 | * A throwing {@link LongUnaryOperator} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingLongUnaryOperator 12 | extends LongUnaryOperator 13 | { 14 | long doApplyAsLong(long operand) 15 | throws Throwable; 16 | 17 | @Override 18 | default long applyAsLong(long operand) 19 | { 20 | try { 21 | return doApplyAsLong(operand); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable tooBad) { 25 | throw new ThrownByLambdaException(tooBad); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/operators/ThrowingUnaryOperator.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.operators; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.UnaryOperator; 6 | 7 | /** 8 | * A throwing {@link UnaryOperator} 9 | * 10 | * @param type parameter of the argument and returning type of this unary 11 | * operator 12 | */ 13 | @FunctionalInterface 14 | public interface ThrowingUnaryOperator 15 | extends UnaryOperator 16 | { 17 | T doApply(T t) 18 | throws Throwable; 19 | 20 | @Override 21 | default T apply(T t) 22 | { 23 | try { 24 | return doApply(t); 25 | } catch (Error | RuntimeException e) { 26 | throw e; 27 | } catch (Throwable tooBad) { 28 | throw new ThrownByLambdaException(tooBad); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/functions/operators/UnaryOperatorChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.operators; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.UnaryOperator; 6 | 7 | public class UnaryOperatorChainer 8 | extends Chainer, ThrowingUnaryOperator, UnaryOperatorChainer> 9 | implements ThrowingUnaryOperator 10 | { 11 | public UnaryOperatorChainer( 12 | final ThrowingUnaryOperator throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public T doApply(final T t) 19 | throws Throwable 20 | { 21 | return throwing.doApply(t); 22 | } 23 | 24 | @Override 25 | public UnaryOperatorChainer orTryWith(final ThrowingUnaryOperator other) 26 | { 27 | final ThrowingUnaryOperator unaryOperator = t -> { 28 | try { 29 | return throwing.doApply(t); 30 | } catch (Error | RuntimeException e) { 31 | throw e; 32 | } catch (Throwable ignored) { 33 | return other.doApply(t); 34 | } 35 | }; 36 | 37 | return new UnaryOperatorChainer<>(unaryOperator); 38 | } 39 | 40 | @Override 41 | public ThrowingUnaryOperator orThrow( 42 | final Class exclass) 43 | { 44 | return t -> { 45 | try { 46 | return throwing.doApply(t); 47 | } catch (Error | RuntimeException e) { 48 | throw e; 49 | } catch (Throwable throwable) { 50 | throw rethrow(exclass, throwable); 51 | } 52 | }; 53 | } 54 | 55 | @Override 56 | public UnaryOperator fallbackTo(final UnaryOperator fallback) 57 | { 58 | return t -> { 59 | try { 60 | return throwing.doApply(t); 61 | } catch (Error | RuntimeException e) { 62 | throw e; 63 | } catch (Throwable ignored) { 64 | return fallback.apply(t); 65 | } 66 | }; 67 | } 68 | 69 | @Override 70 | public UnaryOperator sneakyThrow() 71 | { 72 | return t -> { 73 | try { 74 | return throwing.doApply(t); 75 | } catch (Error | RuntimeException e) { 76 | throw e; 77 | } catch (Throwable throwable) { 78 | throw doSneakyThrow(throwable); 79 | } 80 | }; 81 | } 82 | 83 | public UnaryOperator orReturn(final T retval) 84 | { 85 | return t -> { 86 | try { 87 | return throwing.doApply(t); 88 | } catch (Error | RuntimeException e) { 89 | throw e; 90 | } catch (Throwable ignored) { 91 | return retval; 92 | } 93 | }; 94 | } 95 | 96 | public UnaryOperator orReturnSelf() 97 | { 98 | return t -> { 99 | try { 100 | return throwing.doApply(t); 101 | } catch (Error | RuntimeException e) { 102 | throw e; 103 | } catch (Throwable ignored) { 104 | return t; 105 | } 106 | }; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/predicates/DoublePredicateChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.predicates; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.DoublePredicate; 6 | 7 | public class DoublePredicateChainer 8 | extends Chainer 9 | implements ThrowingDoublePredicate 10 | { 11 | public DoublePredicateChainer( 12 | final ThrowingDoublePredicate throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public boolean doTest(final double value) 19 | throws Throwable 20 | { 21 | return throwing.doTest(value); 22 | } 23 | 24 | @Override 25 | public DoublePredicateChainer orTryWith(final ThrowingDoublePredicate other) 26 | { 27 | final ThrowingDoublePredicate doublePredicate = value -> { 28 | try { 29 | return throwing.doTest(value); 30 | } catch (Error | RuntimeException e) { 31 | throw e; 32 | } catch (Throwable ignored) { 33 | return other.doTest(value); 34 | } 35 | }; 36 | 37 | return new DoublePredicateChainer(doublePredicate); 38 | } 39 | 40 | @Override 41 | public ThrowingDoublePredicate orThrow( 42 | final Class exclass) 43 | { 44 | return value -> { 45 | try { 46 | return throwing.doTest(value); 47 | } catch (Error | RuntimeException e) { 48 | throw e; 49 | } catch (Throwable throwable) { 50 | throw rethrow(exclass, throwable); 51 | } 52 | }; 53 | } 54 | 55 | @Override 56 | public DoublePredicate fallbackTo(final DoublePredicate fallback) 57 | { 58 | return value -> { 59 | try { 60 | return throwing.doTest(value); 61 | } catch (Error | RuntimeException e) { 62 | throw e; 63 | } catch (Throwable ignored) { 64 | return fallback.test(value); 65 | } 66 | }; 67 | } 68 | 69 | @Override 70 | public DoublePredicate sneakyThrow() 71 | { 72 | return value -> { 73 | try { 74 | return throwing.doTest(value); 75 | } catch (Error | RuntimeException e) { 76 | throw e; 77 | } catch (Throwable throwable) { 78 | throw doSneakyThrow(throwable); 79 | } 80 | }; 81 | } 82 | 83 | public DoublePredicate orReturnTrue() 84 | { 85 | return value -> { 86 | try { 87 | return throwing.doTest(value); 88 | } catch (Error | RuntimeException e) { 89 | throw e; 90 | } catch (Throwable ignored) { 91 | return true; 92 | } 93 | }; 94 | } 95 | 96 | public DoublePredicate orReturnFalse() 97 | { 98 | return value -> { 99 | try { 100 | return throwing.doTest(value); 101 | } catch (Error | RuntimeException e) { 102 | throw e; 103 | } catch (Throwable ignored) { 104 | return false; 105 | } 106 | }; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/predicates/IntPredicateChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.predicates; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.IntPredicate; 6 | 7 | public class IntPredicateChainer 8 | extends Chainer 9 | implements ThrowingIntPredicate 10 | { 11 | public IntPredicateChainer( 12 | final ThrowingIntPredicate throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public boolean doTest(final int value) 19 | throws Throwable 20 | { 21 | return throwing.doTest(value); 22 | } 23 | 24 | @Override 25 | public IntPredicateChainer orTryWith(final ThrowingIntPredicate other) 26 | { 27 | final ThrowingIntPredicate intPredicate = value -> { 28 | try { 29 | return throwing.doTest(value); 30 | } catch (Error | RuntimeException e) { 31 | throw e; 32 | } catch (Throwable ignored) { 33 | return other.doTest(value); 34 | } 35 | }; 36 | 37 | return new IntPredicateChainer(intPredicate); 38 | } 39 | 40 | @Override 41 | public ThrowingIntPredicate orThrow( 42 | final Class exclass) 43 | { 44 | return value -> { 45 | try { 46 | return throwing.doTest(value); 47 | } catch (Error | RuntimeException e) { 48 | throw e; 49 | } catch (Throwable throwable) { 50 | throw rethrow(exclass, throwable); 51 | } 52 | }; 53 | } 54 | 55 | @Override 56 | public IntPredicate fallbackTo(final IntPredicate fallback) 57 | { 58 | return value -> { 59 | try { 60 | return throwing.doTest(value); 61 | } catch (Error | RuntimeException e) { 62 | throw e; 63 | } catch (Throwable ignored) { 64 | return fallback.test(value); 65 | } 66 | }; 67 | } 68 | 69 | @Override 70 | public IntPredicate sneakyThrow() 71 | { 72 | return value -> { 73 | try { 74 | return throwing.doTest(value); 75 | } catch (Error | RuntimeException e) { 76 | throw e; 77 | } catch (Throwable throwable) { 78 | throw doSneakyThrow(throwable); 79 | } 80 | }; 81 | } 82 | 83 | public IntPredicate orReturnTrue() 84 | { 85 | return value -> { 86 | try { 87 | return throwing.doTest(value); 88 | } catch (Error | RuntimeException e) { 89 | throw e; 90 | } catch (Throwable ignored) { 91 | return true; 92 | } 93 | }; 94 | } 95 | 96 | public IntPredicate orReturnFalse() 97 | { 98 | return value -> { 99 | try { 100 | return throwing.doTest(value); 101 | } catch (Error | RuntimeException e) { 102 | throw e; 103 | } catch (Throwable ignored) { 104 | return false; 105 | } 106 | }; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/predicates/LongPredicateChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.predicates; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.LongPredicate; 6 | 7 | public class LongPredicateChainer 8 | extends Chainer 9 | implements ThrowingLongPredicate 10 | { 11 | public LongPredicateChainer( 12 | final ThrowingLongPredicate throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public boolean doTest(final long value) 19 | throws Throwable 20 | { 21 | return throwing.doTest(value); 22 | } 23 | 24 | @Override 25 | public LongPredicateChainer orTryWith(final ThrowingLongPredicate other) 26 | { 27 | final ThrowingLongPredicate longPredicate = value -> { 28 | try { 29 | return throwing.doTest(value); 30 | } catch (Error | RuntimeException e) { 31 | throw e; 32 | } catch (Throwable ignored) { 33 | return other.doTest(value); 34 | } 35 | }; 36 | 37 | return new LongPredicateChainer(longPredicate); 38 | } 39 | 40 | @Override 41 | public ThrowingLongPredicate orThrow( 42 | final Class exclass) 43 | { 44 | return value -> { 45 | try { 46 | return throwing.doTest(value); 47 | } catch (Error | RuntimeException e) { 48 | throw e; 49 | } catch (Throwable throwable) { 50 | throw rethrow(exclass, throwable); 51 | } 52 | }; 53 | } 54 | 55 | @Override 56 | public LongPredicate fallbackTo(final LongPredicate fallback) 57 | { 58 | return value -> { 59 | try { 60 | return throwing.doTest(value); 61 | } catch (Error | RuntimeException e) { 62 | throw e; 63 | } catch (Throwable ignored) { 64 | return fallback.test(value); 65 | } 66 | }; 67 | } 68 | 69 | @Override 70 | public LongPredicate sneakyThrow() 71 | { 72 | return value -> { 73 | try { 74 | return throwing.doTest(value); 75 | } catch (Error | RuntimeException e) { 76 | throw e; 77 | } catch (Throwable throwable) { 78 | throw doSneakyThrow(throwable); 79 | } 80 | }; 81 | } 82 | 83 | public LongPredicate orReturnTrue() 84 | { 85 | return value -> { 86 | try { 87 | return throwing.doTest(value); 88 | } catch (Error | RuntimeException e) { 89 | throw e; 90 | } catch (Throwable ignored) { 91 | return true; 92 | } 93 | }; 94 | } 95 | 96 | public LongPredicate orReturnFalse() 97 | { 98 | return value -> { 99 | try { 100 | return throwing.doTest(value); 101 | } catch (Error | RuntimeException e) { 102 | throw e; 103 | } catch (Throwable ignored) { 104 | return false; 105 | } 106 | }; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/predicates/PredicateChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.predicates; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.Predicate; 6 | 7 | public class PredicateChainer 8 | extends Chainer, ThrowingPredicate, PredicateChainer> 9 | implements ThrowingPredicate 10 | { 11 | public PredicateChainer( 12 | final ThrowingPredicate throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public boolean doTest(final T t) 19 | throws Throwable 20 | { 21 | return throwing.doTest(t); 22 | } 23 | 24 | @Override 25 | public PredicateChainer orTryWith(final ThrowingPredicate other) 26 | { 27 | final ThrowingPredicate predicate = t -> { 28 | try { 29 | return throwing.doTest(t); 30 | } catch (Error | RuntimeException e) { 31 | throw e; 32 | } catch (Throwable ignored) { 33 | return other.doTest(t); 34 | } 35 | }; 36 | 37 | return new PredicateChainer<>(predicate); 38 | } 39 | 40 | @Override 41 | public ThrowingPredicate orThrow( 42 | final Class exclass) 43 | { 44 | return t -> { 45 | try { 46 | return throwing.doTest(t); 47 | } catch (Error | RuntimeException e) { 48 | throw e; 49 | } catch (Throwable throwable) { 50 | throw rethrow(exclass, throwable); 51 | } 52 | }; 53 | } 54 | 55 | @Override 56 | public Predicate fallbackTo(final Predicate fallback) 57 | { 58 | return t -> { 59 | try { 60 | return throwing.doTest(t); 61 | } catch (Error | RuntimeException e) { 62 | throw e; 63 | } catch (Throwable ignored) { 64 | return fallback.test(t); 65 | } 66 | }; 67 | } 68 | 69 | @Override 70 | public Predicate sneakyThrow() 71 | { 72 | return t -> { 73 | try { 74 | return throwing.doTest(t); 75 | } catch (Error | RuntimeException e) { 76 | throw e; 77 | } catch (Throwable throwable) { 78 | throw doSneakyThrow(throwable); 79 | } 80 | }; 81 | } 82 | 83 | public Predicate orReturnTrue() 84 | { 85 | return t -> { 86 | try { 87 | return throwing.doTest(t); 88 | } catch (Error | RuntimeException e) { 89 | throw e; 90 | } catch (Throwable ignored) { 91 | return true; 92 | } 93 | }; 94 | } 95 | 96 | public Predicate orReturnFalse() 97 | { 98 | return t -> { 99 | try { 100 | return throwing.doTest(t); 101 | } catch (Error | RuntimeException e) { 102 | throw e; 103 | } catch (Throwable ignored) { 104 | return false; 105 | } 106 | }; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/predicates/ThrowingDoublePredicate.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.predicates; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.DoublePredicate; 6 | 7 | /** 8 | * A throwing {@link DoublePredicate} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingDoublePredicate 12 | extends DoublePredicate 13 | { 14 | boolean doTest(double value) 15 | throws Throwable; 16 | 17 | @Override 18 | default boolean test(double value) 19 | { 20 | try { 21 | return doTest(value); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable throwable) { 25 | throw new ThrownByLambdaException(throwable); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/predicates/ThrowingIntPredicate.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.predicates; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.IntPredicate; 6 | 7 | /** 8 | * A throwing {@link IntPredicate} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingIntPredicate 12 | extends IntPredicate 13 | { 14 | boolean doTest(int value) 15 | throws Throwable; 16 | 17 | @Override 18 | default boolean test(int value) 19 | { 20 | try { 21 | return doTest(value); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable throwable) { 25 | throw new ThrownByLambdaException(throwable); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/predicates/ThrowingLongPredicate.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.predicates; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.LongPredicate; 6 | 7 | /** 8 | * A throwing {@link LongPredicate} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingLongPredicate 12 | extends LongPredicate 13 | { 14 | boolean doTest(long value) 15 | throws Throwable; 16 | 17 | @Override 18 | default boolean test(long value) 19 | { 20 | try { 21 | return doTest(value); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable tooBad) { 25 | throw new ThrownByLambdaException(tooBad); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/predicates/ThrowingPredicate.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.predicates; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.Predicate; 6 | 7 | /** 8 | * A throwing {@link Predicate} 9 | * 10 | * @param type parameter of the argument to this predicate 11 | */ 12 | @FunctionalInterface 13 | public interface ThrowingPredicate 14 | extends Predicate 15 | { 16 | boolean doTest(T t) 17 | throws Throwable; 18 | 19 | @Override 20 | default boolean test(T t) 21 | { 22 | try { 23 | return doTest(t); 24 | } catch (Error | RuntimeException e) { 25 | throw e; 26 | } catch (Throwable throwable) { 27 | throw new ThrownByLambdaException(throwable); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/runnable/RunnableChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.runnable; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | public class RunnableChainer 6 | extends Chainer 7 | implements ThrowingRunnable 8 | { 9 | public RunnableChainer(final ThrowingRunnable throwing) 10 | { 11 | super(throwing); 12 | } 13 | 14 | @Override 15 | public void doRun() 16 | throws Throwable 17 | { 18 | throwing.doRun(); 19 | } 20 | 21 | @Override 22 | public RunnableChainer orTryWith(final ThrowingRunnable other) 23 | { 24 | final ThrowingRunnable runnable = () -> { 25 | try { 26 | throwing.doRun(); 27 | } catch (Error | RuntimeException e) { 28 | throw e; 29 | } catch (Throwable ignored) { 30 | other.doRun(); 31 | } 32 | }; 33 | 34 | return new RunnableChainer(runnable); 35 | } 36 | 37 | @Override 38 | public ThrowingRunnable orThrow( 39 | final Class exclass) 40 | { 41 | return () -> { 42 | try { 43 | throwing.doRun(); 44 | } catch (Error | RuntimeException e) { 45 | throw e; 46 | } catch (Throwable throwable) { 47 | throw rethrow(exclass, throwable); 48 | } 49 | }; 50 | } 51 | 52 | @Override 53 | public Runnable fallbackTo(final Runnable fallback) 54 | { 55 | return () -> { 56 | try { 57 | throwing.doRun(); 58 | } catch (Error | RuntimeException e) { 59 | throw e; 60 | } catch (Throwable ignored) { 61 | fallback.run(); 62 | } 63 | }; 64 | } 65 | 66 | @Override 67 | public Runnable sneakyThrow() 68 | { 69 | return () -> { 70 | try { 71 | throwing.doRun(); 72 | } catch (Error | RuntimeException e) { 73 | throw e; 74 | } catch (Throwable throwable) { 75 | throw doSneakyThrow(throwable); 76 | } 77 | }; 78 | } 79 | 80 | public Runnable orDoNothing() 81 | { 82 | return () -> { 83 | try { 84 | throwing.doRun(); 85 | } catch (Error | RuntimeException e) { 86 | throw e; 87 | } catch (Throwable ignored) { 88 | // nothing 89 | } 90 | }; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/runnable/ThrowingRunnable.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.runnable; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | @FunctionalInterface 6 | public interface ThrowingRunnable 7 | extends Runnable 8 | { 9 | void doRun() 10 | throws Throwable; 11 | 12 | @Override 13 | default void run() 14 | { 15 | try { 16 | doRun(); 17 | } catch (Error | RuntimeException e) { 18 | throw e; 19 | } catch (Throwable throwable) { 20 | throw new ThrownByLambdaException(throwable); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/supplier/DoubleSupplierChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.supplier; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.DoubleSupplier; 6 | 7 | public class DoubleSupplierChainer 8 | extends Chainer 9 | implements ThrowingDoubleSupplier 10 | { 11 | public DoubleSupplierChainer(final ThrowingDoubleSupplier throwing) 12 | { 13 | super(throwing); 14 | } 15 | 16 | @Override 17 | public double doGetAsDouble() 18 | throws Throwable 19 | { 20 | return throwing.doGetAsDouble(); 21 | } 22 | 23 | @Override 24 | public DoubleSupplierChainer orTryWith(final ThrowingDoubleSupplier other) 25 | { 26 | final ThrowingDoubleSupplier doubleSupplier = () -> { 27 | try { 28 | return throwing.doGetAsDouble(); 29 | } catch (Error | RuntimeException e) { 30 | throw e; 31 | } catch (Throwable ignored) { 32 | return other.doGetAsDouble(); 33 | } 34 | }; 35 | 36 | return new DoubleSupplierChainer(doubleSupplier); 37 | } 38 | 39 | @Override 40 | public ThrowingDoubleSupplier orThrow( 41 | final Class exclass) 42 | { 43 | return () -> { 44 | try { 45 | return throwing.doGetAsDouble(); 46 | } catch (Error | RuntimeException e) { 47 | throw e; 48 | } catch (Throwable throwable) { 49 | throw rethrow(exclass, throwable); 50 | } 51 | }; 52 | } 53 | 54 | @Override 55 | public DoubleSupplier fallbackTo(final DoubleSupplier fallback) 56 | { 57 | return () -> { 58 | try { 59 | return throwing.doGetAsDouble(); 60 | } catch (Error | RuntimeException e) { 61 | throw e; 62 | } catch (Throwable ignored) { 63 | return fallback.getAsDouble(); 64 | } 65 | }; 66 | } 67 | 68 | @Override 69 | public DoubleSupplier sneakyThrow() 70 | { 71 | return () -> { 72 | try { 73 | return throwing.doGetAsDouble(); 74 | } catch (Error | RuntimeException e) { 75 | throw e; 76 | } catch (Throwable throwable) { 77 | throw doSneakyThrow(throwable); 78 | } 79 | }; 80 | } 81 | 82 | public DoubleSupplier orReturn(final double retval) 83 | { 84 | return () -> { 85 | try { 86 | return throwing.doGetAsDouble(); 87 | } catch (Error | RuntimeException e) { 88 | throw e; 89 | } catch (Throwable ignored) { 90 | return retval; 91 | } 92 | }; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/supplier/IntSupplierChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.supplier; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.IntSupplier; 6 | 7 | public class IntSupplierChainer 8 | extends Chainer 9 | implements ThrowingIntSupplier 10 | { 11 | public IntSupplierChainer(final ThrowingIntSupplier throwing) 12 | { 13 | super(throwing); 14 | } 15 | 16 | @Override 17 | public int doGetAsInt() 18 | throws Throwable 19 | { 20 | return throwing.doGetAsInt(); 21 | } 22 | 23 | @Override 24 | public IntSupplierChainer orTryWith(final ThrowingIntSupplier other) 25 | { 26 | final ThrowingIntSupplier intSupplier = () -> { 27 | try { 28 | return throwing.doGetAsInt(); 29 | } catch (Error | RuntimeException e) { 30 | throw e; 31 | } catch (Throwable ignored) { 32 | return other.doGetAsInt(); 33 | } 34 | }; 35 | 36 | return new IntSupplierChainer(intSupplier); 37 | } 38 | 39 | @Override 40 | public ThrowingIntSupplier orThrow( 41 | final Class exclass) 42 | { 43 | return () -> { 44 | try { 45 | return throwing.doGetAsInt(); 46 | } catch (Error | RuntimeException e) { 47 | throw e; 48 | } catch (Throwable throwable) { 49 | throw rethrow(exclass, throwable); 50 | } 51 | }; 52 | } 53 | 54 | @Override 55 | public IntSupplier fallbackTo(final IntSupplier fallback) 56 | { 57 | return () -> { 58 | try { 59 | return throwing.doGetAsInt(); 60 | } catch (Error | RuntimeException e) { 61 | throw e; 62 | } catch (Throwable ignored) { 63 | return fallback.getAsInt(); 64 | } 65 | }; 66 | } 67 | 68 | @Override 69 | public IntSupplier sneakyThrow() 70 | { 71 | return () -> { 72 | try { 73 | return throwing.doGetAsInt(); 74 | } catch (Error | RuntimeException e) { 75 | throw e; 76 | } catch (Throwable throwable) { 77 | throw doSneakyThrow(throwable); 78 | } 79 | }; 80 | } 81 | 82 | public IntSupplier orReturn(final int retval) 83 | { 84 | return () -> { 85 | try { 86 | return throwing.doGetAsInt(); 87 | } catch (Error | RuntimeException e) { 88 | throw e; 89 | } catch (Throwable ignored) { 90 | return retval; 91 | } 92 | }; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/supplier/LongSupplierChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.supplier; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.LongSupplier; 6 | 7 | public class LongSupplierChainer 8 | extends Chainer 9 | implements ThrowingLongSupplier 10 | { 11 | public LongSupplierChainer(final ThrowingLongSupplier throwing) 12 | { 13 | super(throwing); 14 | } 15 | 16 | @Override 17 | public long doGetAsLong() 18 | throws Throwable 19 | { 20 | return throwing.doGetAsLong(); 21 | } 22 | 23 | @Override 24 | public LongSupplierChainer orTryWith(final ThrowingLongSupplier other) 25 | { 26 | final ThrowingLongSupplier longSupplier = () -> { 27 | try { 28 | return throwing.doGetAsLong(); 29 | } catch (Error | RuntimeException e) { 30 | throw e; 31 | } catch (Throwable ignored) { 32 | return other.doGetAsLong(); 33 | } 34 | }; 35 | 36 | return new LongSupplierChainer(longSupplier); 37 | } 38 | 39 | @Override 40 | public ThrowingLongSupplier orThrow( 41 | final Class exclass) 42 | { 43 | return () -> { 44 | try { 45 | return throwing.doGetAsLong(); 46 | } catch (Error | RuntimeException e) { 47 | throw e; 48 | } catch (Throwable throwable) { 49 | throw rethrow(exclass, throwable); 50 | } 51 | }; 52 | } 53 | 54 | @Override 55 | public LongSupplier fallbackTo(final LongSupplier fallback) 56 | { 57 | return () -> { 58 | try { 59 | return throwing.doGetAsLong(); 60 | } catch (Error | RuntimeException e) { 61 | throw e; 62 | } catch (Throwable ignored) { 63 | return fallback.getAsLong(); 64 | } 65 | }; 66 | } 67 | 68 | @Override 69 | public LongSupplier sneakyThrow() 70 | { 71 | return () -> { 72 | try { 73 | return throwing.doGetAsLong(); 74 | } catch (Error | RuntimeException e) { 75 | throw e; 76 | } catch (Throwable throwable) { 77 | throw doSneakyThrow(throwable); 78 | } 79 | }; 80 | } 81 | 82 | public LongSupplier orReturn(final long retval) 83 | { 84 | return () -> { 85 | try { 86 | return throwing.doGetAsLong(); 87 | } catch (Error | RuntimeException e) { 88 | throw e; 89 | } catch (Throwable ignored) { 90 | return retval; 91 | } 92 | }; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/supplier/SupplierChainer.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.supplier; 2 | 3 | import com.github.fge.lambdas.Chainer; 4 | 5 | import java.util.function.Supplier; 6 | 7 | public class SupplierChainer 8 | extends Chainer, ThrowingSupplier, SupplierChainer> 9 | implements ThrowingSupplier 10 | { 11 | public SupplierChainer( 12 | final ThrowingSupplier throwing) 13 | { 14 | super(throwing); 15 | } 16 | 17 | @Override 18 | public T doGet() 19 | throws Throwable 20 | { 21 | return throwing.doGet(); 22 | } 23 | 24 | @Override 25 | public SupplierChainer orTryWith(final ThrowingSupplier other) 26 | { 27 | final ThrowingSupplier supplier = () -> { 28 | try { 29 | return throwing.doGet(); 30 | } catch (Error | RuntimeException e) { 31 | throw e; 32 | } catch (Throwable ignored) { 33 | return other.doGet(); 34 | } 35 | }; 36 | 37 | return new SupplierChainer<>(supplier); 38 | } 39 | 40 | @Override 41 | public ThrowingSupplier orThrow( 42 | final Class exclass) 43 | { 44 | return () -> { 45 | try { 46 | return throwing.doGet(); 47 | } catch (Error | RuntimeException e) { 48 | throw e; 49 | } catch (Throwable throwable) { 50 | throw rethrow(exclass, throwable); 51 | } 52 | }; 53 | } 54 | 55 | @Override 56 | public Supplier fallbackTo(final Supplier fallback) 57 | { 58 | return () -> { 59 | try { 60 | return throwing.doGet(); 61 | } catch (Error | RuntimeException e) { 62 | throw e; 63 | } catch (Throwable ignored) { 64 | return fallback.get(); 65 | } 66 | }; 67 | } 68 | 69 | @Override 70 | public Supplier sneakyThrow() 71 | { 72 | return () -> { 73 | try { 74 | return throwing.doGet(); 75 | } catch (Error | RuntimeException e) { 76 | throw e; 77 | } catch (Throwable throwable) { 78 | throw doSneakyThrow(throwable); 79 | } 80 | }; 81 | } 82 | 83 | public Supplier orReturn(final T retval) 84 | { 85 | return () -> { 86 | try { 87 | return throwing.doGet(); 88 | } catch (Error | RuntimeException e) { 89 | throw e; 90 | } catch (Throwable ignored) { 91 | return retval; 92 | } 93 | }; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/supplier/ThrowingDoubleSupplier.java: -------------------------------------------------------------------------------- 1 | 2 | package com.github.fge.lambdas.supplier; 3 | 4 | import com.github.fge.lambdas.ThrownByLambdaException; 5 | 6 | import java.util.function.DoubleSupplier; 7 | 8 | /** 9 | * A throwing {@link DoubleSupplier} 10 | */ 11 | @FunctionalInterface 12 | public interface ThrowingDoubleSupplier 13 | extends DoubleSupplier 14 | { 15 | double doGetAsDouble() 16 | throws Throwable; 17 | 18 | @Override 19 | default double getAsDouble() 20 | { 21 | try { 22 | return doGetAsDouble(); 23 | } catch (Error | RuntimeException e) { 24 | throw e; 25 | } catch (Throwable throwable) { 26 | throw new ThrownByLambdaException(throwable); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/supplier/ThrowingIntSupplier.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.supplier; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.IntSupplier; 6 | 7 | /** 8 | * A throwing {@link IntSupplier} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingIntSupplier 12 | extends IntSupplier 13 | { 14 | int doGetAsInt() 15 | throws Throwable; 16 | 17 | @Override 18 | default int getAsInt() 19 | { 20 | try { 21 | return doGetAsInt(); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable tooBad) { 25 | throw new ThrownByLambdaException(tooBad); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/supplier/ThrowingLongSupplier.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.supplier; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.LongSupplier; 6 | 7 | /** 8 | * A throwing {@link LongSupplier} 9 | */ 10 | @FunctionalInterface 11 | public interface ThrowingLongSupplier 12 | extends LongSupplier 13 | { 14 | long doGetAsLong() 15 | throws Throwable; 16 | 17 | @Override 18 | default long getAsLong() 19 | { 20 | try { 21 | return doGetAsLong(); 22 | } catch (Error | RuntimeException e) { 23 | throw e; 24 | } catch (Throwable tooBad) { 25 | throw new ThrownByLambdaException(tooBad); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/fge/lambdas/supplier/ThrowingSupplier.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.supplier; 2 | 3 | import com.github.fge.lambdas.ThrownByLambdaException; 4 | 5 | import java.util.function.Supplier; 6 | 7 | /** 8 | * A throwing {@link Supplier} 9 | * 10 | * @param type parameter of the return value of this supplier 11 | */ 12 | @FunctionalInterface 13 | public interface ThrowingSupplier 14 | extends Supplier 15 | { 16 | T doGet() 17 | throws Throwable; 18 | 19 | @Override 20 | default T get() 21 | { 22 | try { 23 | return doGet(); 24 | } catch (Error | RuntimeException e) { 25 | throw e; 26 | } catch (Throwable tooBad) { 27 | throw new ThrownByLambdaException(tooBad); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/LICENSE: -------------------------------------------------------------------------------- 1 | This software is dual-licensed under: 2 | 3 | - the Lesser General Public License (LGPL) version 3.0 or, at your option, any 4 | later version; 5 | - the Apache Software License (ASL) version 2.0. 6 | 7 | The text of both licenses is included (under the names LGPL-3.0.txt and 8 | ASL-2.0.txt respectively). 9 | 10 | Direct link to the sources: 11 | 12 | - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt 13 | - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt 14 | 15 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/ChainerRethrowTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas; 2 | 3 | import org.testng.annotations.DataProvider; 4 | import org.testng.annotations.Test; 5 | 6 | import java.util.ArrayList; 7 | import java.util.Iterator; 8 | import java.util.List; 9 | 10 | import static com.github.fge.lambdas.helpers.CustomAssertions.shouldHaveThrown; 11 | import static org.assertj.core.api.Assertions.assertThat; 12 | 13 | public final class ChainerRethrowTest 14 | { 15 | private static final Throwable THROWABLE = new Throwable(); 16 | 17 | /* 18 | * Case 1: constructor is there but not public 19 | */ 20 | private static final class PrivateException 21 | extends RuntimeException 22 | { 23 | PrivateException(final Throwable cause) 24 | { 25 | super(cause); 26 | } 27 | } 28 | 29 | /* 30 | * Case 2: class is public, but no constructor 31 | */ 32 | public static final class NoConstructorException 33 | extends RuntimeException 34 | { 35 | public NoConstructorException() 36 | { 37 | } 38 | } 39 | 40 | /* 41 | * Case 3: class is public constructor is there, but it throws a checked 42 | * exception 43 | */ 44 | public static final class ThrowingConstructorException 45 | extends RuntimeException 46 | { 47 | public ThrowingConstructorException(final Throwable cause) 48 | throws Exception 49 | { 50 | throw new Exception(); 51 | } 52 | } 53 | 54 | @DataProvider 55 | public Iterator brokenThrowables() 56 | { 57 | final List list = new ArrayList<>(); 58 | 59 | list.add(new Object[] { PrivateException.class }); 60 | list.add(new Object[] { NoConstructorException.class }); 61 | list.add(new Object[] { ThrowingConstructorException.class }); 62 | 63 | return list.iterator(); 64 | } 65 | 66 | @Test(dataProvider = "brokenThrowables") 67 | public void instantiationFailsWithAppropriateException( 68 | final Class c) 69 | { 70 | try { 71 | Chainer.rethrow(c, THROWABLE); 72 | shouldHaveThrown(Chainer.InstantiationException.class); 73 | } catch (Chainer.InstantiationException e) { 74 | final Throwable[] suppressed = e.getSuppressed(); 75 | assertThat(suppressed).hasSize(1); 76 | assertThat(suppressed[0]).isSameAs(THROWABLE); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/comparators/ComparatorChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.comparators; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import com.github.fge.lambdas.helpers.Type1; 6 | import org.testng.annotations.Test; 7 | 8 | import java.util.Comparator; 9 | import java.util.concurrent.Callable; 10 | 11 | import static org.assertj.core.api.Assertions.assertThat; 12 | import static org.mockito.Mockito.mock; 13 | import static org.mockito.Mockito.when; 14 | 15 | public final class ComparatorChainerTest 16 | extends ChainerTest, ThrowingComparator, ComparatorChainer, Integer> 17 | { 18 | private final Type1 o1 = Type1.mock(); 19 | private final Type1 o2 = Type1.mock(); 20 | 21 | public ComparatorChainerTest() 22 | { 23 | super(42, 24); 24 | } 25 | 26 | @Override 27 | protected Comparator getFallback() 28 | { 29 | return mock(Comparator.class); 30 | } 31 | 32 | @Override 33 | protected ThrowingComparator getThrowing() 34 | { 35 | return mock(ThrowingComparator.class); 36 | } 37 | 38 | @Override 39 | protected ComparatorChainer getChain( 40 | final ThrowingComparator throwing) 41 | { 42 | return Throwing.comparator(throwing); 43 | } 44 | 45 | @Override 46 | protected Callable toCallable(final Comparator chain) 47 | { 48 | return () -> chain.compare(o1, o2); 49 | } 50 | 51 | @Override 52 | protected void configureFull(final ThrowingComparator throwing) 53 | throws Throwable 54 | { 55 | when(throwing.doCompare(o1, o2)) 56 | .thenReturn(ret1) 57 | .thenThrow(checked) 58 | .thenThrow(unchecked) 59 | .thenThrow(error); 60 | } 61 | 62 | @Override 63 | protected void configureAlternate(final ThrowingComparator throwing) 64 | throws Throwable 65 | { 66 | when(throwing.doCompare(o1, o2)) 67 | .thenReturn(ret2); 68 | } 69 | 70 | @Override 71 | protected void configureFallback(final Comparator fallback) 72 | { 73 | when(fallback.compare(o1, o2)) 74 | .thenReturn(ret2); 75 | } 76 | 77 | @Test 78 | public void orReturnTest() 79 | throws Throwable 80 | { 81 | final ThrowingComparator throwing = getThrowing(); 82 | configureFull(throwing); 83 | 84 | final Comparator comparator = getChain(throwing).orReturn(ret2); 85 | 86 | final Callable callable = toCallable(comparator); 87 | 88 | assertThat(callable.call()).isSameAs(ret1); 89 | 90 | assertThat(callable.call()).isSameAs(ret2); 91 | 92 | verifyUncheckedThrow(callable); 93 | 94 | verifyErrorThrow(callable); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/consumers/DoubleConsumerChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import com.github.fge.lambdas.helpers.Type1; 6 | import org.testng.annotations.BeforeMethod; 7 | import org.testng.annotations.Test; 8 | 9 | import java.util.concurrent.Callable; 10 | import java.util.concurrent.atomic.AtomicReference; 11 | import java.util.function.DoubleConsumer; 12 | 13 | import static org.assertj.core.api.Assertions.assertThat; 14 | import static org.mockito.Mockito.doAnswer; 15 | import static org.mockito.Mockito.mock; 16 | 17 | public final class DoubleConsumerChainerTest 18 | extends ChainerTest 19 | { 20 | private final double value = 42.0; 21 | 22 | private final Type1 noValue = Type1.mock(); 23 | 24 | private final AtomicReference sentinel = new AtomicReference<>(); 25 | 26 | public DoubleConsumerChainerTest() 27 | { 28 | super(Type1.mock(), Type1.mock()); 29 | } 30 | 31 | @BeforeMethod 32 | public void initSentinel() 33 | { 34 | sentinel.set(noValue); 35 | } 36 | 37 | @Override 38 | protected DoubleConsumer getFallback() 39 | { 40 | return mock(DoubleConsumer.class); 41 | } 42 | 43 | @Override 44 | protected ThrowingDoubleConsumer getThrowing() 45 | { 46 | return mock(ThrowingDoubleConsumer.class); 47 | } 48 | 49 | @Override 50 | protected DoubleConsumerChainer getChain( 51 | final ThrowingDoubleConsumer throwing) 52 | { 53 | return Throwing.doubleConsumer(throwing); 54 | } 55 | 56 | @Override 57 | protected Callable toCallable(final DoubleConsumer chain) 58 | { 59 | return () -> { chain.accept(value); return sentinel.get(); }; 60 | } 61 | 62 | @Override 63 | protected void configureFull(final ThrowingDoubleConsumer throwing) 64 | throws Throwable 65 | { 66 | doAnswer(ignored -> { sentinel.set(ret1); return null; }) 67 | .doThrow(checked) 68 | .doThrow(unchecked) 69 | .doThrow(error) 70 | .when(throwing).doAccept(value); 71 | } 72 | 73 | @Override 74 | protected void configureAlternate(final ThrowingDoubleConsumer throwing) 75 | throws Throwable 76 | { 77 | doAnswer(ignored -> { sentinel.set(ret2); return null; }) 78 | .when(throwing).doAccept(value); 79 | } 80 | 81 | @Override 82 | protected void configureFallback(final DoubleConsumer fallback) 83 | { 84 | doAnswer(ignored -> { sentinel.set(ret2); return null; }) 85 | .when(fallback).accept(value); 86 | } 87 | 88 | @Test 89 | public void orDoNothingTest() 90 | throws Throwable 91 | { 92 | final ThrowingDoubleConsumer throwing = getThrowing(); 93 | configureFull(throwing); 94 | 95 | final DoubleConsumer chain = getChain(throwing).orDoNothing(); 96 | 97 | final Callable callable = toCallable(chain); 98 | 99 | assertThat(callable.call()).isSameAs(ret1); 100 | 101 | assertThat(callable.call()).isSameAs(ret1); 102 | 103 | verifyUncheckedThrow(callable); 104 | 105 | verifyErrorThrow(callable); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/consumers/IntConsumerChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import com.github.fge.lambdas.helpers.Type1; 6 | import org.testng.annotations.BeforeMethod; 7 | import org.testng.annotations.Test; 8 | 9 | import java.util.concurrent.Callable; 10 | import java.util.concurrent.atomic.AtomicReference; 11 | import java.util.function.IntConsumer; 12 | 13 | import static org.assertj.core.api.Assertions.assertThat; 14 | import static org.mockito.Mockito.doAnswer; 15 | import static org.mockito.Mockito.mock; 16 | 17 | public final class IntConsumerChainerTest 18 | extends ChainerTest 19 | { 20 | private final int value = 42; 21 | 22 | private final Type1 noValue = Type1.mock(); 23 | 24 | private final AtomicReference sentinel = new AtomicReference<>(); 25 | 26 | public IntConsumerChainerTest() 27 | { 28 | super(Type1.mock(), Type1.mock()); 29 | } 30 | 31 | @BeforeMethod 32 | public void initSentinel() 33 | { 34 | sentinel.set(noValue); 35 | } 36 | 37 | @Override 38 | protected IntConsumer getFallback() 39 | { 40 | return mock(IntConsumer.class); 41 | } 42 | 43 | @Override 44 | protected ThrowingIntConsumer getThrowing() 45 | { 46 | return mock(ThrowingIntConsumer.class); 47 | } 48 | 49 | @Override 50 | protected IntConsumerChainer getChain( 51 | final ThrowingIntConsumer throwing) 52 | { 53 | return Throwing.intConsumer(throwing); 54 | } 55 | 56 | @Override 57 | protected Callable toCallable(final IntConsumer chain) 58 | { 59 | return () -> { chain.accept(value); return sentinel.get(); }; 60 | } 61 | 62 | @Override 63 | protected void configureFull(final ThrowingIntConsumer throwing) 64 | throws Throwable 65 | { 66 | doAnswer(ignored -> { sentinel.set(ret1); return null; }) 67 | .doThrow(checked) 68 | .doThrow(unchecked) 69 | .doThrow(error) 70 | .when(throwing).doAccept(value); 71 | } 72 | 73 | @Override 74 | protected void configureAlternate(final ThrowingIntConsumer throwing) 75 | throws Throwable 76 | { 77 | doAnswer(ignored -> { sentinel.set(ret2); return null; }) 78 | .when(throwing).doAccept(value); 79 | } 80 | 81 | @Override 82 | protected void configureFallback(final IntConsumer fallback) 83 | { 84 | doAnswer(ignored -> { sentinel.set(ret2); return null; }) 85 | .when(fallback).accept(value); 86 | } 87 | 88 | @Test 89 | public void orDoNothingTest() 90 | throws Throwable 91 | { 92 | final ThrowingIntConsumer throwing = getThrowing(); 93 | configureFull(throwing); 94 | 95 | final IntConsumer chain = getChain(throwing).orDoNothing(); 96 | 97 | final Callable callable = toCallable(chain); 98 | 99 | assertThat(callable.call()).isSameAs(ret1); 100 | 101 | assertThat(callable.call()).isSameAs(ret1); 102 | 103 | verifyUncheckedThrow(callable); 104 | 105 | verifyErrorThrow(callable); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/consumers/LongConsumerChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.consumers; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import com.github.fge.lambdas.helpers.Type1; 6 | import org.testng.annotations.BeforeMethod; 7 | import org.testng.annotations.Test; 8 | 9 | import java.util.concurrent.Callable; 10 | import java.util.concurrent.atomic.AtomicReference; 11 | import java.util.function.LongConsumer; 12 | 13 | import static org.assertj.core.api.Assertions.assertThat; 14 | import static org.mockito.Mockito.doAnswer; 15 | import static org.mockito.Mockito.mock; 16 | 17 | public final class LongConsumerChainerTest 18 | extends ChainerTest 19 | { 20 | private final long value = 42L; 21 | 22 | private final Type1 noValue = Type1.mock(); 23 | 24 | private final AtomicReference sentinel = new AtomicReference<>(); 25 | 26 | public LongConsumerChainerTest() 27 | { 28 | super(Type1.mock(), Type1.mock()); 29 | } 30 | 31 | @BeforeMethod 32 | public void initSentinel() 33 | { 34 | sentinel.set(noValue); 35 | } 36 | 37 | @Override 38 | protected LongConsumer getFallback() 39 | { 40 | return mock(LongConsumer.class); 41 | } 42 | 43 | @Override 44 | protected ThrowingLongConsumer getThrowing() 45 | { 46 | return mock(ThrowingLongConsumer.class); 47 | } 48 | 49 | @Override 50 | protected LongConsumerChainer getChain( 51 | final ThrowingLongConsumer throwing) 52 | { 53 | return Throwing.longConsumer(throwing); 54 | } 55 | 56 | @Override 57 | protected Callable toCallable(final LongConsumer chain) 58 | { 59 | return () -> { chain.accept(value); return sentinel.get(); }; 60 | } 61 | 62 | @Override 63 | protected void configureFull(final ThrowingLongConsumer throwing) 64 | throws Throwable 65 | { 66 | doAnswer(ignored -> { sentinel.set(ret1); return null; }) 67 | .doThrow(checked) 68 | .doThrow(unchecked) 69 | .doThrow(error) 70 | .when(throwing).doAccept(value); 71 | } 72 | 73 | @Override 74 | protected void configureAlternate(final ThrowingLongConsumer throwing) 75 | throws Throwable 76 | { 77 | doAnswer(ignored -> { sentinel.set(ret2); return null; }) 78 | .when(throwing).doAccept(value); 79 | } 80 | 81 | @Override 82 | protected void configureFallback(final LongConsumer fallback) 83 | { 84 | doAnswer(ignored -> { sentinel.set(ret2); return null; }) 85 | .when(fallback).accept(value); 86 | } 87 | 88 | @Test 89 | public void orDoNothingTest() 90 | throws Throwable 91 | { 92 | final ThrowingLongConsumer throwing = getThrowing(); 93 | configureFull(throwing); 94 | 95 | final LongConsumer chain = getChain(throwing).orDoNothing(); 96 | 97 | final Callable callable = toCallable(chain); 98 | 99 | assertThat(callable.call()).isSameAs(ret1); 100 | 101 | assertThat(callable.call()).isSameAs(ret1); 102 | 103 | verifyUncheckedThrow(callable); 104 | 105 | verifyErrorThrow(callable); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/functions/BiFunctionChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import com.github.fge.lambdas.helpers.Type1; 6 | import com.github.fge.lambdas.helpers.Type2; 7 | import com.github.fge.lambdas.helpers.Type3; 8 | import org.testng.annotations.Test; 9 | 10 | import java.util.concurrent.Callable; 11 | import java.util.function.BiFunction; 12 | 13 | import static org.assertj.core.api.Assertions.assertThat; 14 | import static org.mockito.Mockito.mock; 15 | import static org.mockito.Mockito.when; 16 | 17 | public final class BiFunctionChainerTest 18 | extends ChainerTest, ThrowingBiFunction, BiFunctionChainer, Type3> 19 | { 20 | private final Type1 t = Type1.mock(); 21 | private final Type2 u = Type2.mock(); 22 | 23 | public BiFunctionChainerTest() 24 | { 25 | super(Type3.mock(), Type3.mock()); 26 | } 27 | 28 | @Override 29 | protected BiFunction getFallback() 30 | { 31 | return mock(BiFunction.class); 32 | } 33 | 34 | @Override 35 | protected ThrowingBiFunction getThrowing() 36 | { 37 | return mock(ThrowingBiFunction.class); 38 | } 39 | 40 | @Override 41 | protected BiFunctionChainer getChain( 42 | final ThrowingBiFunction throwing) 43 | { 44 | return Throwing.biFunction(throwing); 45 | } 46 | 47 | @Override 48 | protected Callable toCallable( 49 | final BiFunction chain) 50 | { 51 | return () -> chain.apply(t, u); 52 | } 53 | 54 | @Override 55 | protected void configureFull( 56 | final ThrowingBiFunction throwing) 57 | throws Throwable 58 | { 59 | when(throwing.doApply(t, u)) 60 | .thenReturn(ret1) 61 | .thenThrow(checked) 62 | .thenThrow(unchecked) 63 | .thenThrow(error); 64 | } 65 | 66 | @Override 67 | protected void configureAlternate( 68 | final ThrowingBiFunction throwing) 69 | throws Throwable 70 | { 71 | when(throwing.doApply(t, u)) 72 | .thenReturn(ret2); 73 | } 74 | 75 | @Override 76 | protected void configureFallback( 77 | final BiFunction fallback) 78 | { 79 | when(fallback.apply(t, u)) 80 | .thenReturn(ret2); 81 | } 82 | 83 | @Test 84 | public void orReturnTest() 85 | throws Throwable 86 | { 87 | final ThrowingBiFunction throwing = getThrowing(); 88 | configureFull(throwing); 89 | 90 | final BiFunction chain 91 | = getChain(throwing).orReturn(ret2); 92 | 93 | final Callable callable = toCallable(chain); 94 | 95 | assertThat(callable.call()).isSameAs(ret1); 96 | 97 | assertThat(callable.call()).isSameAs(ret2); 98 | 99 | verifyUncheckedThrow(callable); 100 | 101 | verifyErrorThrow(callable); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/functions/FunctionChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import com.github.fge.lambdas.helpers.Type1; 6 | import com.github.fge.lambdas.helpers.Type2; 7 | import org.testng.annotations.Test; 8 | 9 | import java.util.concurrent.Callable; 10 | import java.util.function.Function; 11 | 12 | import static org.assertj.core.api.Assertions.assertThat; 13 | import static org.mockito.Mockito.mock; 14 | import static org.mockito.Mockito.when; 15 | 16 | public final class FunctionChainerTest 17 | extends ChainerTest, ThrowingFunction, 18 | FunctionChainer, Type2> 19 | { 20 | private final Type1 t = Type1.mock(); 21 | 22 | 23 | public FunctionChainerTest() 24 | throws NoSuchMethodException, IllegalAccessException 25 | { 26 | super(Type2.mock(), Type2.mock()); 27 | } 28 | 29 | @Override 30 | protected Function getFallback() 31 | { 32 | return mock(Function.class); 33 | } 34 | 35 | @Override 36 | protected ThrowingFunction getThrowing() 37 | { 38 | return mock(ThrowingFunction.class); 39 | } 40 | 41 | @Override 42 | protected FunctionChainer getChain( 43 | final ThrowingFunction throwing) 44 | { 45 | return Throwing.function(throwing); 46 | } 47 | 48 | @Override 49 | protected Callable toCallable( 50 | final Function chainer) 51 | { 52 | return () -> chainer.apply(t); 53 | } 54 | 55 | @Override 56 | protected void configureFull(final ThrowingFunction throwing) 57 | throws Throwable 58 | { 59 | when(throwing.doApply(t)).thenReturn(ret1) 60 | .thenThrow(checked) 61 | .thenThrow(unchecked) 62 | .thenThrow(error); 63 | } 64 | 65 | @Override 66 | protected void configureAlternate( 67 | final ThrowingFunction throwing) 68 | throws Throwable 69 | { 70 | when(throwing.doApply(t)).thenReturn(ret2); 71 | } 72 | 73 | @Override 74 | protected void configureFallback(final Function fallback) 75 | { 76 | when(fallback.apply(t)).thenReturn(ret2); 77 | } 78 | 79 | @Test 80 | public void orReturnTest() 81 | throws Throwable 82 | { 83 | final ThrowingFunction throwing = getThrowing(); 84 | configureFull(throwing); 85 | 86 | final Function chain = getChain(throwing) 87 | .orReturn(ret2); 88 | 89 | final Callable callable = toCallable(chain); 90 | 91 | assertThat(callable.call()).isSameAs(ret1); 92 | 93 | assertThat(callable.call()).isSameAs(ret2); 94 | 95 | verifyUncheckedThrow(callable); 96 | 97 | verifyErrorThrow(callable); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/functions/ToDoubleFunctionChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import com.github.fge.lambdas.helpers.Type1; 6 | import org.testng.annotations.Test; 7 | 8 | import java.util.concurrent.Callable; 9 | import java.util.function.ToDoubleFunction; 10 | 11 | import static org.assertj.core.api.Assertions.assertThat; 12 | import static org.mockito.Mockito.mock; 13 | import static org.mockito.Mockito.when; 14 | 15 | public final class ToDoubleFunctionChainerTest 16 | extends ChainerTest, ThrowingToDoubleFunction, ToDoubleFunctionChainer, Double> 17 | { 18 | private final Type1 value = Type1.mock(); 19 | 20 | public ToDoubleFunctionChainerTest() 21 | { 22 | super(42.0, 24.0); 23 | } 24 | 25 | @Override 26 | protected ToDoubleFunction getFallback() 27 | { 28 | return mock(ToDoubleFunction.class); 29 | } 30 | 31 | @Override 32 | protected ThrowingToDoubleFunction getThrowing() 33 | { 34 | return mock(ThrowingToDoubleFunction.class); 35 | } 36 | 37 | @Override 38 | protected ToDoubleFunctionChainer getChain( 39 | final ThrowingToDoubleFunction throwing) 40 | { 41 | return Throwing.toDoubleFunction(throwing); 42 | } 43 | 44 | @Override 45 | protected Callable toCallable(final ToDoubleFunction chain) 46 | { 47 | return () -> chain.applyAsDouble(value); 48 | } 49 | 50 | @Override 51 | protected void configureFull(final ThrowingToDoubleFunction throwing) 52 | throws Throwable 53 | { 54 | when(throwing.doApplyAsDouble(value)) 55 | .thenReturn(ret1) 56 | .thenThrow(checked) 57 | .thenThrow(unchecked) 58 | .thenThrow(error); 59 | } 60 | 61 | @Override 62 | protected void configureAlternate( 63 | final ThrowingToDoubleFunction throwing) 64 | throws Throwable 65 | { 66 | when(throwing.doApplyAsDouble(value)) 67 | .thenReturn(ret2); 68 | } 69 | 70 | @Override 71 | protected void configureFallback(final ToDoubleFunction fallback) 72 | { 73 | when(fallback.applyAsDouble(value)) 74 | .thenReturn(ret2); 75 | } 76 | 77 | @Test 78 | public void orReturnTest() 79 | throws Throwable 80 | { 81 | final ThrowingToDoubleFunction throwing = getThrowing(); 82 | configureFull(throwing); 83 | 84 | final ToDoubleFunction chain = getChain(throwing).orReturn(ret2); 85 | 86 | final Callable callable = toCallable(chain); 87 | 88 | assertThat(callable.call()).isEqualTo(ret1); 89 | 90 | assertThat(callable.call()).isEqualTo(ret2); 91 | 92 | verifyUncheckedThrow(callable); 93 | 94 | verifyErrorThrow(callable); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/functions/ToIntFunctionChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import com.github.fge.lambdas.helpers.Type1; 6 | import org.testng.annotations.Test; 7 | 8 | import java.util.concurrent.Callable; 9 | import java.util.function.ToIntFunction; 10 | 11 | import static org.assertj.core.api.Assertions.assertThat; 12 | import static org.mockito.Mockito.mock; 13 | import static org.mockito.Mockito.when; 14 | 15 | public final class ToIntFunctionChainerTest 16 | extends ChainerTest, ThrowingToIntFunction, ToIntFunctionChainer, Integer> 17 | { 18 | private final Type1 value = Type1.mock(); 19 | 20 | public ToIntFunctionChainerTest() 21 | { 22 | super(42, 24); 23 | } 24 | 25 | @Override 26 | protected ToIntFunction getFallback() 27 | { 28 | return mock(ToIntFunction.class); 29 | } 30 | 31 | @Override 32 | protected ThrowingToIntFunction getThrowing() 33 | { 34 | return mock(ThrowingToIntFunction.class); 35 | } 36 | 37 | @Override 38 | protected ToIntFunctionChainer getChain( 39 | final ThrowingToIntFunction throwing) 40 | { 41 | return Throwing.toIntFunction(throwing); 42 | } 43 | 44 | @Override 45 | protected Callable toCallable(final ToIntFunction chain) 46 | { 47 | return () -> chain.applyAsInt(value); 48 | } 49 | 50 | @Override 51 | protected void configureFull(final ThrowingToIntFunction throwing) 52 | throws Throwable 53 | { 54 | when(throwing.doApplyAsInt(value)) 55 | .thenReturn(ret1) 56 | .thenThrow(checked) 57 | .thenThrow(unchecked) 58 | .thenThrow(error); 59 | } 60 | 61 | @Override 62 | protected void configureAlternate( 63 | final ThrowingToIntFunction throwing) 64 | throws Throwable 65 | { 66 | when(throwing.doApplyAsInt(value)) 67 | .thenReturn(ret2); 68 | } 69 | 70 | @Override 71 | protected void configureFallback(final ToIntFunction fallback) 72 | { 73 | when(fallback.applyAsInt(value)) 74 | .thenReturn(ret2); 75 | } 76 | 77 | @Test 78 | public void orReturnTest() 79 | throws Throwable 80 | { 81 | final ThrowingToIntFunction throwing = getThrowing(); 82 | configureFull(throwing); 83 | 84 | final ToIntFunction chain = getChain(throwing).orReturn(ret2); 85 | 86 | final Callable callable = toCallable(chain); 87 | 88 | assertThat(callable.call()).isEqualTo(ret1); 89 | 90 | assertThat(callable.call()).isEqualTo(ret2); 91 | 92 | verifyUncheckedThrow(callable); 93 | 94 | verifyErrorThrow(callable); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/functions/ToLongFunctionChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import com.github.fge.lambdas.helpers.Type1; 6 | import org.testng.annotations.Test; 7 | 8 | import java.util.concurrent.Callable; 9 | import java.util.function.ToLongFunction; 10 | 11 | import static org.assertj.core.api.Assertions.assertThat; 12 | import static org.mockito.Mockito.mock; 13 | import static org.mockito.Mockito.when; 14 | 15 | public final class ToLongFunctionChainerTest 16 | extends ChainerTest, ThrowingToLongFunction, ToLongFunctionChainer, Long> 17 | { 18 | private final Type1 value = Type1.mock(); 19 | 20 | public ToLongFunctionChainerTest() 21 | { 22 | super(42L, 24L); 23 | } 24 | 25 | @Override 26 | protected ToLongFunction getFallback() 27 | { 28 | return mock(ToLongFunction.class); 29 | } 30 | 31 | @Override 32 | protected ThrowingToLongFunction getThrowing() 33 | { 34 | return mock(ThrowingToLongFunction.class); 35 | } 36 | 37 | @Override 38 | protected ToLongFunctionChainer getChain( 39 | final ThrowingToLongFunction throwing) 40 | { 41 | return Throwing.toLongFunction(throwing); 42 | } 43 | 44 | @Override 45 | protected Callable toCallable(final ToLongFunction chain) 46 | { 47 | return () -> chain.applyAsLong(value); 48 | } 49 | 50 | @Override 51 | protected void configureFull(final ThrowingToLongFunction throwing) 52 | throws Throwable 53 | { 54 | when(throwing.doApplyAsLong(value)) 55 | .thenReturn(ret1) 56 | .thenThrow(checked) 57 | .thenThrow(unchecked) 58 | .thenThrow(error); 59 | } 60 | 61 | @Override 62 | protected void configureAlternate( 63 | final ThrowingToLongFunction throwing) 64 | throws Throwable 65 | { 66 | when(throwing.doApplyAsLong(value)) 67 | .thenReturn(ret2); 68 | } 69 | 70 | @Override 71 | protected void configureFallback(final ToLongFunction fallback) 72 | { 73 | when(fallback.applyAsLong(value)) 74 | .thenReturn(ret2); 75 | } 76 | 77 | @Test 78 | public void orReturnTest() 79 | throws Throwable 80 | { 81 | final ThrowingToLongFunction throwing = getThrowing(); 82 | configureFull(throwing); 83 | 84 | final ToLongFunction chain = getChain(throwing).orReturn(ret2); 85 | 86 | final Callable callable = toCallable(chain); 87 | 88 | assertThat(callable.call()).isEqualTo(ret1); 89 | 90 | assertThat(callable.call()).isEqualTo(ret2); 91 | 92 | verifyUncheckedThrow(callable); 93 | 94 | verifyErrorThrow(callable); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/functions/doublefunctions/DoubleFunctionChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.doublefunctions; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import com.github.fge.lambdas.helpers.Type1; 6 | import org.testng.annotations.Test; 7 | 8 | import java.util.concurrent.Callable; 9 | import java.util.function.DoubleFunction; 10 | 11 | import static org.assertj.core.api.Assertions.assertThat; 12 | import static org.mockito.Mockito.mock; 13 | import static org.mockito.Mockito.when; 14 | 15 | public final class DoubleFunctionChainerTest 16 | extends ChainerTest, ThrowingDoubleFunction, DoubleFunctionChainer, Type1> 17 | { 18 | private final double value = 42.0; 19 | 20 | public DoubleFunctionChainerTest() 21 | { 22 | super(Type1.mock(), Type1.mock()); 23 | } 24 | 25 | @Override 26 | protected DoubleFunction getFallback() 27 | { 28 | return mock(DoubleFunction.class); 29 | } 30 | 31 | @Override 32 | protected ThrowingDoubleFunction getThrowing() 33 | { 34 | return mock(ThrowingDoubleFunction.class); 35 | } 36 | 37 | @Override 38 | protected DoubleFunctionChainer getChain( 39 | final ThrowingDoubleFunction throwing) 40 | { 41 | return Throwing.doubleFunction(throwing); 42 | } 43 | 44 | @Override 45 | protected Callable toCallable(final DoubleFunction chain) 46 | { 47 | return () -> chain.apply(value); 48 | } 49 | 50 | @Override 51 | protected void configureFull(final ThrowingDoubleFunction throwing) 52 | throws Throwable 53 | { 54 | when(throwing.doApply(value)) 55 | .thenReturn(ret1) 56 | .thenThrow(checked) 57 | .thenThrow(unchecked) 58 | .thenThrow(error); 59 | } 60 | 61 | @Override 62 | protected void configureAlternate( 63 | final ThrowingDoubleFunction throwing) 64 | throws Throwable 65 | { 66 | when(throwing.doApply(value)) 67 | .thenReturn(ret2); 68 | } 69 | 70 | @Override 71 | protected void configureFallback(final DoubleFunction fallback) 72 | { 73 | when(fallback.apply(value)) 74 | .thenReturn(ret2); 75 | } 76 | 77 | @Test 78 | public void orReturnTest() 79 | throws Throwable 80 | { 81 | final ThrowingDoubleFunction throwing 82 | = mock(ThrowingDoubleFunction.class); 83 | configureFull(throwing); 84 | 85 | final DoubleFunction chain = getChain(throwing).orReturn(ret2); 86 | 87 | final Callable callable = toCallable(chain); 88 | 89 | 90 | assertThat(callable.call()).isEqualTo(ret1); 91 | 92 | assertThat(callable.call()).isEqualTo(ret2); 93 | 94 | verifyUncheckedThrow(callable); 95 | 96 | verifyErrorThrow(callable); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/functions/doublefunctions/DoubleToIntFunctionChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.doublefunctions; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import org.testng.annotations.Test; 6 | 7 | import java.util.concurrent.Callable; 8 | import java.util.function.DoubleToIntFunction; 9 | 10 | import static org.assertj.core.api.Assertions.assertThat; 11 | import static org.mockito.Mockito.mock; 12 | import static org.mockito.Mockito.when; 13 | 14 | public final class DoubleToIntFunctionChainerTest 15 | extends ChainerTest 16 | { 17 | private final double value = 42.0; 18 | 19 | public DoubleToIntFunctionChainerTest() 20 | { 21 | super(42, 24); 22 | } 23 | 24 | @Override 25 | protected DoubleToIntFunction getFallback() 26 | { 27 | return mock(DoubleToIntFunction.class); 28 | } 29 | 30 | @Override 31 | protected ThrowingDoubleToIntFunction getThrowing() 32 | { 33 | return mock(ThrowingDoubleToIntFunction.class); 34 | } 35 | 36 | @Override 37 | protected DoubleToIntFunctionChainer getChain( 38 | final ThrowingDoubleToIntFunction throwing) 39 | { 40 | return Throwing.doubleToIntFunction(throwing); 41 | } 42 | 43 | @Override 44 | protected Callable toCallable(final DoubleToIntFunction chain) 45 | { 46 | return () -> chain.applyAsInt(value); 47 | } 48 | 49 | @Override 50 | protected void configureFull(final ThrowingDoubleToIntFunction throwing) 51 | throws Throwable 52 | { 53 | when(throwing.doApplyAsInt(value)) 54 | .thenReturn(ret1) 55 | .thenThrow(checked) 56 | .thenThrow(unchecked) 57 | .thenThrow(error); 58 | } 59 | 60 | @Override 61 | protected void configureAlternate( 62 | final ThrowingDoubleToIntFunction throwing) 63 | throws Throwable 64 | { 65 | when(throwing.doApplyAsInt(value)) 66 | .thenReturn(ret2); 67 | } 68 | 69 | @Override 70 | protected void configureFallback(final DoubleToIntFunction fallback) 71 | { 72 | when(fallback.applyAsInt(value)) 73 | .thenReturn(ret2); 74 | } 75 | 76 | @Test 77 | public void orReturnTest() 78 | throws Throwable 79 | { 80 | final ThrowingDoubleToIntFunction throwing = getThrowing(); 81 | configureFull(throwing); 82 | 83 | final DoubleToIntFunction chain = getChain(throwing).orReturn(ret2); 84 | 85 | final Callable callable = toCallable(chain); 86 | 87 | assertThat(callable.call()).isEqualTo(ret1); 88 | 89 | assertThat(callable.call()).isEqualTo(ret2); 90 | 91 | verifyUncheckedThrow(callable); 92 | 93 | verifyErrorThrow(callable); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/functions/doublefunctions/DoubleToLongFunctionChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.doublefunctions; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import org.testng.annotations.Test; 6 | 7 | import java.util.concurrent.Callable; 8 | import java.util.function.DoubleToLongFunction; 9 | 10 | import static org.assertj.core.api.Assertions.assertThat; 11 | import static org.mockito.Mockito.mock; 12 | import static org.mockito.Mockito.when; 13 | 14 | public final class DoubleToLongFunctionChainerTest 15 | extends ChainerTest 16 | { 17 | private final double value = 42.0; 18 | 19 | public DoubleToLongFunctionChainerTest() 20 | { 21 | super(42L, 24L); 22 | } 23 | 24 | @Override 25 | protected DoubleToLongFunction getFallback() 26 | { 27 | return mock(DoubleToLongFunction.class); 28 | } 29 | 30 | @Override 31 | protected ThrowingDoubleToLongFunction getThrowing() 32 | { 33 | return mock(ThrowingDoubleToLongFunction.class); 34 | } 35 | 36 | @Override 37 | protected DoubleToLongFunctionChainer getChain( 38 | final ThrowingDoubleToLongFunction throwing) 39 | { 40 | return Throwing.doubleToLongFunction(throwing); 41 | } 42 | 43 | @Override 44 | protected Callable toCallable(final DoubleToLongFunction chain) 45 | { 46 | return () -> chain.applyAsLong(value); 47 | } 48 | 49 | @Override 50 | protected void configureFull(final ThrowingDoubleToLongFunction throwing) 51 | throws Throwable 52 | { 53 | when(throwing.doApplyAsLong(value)) 54 | .thenReturn(ret1) 55 | .thenThrow(checked) 56 | .thenThrow(unchecked) 57 | .thenThrow(error); 58 | } 59 | 60 | @Override 61 | protected void configureAlternate( 62 | final ThrowingDoubleToLongFunction throwing) 63 | throws Throwable 64 | { 65 | when(throwing.doApplyAsLong(value)) 66 | .thenReturn(ret2); 67 | } 68 | 69 | @Override 70 | protected void configureFallback(final DoubleToLongFunction fallback) 71 | { 72 | when(fallback.applyAsLong(value)) 73 | .thenReturn(ret2); 74 | } 75 | 76 | @Test 77 | public void orReturnTest() 78 | throws Throwable 79 | { 80 | final ThrowingDoubleToLongFunction throwing = getThrowing(); 81 | configureFull(throwing); 82 | 83 | final DoubleToLongFunction chain = getChain(throwing).orReturn(ret2); 84 | 85 | final Callable callable = toCallable(chain); 86 | 87 | assertThat(callable.call()).isEqualTo(ret1); 88 | 89 | assertThat(callable.call()).isEqualTo(ret2); 90 | 91 | verifyUncheckedThrow(callable); 92 | 93 | verifyErrorThrow(callable); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/functions/intfunctions/IntFunctionChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.intfunctions; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import com.github.fge.lambdas.helpers.Type1; 6 | import org.testng.annotations.Test; 7 | 8 | import java.util.concurrent.Callable; 9 | import java.util.function.IntFunction; 10 | 11 | import static org.assertj.core.api.Assertions.assertThat; 12 | import static org.mockito.Mockito.mock; 13 | import static org.mockito.Mockito.when; 14 | 15 | public final class IntFunctionChainerTest 16 | extends ChainerTest, ThrowingIntFunction, IntFunctionChainer, Type1> 17 | { 18 | private final int value = 42; 19 | 20 | public IntFunctionChainerTest() 21 | { 22 | super(Type1.mock(), Type1.mock()); 23 | } 24 | 25 | @Override 26 | protected IntFunction getFallback() 27 | { 28 | return mock(IntFunction.class); 29 | } 30 | 31 | @Override 32 | protected ThrowingIntFunction getThrowing() 33 | { 34 | return mock(ThrowingIntFunction.class); 35 | } 36 | 37 | @Override 38 | protected IntFunctionChainer getChain( 39 | final ThrowingIntFunction throwing) 40 | { 41 | return Throwing.intFunction(throwing); 42 | } 43 | 44 | @Override 45 | protected Callable toCallable(final IntFunction chain) 46 | { 47 | return () -> chain.apply(value); 48 | } 49 | 50 | @Override 51 | protected void configureFull(final ThrowingIntFunction throwing) 52 | throws Throwable 53 | { 54 | when(throwing.doApply(value)) 55 | .thenReturn(ret1) 56 | .thenThrow(checked) 57 | .thenThrow(unchecked) 58 | .thenThrow(error); 59 | } 60 | 61 | @Override 62 | protected void configureAlternate( 63 | final ThrowingIntFunction throwing) 64 | throws Throwable 65 | { 66 | when(throwing.doApply(value)) 67 | .thenReturn(ret2); 68 | } 69 | 70 | @Override 71 | protected void configureFallback(final IntFunction fallback) 72 | { 73 | when(fallback.apply(value)) 74 | .thenReturn(ret2); 75 | } 76 | 77 | @Test 78 | public void orReturnTest() 79 | throws Throwable 80 | { 81 | final ThrowingIntFunction throwing 82 | = mock(ThrowingIntFunction.class); 83 | configureFull(throwing); 84 | 85 | final IntFunction chain = getChain(throwing).orReturn(ret2); 86 | 87 | final Callable callable = toCallable(chain); 88 | 89 | 90 | assertThat(callable.call()).isEqualTo(ret1); 91 | 92 | assertThat(callable.call()).isEqualTo(ret2); 93 | 94 | verifyUncheckedThrow(callable); 95 | 96 | verifyErrorThrow(callable); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/functions/intfunctions/IntToDoubleFunctionChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.intfunctions; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import org.testng.annotations.Test; 6 | 7 | import java.util.concurrent.Callable; 8 | import java.util.function.IntToDoubleFunction; 9 | 10 | import static org.assertj.core.api.Assertions.assertThat; 11 | import static org.mockito.Mockito.mock; 12 | import static org.mockito.Mockito.when; 13 | 14 | public final class IntToDoubleFunctionChainerTest 15 | extends ChainerTest 16 | { 17 | private final int value = 42; 18 | 19 | public IntToDoubleFunctionChainerTest() 20 | { 21 | super(42.0, 24.0); 22 | } 23 | 24 | @Override 25 | protected IntToDoubleFunction getFallback() 26 | { 27 | return mock(IntToDoubleFunction.class); 28 | } 29 | 30 | @Override 31 | protected ThrowingIntToDoubleFunction getThrowing() 32 | { 33 | return mock(ThrowingIntToDoubleFunction.class); 34 | } 35 | 36 | @Override 37 | protected IntToDoubleFunctionChainer getChain( 38 | final ThrowingIntToDoubleFunction throwing) 39 | { 40 | return Throwing.intToDoubleFunction(throwing); 41 | } 42 | 43 | @Override 44 | protected Callable toCallable(final IntToDoubleFunction chain) 45 | { 46 | return () -> chain.applyAsDouble(value); 47 | } 48 | 49 | @Override 50 | protected void configureFull(final ThrowingIntToDoubleFunction throwing) 51 | throws Throwable 52 | { 53 | when(throwing.doApplyAsDouble(value)) 54 | .thenReturn(ret1) 55 | .thenThrow(checked) 56 | .thenThrow(unchecked) 57 | .thenThrow(error); 58 | } 59 | 60 | @Override 61 | protected void configureAlternate( 62 | final ThrowingIntToDoubleFunction throwing) 63 | throws Throwable 64 | { 65 | when(throwing.doApplyAsDouble(value)) 66 | .thenReturn(ret2); 67 | } 68 | 69 | @Override 70 | protected void configureFallback(final IntToDoubleFunction fallback) 71 | { 72 | when(fallback.applyAsDouble(value)) 73 | .thenReturn(ret2); 74 | } 75 | 76 | @Test 77 | public void orReturnTest() 78 | throws Throwable 79 | { 80 | final ThrowingIntToDoubleFunction throwing = getThrowing(); 81 | configureFull(throwing); 82 | 83 | final IntToDoubleFunction chain = getChain(throwing).orReturn(ret2); 84 | 85 | final Callable callable = toCallable(chain); 86 | 87 | assertThat(callable.call()).isEqualTo(ret1); 88 | 89 | assertThat(callable.call()).isEqualTo(ret2); 90 | 91 | verifyUncheckedThrow(callable); 92 | 93 | verifyErrorThrow(callable); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/functions/intfunctions/IntToLongFunctionChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.intfunctions; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import org.testng.annotations.Test; 6 | 7 | import java.util.concurrent.Callable; 8 | import java.util.function.IntToLongFunction; 9 | 10 | import static org.assertj.core.api.Assertions.assertThat; 11 | import static org.mockito.Mockito.mock; 12 | import static org.mockito.Mockito.when; 13 | 14 | public final class IntToLongFunctionChainerTest 15 | extends ChainerTest 16 | { 17 | private final int value = 42; 18 | 19 | public IntToLongFunctionChainerTest() 20 | { 21 | super(42L, 24L); 22 | } 23 | 24 | @Override 25 | protected IntToLongFunction getFallback() 26 | { 27 | return mock(IntToLongFunction.class); 28 | } 29 | 30 | @Override 31 | protected ThrowingIntToLongFunction getThrowing() 32 | { 33 | return mock(ThrowingIntToLongFunction.class); 34 | } 35 | 36 | @Override 37 | protected IntToLongFunctionChainer getChain( 38 | final ThrowingIntToLongFunction throwing) 39 | { 40 | return Throwing.intToLongFunction(throwing); 41 | } 42 | 43 | @Override 44 | protected Callable toCallable(final IntToLongFunction chain) 45 | { 46 | return () -> chain.applyAsLong(value); 47 | } 48 | 49 | @Override 50 | protected void configureFull(final ThrowingIntToLongFunction throwing) 51 | throws Throwable 52 | { 53 | when(throwing.doApplyAsLong(value)) 54 | .thenReturn(ret1) 55 | .thenThrow(checked) 56 | .thenThrow(unchecked) 57 | .thenThrow(error); 58 | } 59 | 60 | @Override 61 | protected void configureAlternate( 62 | final ThrowingIntToLongFunction throwing) 63 | throws Throwable 64 | { 65 | when(throwing.doApplyAsLong(value)) 66 | .thenReturn(ret2); 67 | } 68 | 69 | @Override 70 | protected void configureFallback(final IntToLongFunction fallback) 71 | { 72 | when(fallback.applyAsLong(value)) 73 | .thenReturn(ret2); 74 | } 75 | 76 | @Test 77 | public void orReturnTest() 78 | throws Throwable 79 | { 80 | final ThrowingIntToLongFunction throwing = getThrowing(); 81 | configureFull(throwing); 82 | 83 | final IntToLongFunction chain = getChain(throwing).orReturn(ret2); 84 | 85 | final Callable callable = toCallable(chain); 86 | 87 | assertThat(callable.call()).isEqualTo(ret1); 88 | 89 | assertThat(callable.call()).isEqualTo(ret2); 90 | 91 | verifyUncheckedThrow(callable); 92 | 93 | verifyErrorThrow(callable); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/functions/longfunctions/LongFunctionChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.longfunctions; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import com.github.fge.lambdas.helpers.Type1; 6 | import org.testng.annotations.Test; 7 | 8 | import java.util.concurrent.Callable; 9 | import java.util.function.LongFunction; 10 | 11 | import static org.assertj.core.api.Assertions.assertThat; 12 | import static org.mockito.Mockito.mock; 13 | import static org.mockito.Mockito.when; 14 | 15 | public final class LongFunctionChainerTest 16 | extends ChainerTest, ThrowingLongFunction, LongFunctionChainer, Type1> 17 | { 18 | private final long value = 42L; 19 | 20 | public LongFunctionChainerTest() 21 | { 22 | super(Type1.mock(), Type1.mock()); 23 | } 24 | 25 | @Override 26 | protected LongFunction getFallback() 27 | { 28 | return mock(LongFunction.class); 29 | } 30 | 31 | @Override 32 | protected ThrowingLongFunction getThrowing() 33 | { 34 | return mock(ThrowingLongFunction.class); 35 | } 36 | 37 | @Override 38 | protected LongFunctionChainer getChain( 39 | final ThrowingLongFunction throwing) 40 | { 41 | return Throwing.longFunction(throwing); 42 | } 43 | 44 | @Override 45 | protected Callable toCallable(final LongFunction chain) 46 | { 47 | return () -> chain.apply(value); 48 | } 49 | 50 | @Override 51 | protected void configureFull(final ThrowingLongFunction throwing) 52 | throws Throwable 53 | { 54 | when(throwing.doApply(value)) 55 | .thenReturn(ret1) 56 | .thenThrow(checked) 57 | .thenThrow(unchecked) 58 | .thenThrow(error); 59 | } 60 | 61 | @Override 62 | protected void configureAlternate( 63 | final ThrowingLongFunction throwing) 64 | throws Throwable 65 | { 66 | when(throwing.doApply(value)) 67 | .thenReturn(ret2); 68 | } 69 | 70 | @Override 71 | protected void configureFallback(final LongFunction fallback) 72 | { 73 | when(fallback.apply(value)) 74 | .thenReturn(ret2); 75 | } 76 | 77 | @Test 78 | public void orReturnTest() 79 | throws Throwable 80 | { 81 | final ThrowingLongFunction throwing = getThrowing(); 82 | configureFull(throwing); 83 | 84 | final LongFunction chain = getChain(throwing).orReturn(ret2); 85 | 86 | final Callable callable = toCallable(chain); 87 | 88 | assertThat(callable.call()).isEqualTo(ret1); 89 | 90 | assertThat(callable.call()).isEqualTo(ret2); 91 | 92 | verifyUncheckedThrow(callable); 93 | 94 | verifyErrorThrow(callable); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/functions/longfunctions/LongToDoubleFunctionChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.longfunctions; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import org.testng.annotations.Test; 6 | 7 | import java.util.concurrent.Callable; 8 | import java.util.function.LongToDoubleFunction; 9 | 10 | import static org.assertj.core.api.Assertions.assertThat; 11 | import static org.mockito.Mockito.mock; 12 | import static org.mockito.Mockito.when; 13 | 14 | public final class LongToDoubleFunctionChainerTest 15 | extends ChainerTest 16 | { 17 | private final long value = 42L; 18 | 19 | public LongToDoubleFunctionChainerTest() 20 | { 21 | super(42.0, 24.0); 22 | } 23 | 24 | @Override 25 | protected LongToDoubleFunction getFallback() 26 | { 27 | return mock(LongToDoubleFunction.class); 28 | } 29 | 30 | @Override 31 | protected ThrowingLongToDoubleFunction getThrowing() 32 | { 33 | return mock(ThrowingLongToDoubleFunction.class); 34 | } 35 | 36 | @Override 37 | protected LongToDoubleFunctionChainer getChain( 38 | final ThrowingLongToDoubleFunction throwing) 39 | { 40 | return Throwing.longToDoubleFunction(throwing); 41 | } 42 | 43 | @Override 44 | protected Callable toCallable(final LongToDoubleFunction chain) 45 | { 46 | return () -> chain.applyAsDouble(value); 47 | } 48 | 49 | @Override 50 | protected void configureFull(final ThrowingLongToDoubleFunction throwing) 51 | throws Throwable 52 | { 53 | when(throwing.doApplyAsDouble(value)) 54 | .thenReturn(ret1) 55 | .thenThrow(checked) 56 | .thenThrow(unchecked) 57 | .thenThrow(error); 58 | } 59 | 60 | @Override 61 | protected void configureAlternate( 62 | final ThrowingLongToDoubleFunction throwing) 63 | throws Throwable 64 | { 65 | when(throwing.doApplyAsDouble(value)) 66 | .thenReturn(ret2); 67 | } 68 | 69 | @Override 70 | protected void configureFallback(final LongToDoubleFunction fallback) 71 | { 72 | when(fallback.applyAsDouble(value)) 73 | .thenReturn(ret2); 74 | } 75 | 76 | @Test 77 | public void orReturnTest() 78 | throws Throwable 79 | { 80 | final ThrowingLongToDoubleFunction throwing = getThrowing(); 81 | configureFull(throwing); 82 | 83 | final LongToDoubleFunction chain = getChain(throwing).orReturn(ret2); 84 | 85 | final Callable callable = toCallable(chain); 86 | 87 | assertThat(callable.call()).isEqualTo(ret1); 88 | 89 | assertThat(callable.call()).isEqualTo(ret2); 90 | 91 | verifyUncheckedThrow(callable); 92 | 93 | verifyErrorThrow(callable); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/functions/longfunctions/LongToIntFunctionChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.functions.longfunctions; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import org.testng.annotations.Test; 6 | 7 | import java.util.concurrent.Callable; 8 | import java.util.function.LongToIntFunction; 9 | 10 | import static org.assertj.core.api.Assertions.assertThat; 11 | import static org.mockito.Mockito.mock; 12 | import static org.mockito.Mockito.when; 13 | 14 | public final class LongToIntFunctionChainerTest 15 | extends ChainerTest 16 | { 17 | private final long value = 42L; 18 | 19 | public LongToIntFunctionChainerTest() 20 | { 21 | super(42, 24); 22 | } 23 | 24 | @Override 25 | protected LongToIntFunction getFallback() 26 | { 27 | return mock(LongToIntFunction.class); 28 | } 29 | 30 | @Override 31 | protected ThrowingLongToIntFunction getThrowing() 32 | { 33 | return mock(ThrowingLongToIntFunction.class); 34 | } 35 | 36 | @Override 37 | protected LongToIntFunctionChainer getChain( 38 | final ThrowingLongToIntFunction throwing) 39 | { 40 | return Throwing.longToIntFunction(throwing); 41 | } 42 | 43 | @Override 44 | protected Callable toCallable(final LongToIntFunction chain) 45 | { 46 | return () -> chain.applyAsInt(value); 47 | } 48 | 49 | @Override 50 | protected void configureFull(final ThrowingLongToIntFunction throwing) 51 | throws Throwable 52 | { 53 | when(throwing.doApplyAsInt(value)) 54 | .thenReturn(ret1) 55 | .thenThrow(checked) 56 | .thenThrow(unchecked) 57 | .thenThrow(error); 58 | } 59 | 60 | @Override 61 | protected void configureAlternate( 62 | final ThrowingLongToIntFunction throwing) 63 | throws Throwable 64 | { 65 | when(throwing.doApplyAsInt(value)) 66 | .thenReturn(ret2); 67 | } 68 | 69 | @Override 70 | protected void configureFallback(final LongToIntFunction fallback) 71 | { 72 | when(fallback.applyAsInt(value)) 73 | .thenReturn(ret2); 74 | } 75 | 76 | @Test 77 | public void orReturnTest() 78 | throws Throwable 79 | { 80 | final ThrowingLongToIntFunction throwing = getThrowing(); 81 | configureFull(throwing); 82 | 83 | final LongToIntFunction chain = getChain(throwing).orReturn(ret2); 84 | 85 | final Callable callable = toCallable(chain); 86 | 87 | assertThat(callable.call()).isEqualTo(ret1); 88 | 89 | assertThat(callable.call()).isEqualTo(ret2); 90 | 91 | verifyUncheckedThrow(callable); 92 | 93 | verifyErrorThrow(callable); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/helpers/CustomAssertions.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.helpers; 2 | 3 | import org.assertj.core.api.Assertions; 4 | 5 | public final class CustomAssertions 6 | extends Assertions 7 | { 8 | public static void shouldHaveThrown(final Class c) 9 | { 10 | failBecauseExceptionWasNotThrown(c); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/helpers/MyException.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.helpers; 2 | 3 | public final class MyException 4 | extends RuntimeException 5 | { 6 | public MyException(final Throwable cause) 7 | { 8 | super(cause); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/helpers/Type1.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.helpers; 2 | 3 | import org.mockito.Matchers; 4 | import org.mockito.Mockito; 5 | 6 | public interface Type1 7 | { 8 | static Type1 mock() 9 | { 10 | return Mockito.mock(Type1.class); 11 | } 12 | 13 | static Type1 any() 14 | { 15 | return Matchers.any(Type1.class); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/helpers/Type2.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.helpers; 2 | 3 | import org.mockito.Matchers; 4 | import org.mockito.Mockito; 5 | 6 | public interface Type2 7 | { 8 | static Type2 mock() 9 | { 10 | return Mockito.mock(Type2.class); 11 | } 12 | 13 | static Type2 any() 14 | { 15 | return Matchers.any(Type2.class); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/helpers/Type3.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.helpers; 2 | 3 | import org.mockito.Matchers; 4 | import org.mockito.Mockito; 5 | 6 | public interface Type3 7 | { 8 | static Type3 mock() 9 | { 10 | return Mockito.mock(Type3.class); 11 | } 12 | 13 | static Type3 any() 14 | { 15 | return Matchers.any(Type3.class); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/predicates/IntPredicateChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.predicates; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import org.testng.annotations.Test; 6 | 7 | import java.util.concurrent.Callable; 8 | import java.util.function.IntPredicate; 9 | 10 | import static org.assertj.core.api.Assertions.assertThat; 11 | import static org.mockito.Mockito.mock; 12 | import static org.mockito.Mockito.when; 13 | 14 | public final class IntPredicateChainerTest 15 | extends ChainerTest 16 | { 17 | private final int value = 1; 18 | 19 | public IntPredicateChainerTest() 20 | { 21 | super(true, false); 22 | } 23 | 24 | @Override 25 | protected IntPredicate getFallback() 26 | { 27 | return mock(IntPredicate.class); 28 | } 29 | 30 | @Override 31 | protected ThrowingIntPredicate getThrowing() 32 | { 33 | return mock(ThrowingIntPredicate.class); 34 | } 35 | 36 | @Override 37 | protected IntPredicateChainer getChain(final ThrowingIntPredicate throwing) 38 | { 39 | return Throwing.intPredicate(throwing); 40 | } 41 | 42 | @Override 43 | protected Callable toCallable(final IntPredicate chain) 44 | { 45 | return () -> chain.test(value); 46 | } 47 | 48 | @Override 49 | protected void configureFull(final ThrowingIntPredicate throwing) 50 | throws Throwable 51 | { 52 | when(throwing.doTest(value)) 53 | .thenReturn(ret1) 54 | .thenThrow(checked) 55 | .thenThrow(unchecked) 56 | .thenThrow(error); 57 | } 58 | 59 | @Override 60 | protected void configureAlternate(final ThrowingIntPredicate throwing) 61 | throws Throwable 62 | { 63 | when(throwing.doTest(value)) 64 | .thenReturn(ret2); 65 | } 66 | 67 | @Override 68 | protected void configureFallback(final IntPredicate fallback) 69 | { 70 | when(fallback.test(value)) 71 | .thenReturn(ret2); 72 | } 73 | 74 | @Test 75 | public void orReturnTrueTest() 76 | throws Throwable 77 | { 78 | final ThrowingIntPredicate throwing = getThrowing(); 79 | configureFull(throwing); 80 | 81 | final IntPredicate chain = getChain(throwing).orReturnTrue(); 82 | 83 | final Callable callable = toCallable(chain); 84 | 85 | assertThat(callable.call()).isEqualTo(ret1); 86 | 87 | assertThat(callable.call()).isTrue(); 88 | 89 | verifyUncheckedThrow(callable); 90 | 91 | verifyErrorThrow(callable); 92 | } 93 | 94 | @Test 95 | public void orReturnFalseTest() 96 | throws Throwable 97 | { 98 | final ThrowingIntPredicate throwing = getThrowing(); 99 | configureFull(throwing); 100 | 101 | final IntPredicate chain = getChain(throwing).orReturnFalse(); 102 | 103 | final Callable callable = toCallable(chain); 104 | 105 | assertThat(callable.call()).isEqualTo(ret1); 106 | 107 | assertThat(callable.call()).isFalse(); 108 | 109 | verifyUncheckedThrow(callable); 110 | 111 | verifyErrorThrow(callable); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/predicates/LongPredicateChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.predicates; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import org.testng.annotations.Test; 6 | 7 | import java.util.concurrent.Callable; 8 | import java.util.function.LongPredicate; 9 | 10 | import static org.assertj.core.api.Assertions.assertThat; 11 | import static org.mockito.Mockito.mock; 12 | import static org.mockito.Mockito.when; 13 | 14 | public final class LongPredicateChainerTest 15 | extends ChainerTest 16 | { 17 | private final long value = 1L; 18 | 19 | public LongPredicateChainerTest() 20 | { 21 | super(true, false); 22 | } 23 | 24 | @Override 25 | protected LongPredicate getFallback() 26 | { 27 | return mock(LongPredicate.class); 28 | } 29 | 30 | @Override 31 | protected ThrowingLongPredicate getThrowing() 32 | { 33 | return mock(ThrowingLongPredicate.class); 34 | } 35 | 36 | @Override 37 | protected LongPredicateChainer getChain(final ThrowingLongPredicate throwing) 38 | { 39 | return Throwing.longPredicate(throwing); 40 | } 41 | 42 | @Override 43 | protected Callable toCallable(final LongPredicate chain) 44 | { 45 | return () -> chain.test(value); 46 | } 47 | 48 | @Override 49 | protected void configureFull(final ThrowingLongPredicate throwing) 50 | throws Throwable 51 | { 52 | when(throwing.doTest(value)) 53 | .thenReturn(ret1) 54 | .thenThrow(checked) 55 | .thenThrow(unchecked) 56 | .thenThrow(error); 57 | } 58 | 59 | @Override 60 | protected void configureAlternate(final ThrowingLongPredicate throwing) 61 | throws Throwable 62 | { 63 | when(throwing.doTest(value)) 64 | .thenReturn(ret2); 65 | } 66 | 67 | @Override 68 | protected void configureFallback(final LongPredicate fallback) 69 | { 70 | when(fallback.test(value)) 71 | .thenReturn(ret2); 72 | } 73 | 74 | @Test 75 | public void orReturnTrueTest() 76 | throws Throwable 77 | { 78 | final ThrowingLongPredicate throwing = getThrowing(); 79 | configureFull(throwing); 80 | 81 | final LongPredicate chain = getChain(throwing).orReturnTrue(); 82 | 83 | final Callable callable = toCallable(chain); 84 | 85 | assertThat(callable.call()).isEqualTo(ret1); 86 | 87 | assertThat(callable.call()).isTrue(); 88 | 89 | verifyUncheckedThrow(callable); 90 | 91 | verifyErrorThrow(callable); 92 | } 93 | 94 | @Test 95 | public void orReturnFalseTest() 96 | throws Throwable 97 | { 98 | final ThrowingLongPredicate throwing = getThrowing(); 99 | configureFull(throwing); 100 | 101 | final LongPredicate chain = getChain(throwing).orReturnFalse(); 102 | 103 | final Callable callable = toCallable(chain); 104 | 105 | assertThat(callable.call()).isEqualTo(ret1); 106 | 107 | assertThat(callable.call()).isFalse(); 108 | 109 | verifyUncheckedThrow(callable); 110 | 111 | verifyErrorThrow(callable); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/runnable/RunnableChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.runnable; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import com.github.fge.lambdas.helpers.Type1; 6 | import org.testng.annotations.BeforeMethod; 7 | import org.testng.annotations.Test; 8 | 9 | import java.util.concurrent.Callable; 10 | import java.util.concurrent.atomic.AtomicReference; 11 | 12 | import static org.assertj.core.api.Assertions.assertThat; 13 | import static org.mockito.Mockito.doAnswer; 14 | import static org.mockito.Mockito.mock; 15 | 16 | public final class RunnableChainerTest 17 | extends ChainerTest 18 | { 19 | private final AtomicReference sentinel = new AtomicReference<>(); 20 | 21 | private final Type1 novalue = Type1.mock(); 22 | 23 | public RunnableChainerTest() 24 | { 25 | super(Type1.mock(), Type1.mock()); 26 | } 27 | 28 | @BeforeMethod 29 | public void initSentinel() 30 | { 31 | sentinel.set(novalue); 32 | } 33 | 34 | @Override 35 | protected Runnable getFallback() 36 | { 37 | return mock(Runnable.class); 38 | } 39 | 40 | @Override 41 | protected ThrowingRunnable getThrowing() 42 | { 43 | return mock(ThrowingRunnable.class); 44 | } 45 | 46 | @Override 47 | protected RunnableChainer getChain(final ThrowingRunnable throwing) 48 | { 49 | return Throwing.runnable(throwing); 50 | } 51 | 52 | @Override 53 | protected Callable toCallable(final Runnable chain) 54 | { 55 | return () -> { 56 | chain.run(); 57 | return sentinel.get(); 58 | }; 59 | } 60 | 61 | @Override 62 | protected void configureFull(final ThrowingRunnable throwing) 63 | throws Throwable 64 | { 65 | doAnswer(invocation -> { sentinel.set(ret1); return null; }) 66 | .doThrow(checked) 67 | .doThrow(unchecked) 68 | .doThrow(error) 69 | .when(throwing).doRun(); 70 | } 71 | 72 | @Override 73 | protected void configureAlternate(final ThrowingRunnable throwing) 74 | throws Throwable 75 | { 76 | doAnswer(invocation -> { sentinel.set(ret2); return null; }) 77 | .when(throwing).doRun(); 78 | } 79 | 80 | @Override 81 | protected void configureFallback(final Runnable fallback) 82 | { 83 | doAnswer(invocation -> { sentinel.set(ret2); return null; }) 84 | .when(fallback).run(); 85 | } 86 | 87 | @Test 88 | public void orDoNothingTest() 89 | throws Throwable 90 | { 91 | final ThrowingRunnable throwing = getThrowing(); 92 | 93 | configureFull(throwing); 94 | 95 | final Runnable chain = getChain(throwing).orDoNothing(); 96 | 97 | final Callable callable = toCallable(chain); 98 | 99 | assertThat(callable.call()).isSameAs(ret1); 100 | 101 | assertThat(callable.call()).isSameAs(ret1); 102 | 103 | verifyUncheckedThrow(callable); 104 | 105 | verifyErrorThrow(callable); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/supplier/DoubleSupplierChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.supplier; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import org.testng.annotations.Test; 6 | 7 | import java.util.concurrent.Callable; 8 | import java.util.function.DoubleSupplier; 9 | 10 | import static org.assertj.core.api.Assertions.assertThat; 11 | import static org.mockito.Mockito.mock; 12 | import static org.mockito.Mockito.when; 13 | 14 | public final class DoubleSupplierChainerTest 15 | extends ChainerTest 16 | { 17 | public DoubleSupplierChainerTest() 18 | { 19 | super(42.0, 24.0); 20 | } 21 | 22 | @Override 23 | protected DoubleSupplier getFallback() 24 | { 25 | return mock(DoubleSupplier.class); 26 | } 27 | 28 | @Override 29 | protected ThrowingDoubleSupplier getThrowing() 30 | { 31 | return mock(ThrowingDoubleSupplier.class); 32 | } 33 | 34 | @Override 35 | protected DoubleSupplierChainer getChain( 36 | final ThrowingDoubleSupplier throwing) 37 | { 38 | return Throwing.doubleSupplier(throwing); 39 | } 40 | 41 | @Override 42 | protected Callable toCallable(final DoubleSupplier chain) 43 | { 44 | return chain::getAsDouble; 45 | } 46 | 47 | @Override 48 | protected void configureFull(final ThrowingDoubleSupplier throwing) 49 | throws Throwable 50 | { 51 | when(throwing.doGetAsDouble()) 52 | .thenReturn(ret1) 53 | .thenThrow(checked) 54 | .thenThrow(unchecked) 55 | .thenThrow(error); 56 | } 57 | 58 | @Override 59 | protected void configureAlternate(final ThrowingDoubleSupplier throwing) 60 | throws Throwable 61 | { 62 | when(throwing.doGetAsDouble()) 63 | .thenReturn(ret2); 64 | } 65 | 66 | @Override 67 | protected void configureFallback(final DoubleSupplier fallback) 68 | { 69 | when(fallback.getAsDouble()) 70 | .thenReturn(ret2); 71 | } 72 | 73 | @Test 74 | public void orReturnTest() 75 | throws Throwable 76 | { 77 | final ThrowingDoubleSupplier throwing = getThrowing(); 78 | configureFull(throwing); 79 | 80 | final DoubleSupplier chain = getChain(throwing).orReturn(ret2); 81 | 82 | final Callable callable = toCallable(chain); 83 | 84 | assertThat(callable.call()).isEqualTo(ret1); 85 | 86 | assertThat(callable.call()).isEqualTo(ret2); 87 | 88 | verifyUncheckedThrow(callable); 89 | 90 | verifyErrorThrow(callable); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/supplier/IntSupplierChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.supplier; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import org.testng.annotations.Test; 6 | 7 | import java.util.concurrent.Callable; 8 | import java.util.function.IntSupplier; 9 | 10 | import static org.assertj.core.api.Assertions.assertThat; 11 | import static org.mockito.Mockito.mock; 12 | import static org.mockito.Mockito.when; 13 | 14 | public final class IntSupplierChainerTest 15 | extends ChainerTest 16 | { 17 | public IntSupplierChainerTest() 18 | { 19 | super(42, 24); 20 | } 21 | 22 | @Override 23 | protected IntSupplier getFallback() 24 | { 25 | return mock(IntSupplier.class); 26 | } 27 | 28 | @Override 29 | protected ThrowingIntSupplier getThrowing() 30 | { 31 | return mock(ThrowingIntSupplier.class); 32 | } 33 | 34 | @Override 35 | protected IntSupplierChainer getChain( 36 | final ThrowingIntSupplier throwing) 37 | { 38 | return Throwing.intSupplier(throwing); 39 | } 40 | 41 | @Override 42 | protected Callable toCallable(final IntSupplier chain) 43 | { 44 | return chain::getAsInt; 45 | } 46 | 47 | @Override 48 | protected void configureFull(final ThrowingIntSupplier throwing) 49 | throws Throwable 50 | { 51 | when(throwing.doGetAsInt()) 52 | .thenReturn(ret1) 53 | .thenThrow(checked) 54 | .thenThrow(unchecked) 55 | .thenThrow(error); 56 | } 57 | 58 | @Override 59 | protected void configureAlternate(final ThrowingIntSupplier throwing) 60 | throws Throwable 61 | { 62 | when(throwing.doGetAsInt()) 63 | .thenReturn(ret2); 64 | } 65 | 66 | @Override 67 | protected void configureFallback(final IntSupplier fallback) 68 | { 69 | when(fallback.getAsInt()) 70 | .thenReturn(ret2); 71 | } 72 | 73 | @Test 74 | public void orReturnTest() 75 | throws Throwable 76 | { 77 | final ThrowingIntSupplier throwing = getThrowing(); 78 | configureFull(throwing); 79 | 80 | final IntSupplier chain = getChain(throwing).orReturn(ret2); 81 | 82 | final Callable callable = toCallable(chain); 83 | 84 | assertThat(callable.call()).isEqualTo(ret1); 85 | 86 | assertThat(callable.call()).isEqualTo(ret2); 87 | 88 | verifyUncheckedThrow(callable); 89 | 90 | verifyErrorThrow(callable); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/supplier/LongSupplierChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.supplier; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import org.testng.annotations.Test; 6 | 7 | import java.util.concurrent.Callable; 8 | import java.util.function.LongSupplier; 9 | 10 | import static org.assertj.core.api.Assertions.assertThat; 11 | import static org.mockito.Mockito.mock; 12 | import static org.mockito.Mockito.when; 13 | 14 | public final class LongSupplierChainerTest 15 | extends ChainerTest 16 | { 17 | public LongSupplierChainerTest() 18 | { 19 | super(42L, 24L); 20 | } 21 | 22 | @Override 23 | protected LongSupplier getFallback() 24 | { 25 | return mock(LongSupplier.class); 26 | } 27 | 28 | @Override 29 | protected ThrowingLongSupplier getThrowing() 30 | { 31 | return mock(ThrowingLongSupplier.class); 32 | } 33 | 34 | @Override 35 | protected LongSupplierChainer getChain( 36 | final ThrowingLongSupplier throwing) 37 | { 38 | return Throwing.longSupplier(throwing); 39 | } 40 | 41 | @Override 42 | protected Callable toCallable(final LongSupplier chain) 43 | { 44 | return chain::getAsLong; 45 | } 46 | 47 | @Override 48 | protected void configureFull(final ThrowingLongSupplier throwing) 49 | throws Throwable 50 | { 51 | when(throwing.doGetAsLong()) 52 | .thenReturn(ret1) 53 | .thenThrow(checked) 54 | .thenThrow(unchecked) 55 | .thenThrow(error); 56 | } 57 | 58 | @Override 59 | protected void configureAlternate(final ThrowingLongSupplier throwing) 60 | throws Throwable 61 | { 62 | when(throwing.doGetAsLong()) 63 | .thenReturn(ret2); 64 | } 65 | 66 | @Override 67 | protected void configureFallback(final LongSupplier fallback) 68 | { 69 | when(fallback.getAsLong()) 70 | .thenReturn(ret2); 71 | } 72 | 73 | @Test 74 | public void orReturnTest() 75 | throws Throwable 76 | { 77 | final ThrowingLongSupplier throwing = getThrowing(); 78 | configureFull(throwing); 79 | 80 | final LongSupplier chain = getChain(throwing).orReturn(ret2); 81 | 82 | final Callable callable = toCallable(chain); 83 | 84 | assertThat(callable.call()).isEqualTo(ret1); 85 | 86 | assertThat(callable.call()).isEqualTo(ret2); 87 | 88 | verifyUncheckedThrow(callable); 89 | 90 | verifyErrorThrow(callable); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/test/java/com/github/fge/lambdas/supplier/SupplierChainerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.fge.lambdas.supplier; 2 | 3 | import com.github.fge.lambdas.ChainerTest; 4 | import com.github.fge.lambdas.Throwing; 5 | import com.github.fge.lambdas.helpers.Type1; 6 | import org.testng.annotations.Test; 7 | 8 | import java.util.concurrent.Callable; 9 | import java.util.function.Supplier; 10 | 11 | import static org.assertj.core.api.Assertions.assertThat; 12 | import static org.mockito.Mockito.mock; 13 | import static org.mockito.Mockito.when; 14 | 15 | public final class SupplierChainerTest 16 | extends ChainerTest, ThrowingSupplier, SupplierChainer, Type1> 17 | { 18 | public SupplierChainerTest() 19 | { 20 | super(Type1.mock(), Type1.mock()); 21 | } 22 | 23 | @Override 24 | protected Supplier getFallback() 25 | { 26 | return mock(Supplier.class); 27 | } 28 | 29 | @Override 30 | protected ThrowingSupplier getThrowing() 31 | { 32 | return mock(ThrowingSupplier.class); 33 | } 34 | 35 | @Override 36 | protected SupplierChainer getChain( 37 | final ThrowingSupplier throwing) 38 | { 39 | return Throwing.supplier(throwing); 40 | } 41 | 42 | @Override 43 | protected Callable toCallable(final Supplier chain) 44 | { 45 | return chain::get; 46 | } 47 | 48 | @Override 49 | protected void configureFull(final ThrowingSupplier throwing) 50 | throws Throwable 51 | { 52 | when(throwing.doGet()) 53 | .thenReturn(ret1) 54 | .thenThrow(checked) 55 | .thenThrow(unchecked) 56 | .thenThrow(error); 57 | } 58 | 59 | @Override 60 | protected void configureAlternate(final ThrowingSupplier throwing) 61 | throws Throwable 62 | { 63 | when(throwing.doGet()) 64 | .thenReturn(ret2); 65 | } 66 | 67 | @Override 68 | protected void configureFallback(final Supplier fallback) 69 | { 70 | when(fallback.get()) 71 | .thenReturn(ret2); 72 | } 73 | 74 | @Test 75 | public void orReturnTest() 76 | throws Throwable 77 | { 78 | final ThrowingSupplier throwing = getThrowing(); 79 | configureFull(throwing); 80 | 81 | final Supplier chain = getChain(throwing).orReturn(ret2); 82 | 83 | final Callable callable = toCallable(chain); 84 | 85 | assertThat(callable.call()).isEqualTo(ret1); 86 | 87 | assertThat(callable.call()).isEqualTo(ret2); 88 | 89 | verifyUncheckedThrow(callable); 90 | 91 | verifyErrorThrow(callable); 92 | } 93 | } 94 | --------------------------------------------------------------------------------