Skip to content

Commit 879863a

Browse files
committed
Issue #72 - Add default value support for boolean methods
1 parent a01f9dd commit 879863a

19 files changed

+436
-0
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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/DoublePredicateWithException.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,39 @@ static <E extends Exception> DoublePredicate ignored(DoublePredicateWithExceptio
250250
return verifyPredicate(predicate).ignore();
251251
}
252252

253+
/**
254+
* Converts a {@code DoublePredicateWithException} to a lifted
255+
* {@code DoublePredicate} returning a default value in case of exception.
256+
*
257+
* @param predicate
258+
* to be lifted
259+
* @param defaultValue
260+
* value in case of exception
261+
* @param <E>
262+
* the type of the potential exception
263+
* @return the lifted predicate
264+
* @see #ignore()
265+
* @see #ignored(DoublePredicateWithException)
266+
* @throws NullPointerException
267+
* if predicate is null
268+
* @since 3.0.0
269+
*/
270+
static <E extends Exception> DoublePredicate ignored(DoublePredicateWithException<E> predicate,
271+
boolean defaultValue) {
272+
verifyPredicate(predicate);
273+
return new DoublePredicateWithException<E>() {
274+
275+
@Override
276+
public boolean test(double value) throws E {
277+
return predicate.test(value);
278+
}
279+
280+
@Override
281+
public boolean defaultValue() {
282+
return defaultValue;
283+
}
284+
285+
}.ignore();
286+
}
287+
253288
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,38 @@ static <E extends Exception> FileFilter ignored(FileFilterWithException<E> predi
253253
return verifyPredicate(predicate).ignore();
254254
}
255255

256+
/**
257+
* Converts a {@code FileFilterWithException} to a lifted {@code FileFilter}
258+
* returning a default value in case of exception.
259+
*
260+
* @param predicate
261+
* to be lifted
262+
* @param defaultValue
263+
* value in case of exception
264+
* @param <E>
265+
* the type of the potential exception
266+
* @return the lifted FileFilter
267+
* @see #ignore()
268+
* @see #ignored(FileFilterWithException)
269+
* @throws NullPointerException
270+
* if predicate is null
271+
* @since 3.0.0
272+
*/
273+
static <E extends Exception> FileFilter ignored(FileFilterWithException<E> predicate, boolean defaultValue) {
274+
verifyPredicate(predicate);
275+
return new FileFilterWithException<E>() {
276+
277+
@Override
278+
public boolean accept(File pathname) throws E {
279+
return predicate.accept(pathname);
280+
}
281+
282+
@Override
283+
public boolean defaultValue() {
284+
return defaultValue;
285+
}
286+
287+
}.ignore();
288+
}
289+
256290
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,39 @@ static <E extends Exception> FilenameFilter ignored(FilenameFilterWithException<
259259
return verifyPredicate(predicate).ignore();
260260
}
261261

262+
/**
263+
* Converts a {@code FilenameFilterWithException} to a lifted
264+
* {@code FilenameFilter} returning a default value in case of exception.
265+
*
266+
* @param predicate
267+
* to be lifted
268+
* @param defaultValue
269+
* value in case of exception
270+
* @param <E>
271+
* the type of the potential exception
272+
* @return the lifted FilenameFilter
273+
* @see #ignore()
274+
* @see #ignored(FilenameFilterWithException)
275+
* @throws NullPointerException
276+
* if predicate is null
277+
* @since 3.0.0
278+
*/
279+
static <E extends Exception> FilenameFilter ignored(FilenameFilterWithException<E> predicate,
280+
boolean defaultValue) {
281+
verifyPredicate(predicate);
282+
return new FilenameFilterWithException<E>() {
283+
284+
@Override
285+
public boolean accept(File dir, String name) throws E {
286+
return predicate.accept(dir, name);
287+
}
288+
289+
@Override
290+
public boolean defaultValue() {
291+
return defaultValue;
292+
}
293+
294+
}.ignore();
295+
}
296+
262297
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,38 @@ static <E extends Exception> IntPredicate ignored(IntPredicateWithException<E> p
250250
return verifyPredicate(predicate).ignore();
251251
}
252252

253+
/**
254+
* Converts a {@code IntPredicateWithException} to a lifted {@code IntPredicate}
255+
* returning a default value in case of exception.
256+
*
257+
* @param predicate
258+
* to be lifted
259+
* @param defaultValue
260+
* value in case of exception
261+
* @param <E>
262+
* the type of the potential exception
263+
* @return the lifted predicate
264+
* @see #ignore()
265+
* @see #ignored(IntPredicateWithException)
266+
* @throws NullPointerException
267+
* if predicate is null
268+
* @since 3.0.0
269+
*/
270+
static <E extends Exception> IntPredicate ignored(IntPredicateWithException<E> predicate, boolean defaultValue) {
271+
verifyPredicate(predicate);
272+
return new IntPredicateWithException<E>() {
273+
274+
@Override
275+
public boolean test(int value) throws E {
276+
return predicate.test(value);
277+
}
278+
279+
@Override
280+
public boolean defaultValue() {
281+
return defaultValue;
282+
}
283+
284+
}.ignore();
285+
}
286+
253287
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,38 @@ static <E extends Exception> LongPredicate ignored(LongPredicateWithException<E>
250250
return verifyPredicate(predicate).ignore();
251251
}
252252

