Skip to content

Commit 24f6c84

Browse files
authored
Merge pull request #73 from /issues/72
Add support of default value other than null/0 and also fixes #72
2 parents bf4f8e8 + 879863a commit 24f6c84

File tree

82 files changed

+2024
-91
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2024
-91
lines changed

src/main/java/ch/powerunit/extensions/exceptions/BiFunctionWithException.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,16 @@ default BiFunction<T, U, Optional<R>> lift() {
103103

104104
/**
105105
* Converts this {@code BiFunctionWithException} to a lifted {@code BiFunction}
106-
* returning {@code null} in case of exception.
106+
* returning {@code null} (or the value redefined by the method
107+
* {@link #defaultValue()}) in case of exception.
107108
*
108109
* @return the function that ignore error
109110
* @see #ignored(BiFunctionWithException)
110111
* @see BiFunction
111112
*/
112113
@Override
113114
default BiFunction<T, U, R> ignore() {
114-
return lift().andThen(o -> o.orElse(null));
115+
return lift().andThen(o -> o.orElse(defaultValue()));
115116
}
116117

117118
/**
@@ -309,6 +310,48 @@ static <T, U, R, E extends Exception> BiFunction<T, U, R> ignored(BiFunctionWith
309310
return verifyFunction(function).ignore();
310311
}
311312

313+
/**
314+
* Converts a {@code BiFunctionWithException} to a lifted {@code BiFunction}
315+
* returning a default in case of exception.
316+
*
317+
* @param function
318+
* to be lifted
319+
* @param defaultValue
320+
* the value to be returned in case of error
321+
* @param <T>
322+
* the type of the first argument to the function
323+
* @param <U>
324+
* the type of the second argument to the function
325+
* @param <R>
326+
* the type of the result of the function
327+
* @param <E>
328+
* the type of the potential exception
329+
* @return the lifted function
330+
* @see #ignore()
331+
* @see #ignored(BiFunctionWithException)
332+
* @throws NullPointerException
333+
* if function is null
334+
* @since 3.0.0
335+
*/
336+
static <T, U, R, E extends Exception> BiFunction<T, U, R> ignored(BiFunctionWithException<T, U, R, E> function,
337+
R defaultValue) {
338+
verifyFunction(function);
339+
return new BiFunctionWithException<T, U, R, E>() {
340+
341+
@Override
342+
public R apply(T t, U u) throws E {
343+
return function.apply(t, u);
344+
}
345+
346+
@Override
347+
public R defaultValue() {
348+
return defaultValue;
349+
}
350+
351+
}.ignore();
352+
353+
}
354+
312355
/**
313356
* Convert this {@code BiFunctionWithException} to a lifted {@code BiFunction}
314357
* return {@code CompletionStage} as return value.

src/main/java/ch/powerunit/extensions/exceptions/BiPredicateWithException.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
*/
5252
@FunctionalInterface
5353
public interface BiPredicateWithException<T, U, E extends Exception>
54-
extends PrimitiveReturnExceptionHandlerSupport<BiPredicate<T, U>> {
54+
extends PrimitiveReturnExceptionHandlerSupport<BiPredicate<T, U>>, BooleanDefaultValue {
5555

5656
/**
5757
* Evaluates this predicate on the given arguments.
@@ -75,7 +75,7 @@ default BiPredicate<T, U> uncheckOrIgnore(boolean uncheck) {
7575
return test(t, u);
7676
} catch (Exception e) {
7777
PrimitiveReturnExceptionHandlerSupport.handleException(uncheck, e, exceptionMapper());
78-
return false;
78+
return defaultValue();
7979
}
8080
};
8181
}
@@ -281,4 +281,43 @@ static <T, U, E extends Exception> BiPredicate<T, U> ignored(BiPredicateWithExce
281281
return verifyPredicate(predicate).ignore();
282282
}
283283

284+
/**
285+
* Converts a {@code BiPredicateWithException} to a lifted {@code BiPredicate}
286+
* returning a default value in case of exception.
287+
*
288+
* @param predicate
289+
* to be lifted
290+
* @param defaultValue
291+
* value in case of exception
292+
* @param <T>
293+
* the type of the first argument to the predicate
294+
* @param <U>
295+
* the type of the second argument the predicate
296+
* @param <E>
297+
* the type of the potential exception
298+
* @return the lifted predicate
299+
* @see #ignore()
300+
* @see #ignored(BiPredicateWithException)
301+
* @throws NullPointerException
302+
* if predicate is null
303+
* @since 3.0.0
304+
*/
305+
static <T, U, E extends Exception> BiPredicate<T, U> ignored(BiPredicateWithException<T, U, E> predicate,
306+
boolean defaultValue) {
307+
verifyPredicate(predicate);
308+
return new BiPredicateWithException<T, U, E>() {
309+
310+
@Override
311+
public boolean test(T t, U u) throws E {
312+
return predicate.test(t, u);
313+
}
314+
315+
@Override
316+
public boolean defaultValue() {
317+
return defaultValue;
318+
}
319+
320+
}.ignore();
321+
}
322+
284323
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Powerunit - A JDK1.8 test framework
3+
* Copyright (C) 2014 Mathieu Boretti.
4+
*
5+
* This file is part of Powerunit
6+
*
7+
* Powerunit is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* Powerunit is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package ch.powerunit.extensions.exceptions;
21+
22+
interface BooleanDefaultValue {
23+
24+
/**
25+
* Defines the default value returned by the ignore and ignored method.
26+
*
27+
* @return the default value for the ignore/ignored method.
28+
* @since 3.0.0
29+
*/
30+
default boolean defaultValue() {
31+
return false;
32+
}
33+
34+
}

src/main/java/ch/powerunit/extensions/exceptions/BooleanSupplierWithException.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
*/
4646
@FunctionalInterface
4747
public interface BooleanSupplierWithException<E extends Exception>
48-
extends PrimitiveReturnExceptionHandlerSupport<BooleanSupplier> {
48+
extends PrimitiveReturnExceptionHandlerSupport<BooleanSupplier>, BooleanDefaultValue {
4949

5050
/**
5151
* Gets a result.
@@ -64,7 +64,7 @@ default BooleanSupplier uncheckOrIgnore(boolean uncheck) {
6464
return getAsBoolean();
6565
} catch (Exception e) {
6666
PrimitiveReturnExceptionHandlerSupport.handleException(uncheck, e, exceptionMapper());
67-
return false;
67+
return defaultValue();
6868
}
6969
};
7070
}
@@ -172,4 +172,39 @@ static <E extends Exception> BooleanSupplier ignored(BooleanSupplierWithExceptio
172172
return verifySupplier(supplier).ignore();
173173
}
174174

175+
/**
176+
* Converts a {@code BooleanSupplierWithException} to a lifted
177+
* {@code BooleanSupplier} returning a default value in case of exception.
178+
*
179+
* @param supplier
180+
* to be lifted
181+
* @param defaultValue
182+
* value in case of exception
183+
* @param <E>
184+
* the type of the potential exception
185+
* @return the lifted supplier
186+
* @see #ignore()
187+
* @see #ignored(BooleanSupplierWithException)
188+
* @throws NullPointerException
189+
* if supplier is null
190+
* @since 3.0.0
191+
*/
192+
static <E extends Exception> BooleanSupplier ignored(BooleanSupplierWithException<E> supplier,
193+
boolean defaultValue) {
194+
verifySupplier(supplier);
195+
return new BooleanSupplierWithException<E>() {
196+
197+
@Override
198+
public boolean getAsBoolean() throws E {
199+
return supplier.getAsBoolean();
200+
}
201+
202+
@Override
203+
public boolean defaultValue() {
204+
return defaultValue;
205+
}
206+
207+
}.ignore();
208+
}
209+
175210
}

src/main/java/ch/powerunit/extensions/exceptions/CommonsCollections4Helper.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
*/
2020
package ch.powerunit.extensions.exceptions;
2121

22+
import static ch.powerunit.extensions.exceptions.Constants.verifyConsumer;
23+
import static ch.powerunit.extensions.exceptions.Constants.verifyFunction;
24+
import static ch.powerunit.extensions.exceptions.Constants.verifyPredicate;
2225
import static ch.powerunit.extensions.exceptions.Constants.verifySupplier;
2326
import static ch.powerunit.extensions.exceptions.ExceptionMapper.forException;
2427
import static ch.powerunit.extensions.exceptions.ExceptionMapper.forExceptions;
25-
import static ch.powerunit.extensions.exceptions.Constants.verifyPredicate;
26-
import static ch.powerunit.extensions.exceptions.Constants.verifyFunction;
27-
import static ch.powerunit.extensions.exceptions.Constants.verifyConsumer;
2828

2929
import java.util.function.Function;
3030

@@ -42,7 +42,7 @@
4242
* <p>
4343
* The mapping between the interface from <i>commons-collections4</i> and this
4444
* library is the following :
45-
*
45+
*
4646
* <table border="1">
4747
* <caption>Mapping between interfaces</caption>
4848
* <tr>
@@ -66,7 +66,7 @@
6666
* <td>ConsumerWithException</td>
6767
* </tr>
6868
* </table>
69-
*
69+
*
7070
* @since 2.2.0
7171
*
7272
*/
@@ -82,7 +82,7 @@ private CommonsCollections4Helper() {
8282
/**
8383
* Transforms a {@link PredicateWithException} to the one from
8484
* commons-collections.
85-
*
85+
*
8686
* @param predicate
8787
* the {@link PredicateWithException} to be transformed to the one
8888
* from commons-collections.
@@ -105,7 +105,7 @@ public static <T> org.apache.commons.collections4.Predicate<T> asPredicate(Predi
105105
/**
106106
* Transforms a {@link SupplierWithException} to the one from
107107
* commons-collections.
108-
*
108+
*
109109
* @param supplier
110110
* the {@link SupplierWithException} to be transformed to the one
111111
* from commons-collections.
@@ -126,7 +126,7 @@ public static <T> Factory<T> asFactory(SupplierWithException<T, ?> supplier) {
126126
/**
127127
* Transforms a {@link FunctionWithException} to the one from
128128
* commons-collections.
129-
*
129+
*
130130
* @param function
131131
* the {@link FunctionWithException} to be transformed to the one
132132
* from commons-collections.
@@ -150,7 +150,7 @@ public static <I, O> Transformer<I, O> asTransformer(FunctionWithException<I, O,
150150
/**
151151
* Transforms a {@link ConsumerWithException} to the one from
152152
* commons-collections.
153-
*
153+
*
154154
* @param consumer
155155
* the {@link ConsumerWithException} to be transformed to the one
156156
* from commons-collections.

src/main/java/ch/powerunit/extensions/exceptions/DoubleBinaryOperatorWithException.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
*/
4747
@FunctionalInterface
4848
public interface DoubleBinaryOperatorWithException<E extends Exception>
49-
extends PrimitiveReturnExceptionHandlerSupport<DoubleBinaryOperator> {
49+
extends PrimitiveReturnExceptionHandlerSupport<DoubleBinaryOperator>, DoubleDefaultValue {
5050

5151
/**
5252
* Applies this operator to the given operands.
@@ -69,7 +69,7 @@ default DoubleBinaryOperator uncheckOrIgnore(boolean uncheck) {
6969
return applyAsDouble(left, right);
7070
} catch (Exception e) {
7171
PrimitiveReturnExceptionHandlerSupport.handleException(uncheck, e, exceptionMapper());
72-
return 0;
72+
return defaultValue();
7373
}
7474
};
7575
}
@@ -180,4 +180,40 @@ static <E extends Exception> DoubleBinaryOperator ignored(DoubleBinaryOperatorWi
180180
return verifyFunction(function).ignore();
181181
}
182182

183+
/**
184+
* Converts a {@code DoubleBinaryOperatorWithException} to a lifted
185+
* {@code DoubleBinaryOperator} with a default value as return value in case of
186+
* exception.
187+
*
188+
* @param function
189+
* to be lifted
190+
* @param defaultValue
191+
* value in case of exception
192+
* @param <E>
193+
* the type of the potential exception
194+
* @return the lifted function
195+
* @see #ignore()
196+
* @see #ignored(DoubleBinaryOperatorWithException)
197+
* @throws NullPointerException
198+
* if function is null
199+
* @since 3.0.0
200+
*/
201+
static <E extends Exception> DoubleBinaryOperator ignored(DoubleBinaryOperatorWithException<E> function,
202+
double defaultValue) {
203+
verifyFunction(function);
204+
return new DoubleBinaryOperatorWithException<E>() {
205+
206+
@Override
207+
public double applyAsDouble(double left, double right) throws E {
208+
return function.applyAsDouble(left, right);
209+
}
210+
211+
@Override
212+
public double defaultValue() {
213+
return defaultValue;
214+
}
215+
216+
}.ignore();
217+
}
218+
183219
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Powerunit - A JDK1.8 test framework
3+
* Copyright (C) 2014 Mathieu Boretti.
4+
*
5+
* This file is part of Powerunit
6+
*
7+
* Powerunit is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* Powerunit is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package ch.powerunit.extensions.exceptions;
21+
22+
interface DoubleDefaultValue {
23+
24+
/**
25+
* Defines the default value returned by the ignore and ignored method.
26+
*
27+
* @return the default value for the ignore/ignored method.
28+
* @since 3.0.0
29+
*/
30+
default double defaultValue() {
31+
return 0;
32+
}
33+
34+
}

0 commit comments

Comments
 (0)