253+
/**
254+
* Converts a {@code LongPredicateWithException} to a lifted
255+
* {@code LongPredicate} returning a default value in case of exception.
256+
*
257+
* @param predicate
258+
* to be lifted
259+
* @param defaultValue
260+
* value in case of exception
261+
* @param <E>
262+
* the type of the potential exception
263+
* @return the lifted predicate
264+
* @see #ignore()
265+
* @see #ignored(LongPredicateWithException)
266+
* @throws NullPointerException
267+
* if predicate is null
268+
* @since 3.0.0
269+
*/
270+
static <E extends Exception> LongPredicate ignored(LongPredicateWithException<E> predicate, boolean defaultValue) {
271+
verifyPredicate(predicate);
272+
return new LongPredicateWithException<E>() {
273+
274+
@Override
275+
public boolean test(long value) throws E {
276+
return predicate.test(value);
277+
}
278+
279+
@Override
280+
public boolean defaultValue() {
281+
return defaultValue;
282+
}
283+
284+
}.ignore();
285+
}
286+
253287
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,38 @@ static <E extends Exception> PathMatcher ignored(PathMatcherWithException<E> pre
256256
return verifyPredicate(predicate).ignore();
257257
}
258258

259+
/**
260+
* Converts a {@code PathMatcherWithException} to a lifted {@code PathMatcher}
261+
* returning a default value in case of exception.
262+
*
263+
* @param predicate
264+
* to be lifted
265+
* @param defaultValue
266+
* value in case of exception
267+
* @param <E>
268+
* the type of the potential exception
269+
* @return the lifted PathMatcher
270+
* @see #ignore()
271+
* @see #ignored(PathMatcherWithException)
272+
* @throws NullPointerException
273+
* if predicate is null
274+
* @since 3.0.0
275+
*/
276+
static <E extends Exception> PathMatcher ignored(PathMatcherWithException<E> predicate, boolean defaultValue) {
277+
verifyPredicate(predicate);
278+
return new PathMatcherWithException<E>() {
279+
280+
@Override
281+
public boolean matches(Path t) throws E {
282+
return predicate.matches(t);
283+
}
284+
285+
@Override
286+
public boolean defaultValue() {
287+
return defaultValue;
288+
}
289+
290+
}.ignore();
291+
}
292+
259293
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,40 @@ static <T, E extends Exception> Predicate<T> ignored(PredicateWithException<T, E
264264
return verifyPredicate(predicate).ignore();
265265
}
266266

267+
/**
268+
* Converts a {@code PredicateWithException} to a lifted {@code Predicate}
269+
* returning a default value in case of exception.
270+
*
271+
* @param predicate
272+
* to be lifted
273+
* @param defaultValue
274+
* value in case of exception
275+
* @param <T>
276+
* the type of the input to the predicate
277+
* @param <E>
278+
* the type of the potential exception
279+
* @return the lifted predicate
280+
* @see #ignore()
281+
* @see #ignored(PredicateWithException)
282+
* @throws NullPointerException
283+
* if predicate is null
284+
* @since 3.0.0
285+
*/
286+
static <T, E extends Exception> Predicate<T> ignored(PredicateWithException<T, E> predicate, boolean defaultValue) {
287+
verifyPredicate(predicate);
288+
return new PredicateWithException<T, E>() {
289+
290+
@Override
291+
public boolean test(T t) throws E {
292+
return predicate.test(t);
293+
}
294+
295+
@Override
296+
public boolean defaultValue() {
297+
return defaultValue;
298+
}
299+
300+
}.ignore();
301+
}
302+
267303
}

src/test/java/ch/powerunit/extensions/exceptions/BiPredicateWithExceptionTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,16 @@ public void testIgnoredException() {
145145
}).test("x", "x")).is(false);
146146
}
147147

148+
@Test
149+
public void testIgnoredDefaultNoException() {
150+
assertThat(BiPredicateWithException.ignored((x, y) -> false, true).test("2", "3")).is(false);
151+
}
152+
153+
@Test
154+
public void testIgnoredDefaultException() {
155+
assertThat(BiPredicateWithException.ignored((x, y) -> {
156+
throw new Exception();
157+
}, true).test("x", "x")).is(true);
158+
}
159+
148160
}

0 commit comments

Comments
 (0)