Skip to content

Commit a01f9dd

Browse files
committed
Issue #72 - Add double support for ignore methods
1 parent b30501d commit a01f9dd

14 files changed

+334
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,39 @@ static <E extends Exception> DoubleToIntFunction ignored(DoubleToIntFunctionWith
175175
return verifyFunction(function).ignore();
176176
}
177177

178+
/**
179+
* Converts a {@code DoubleToIntFunctionWithException} to a lifted
180+
* {@code DoubleToIntFunction} returning a default value in case of exception.
181+
*
182+
* @param function
183+
* to be lifted
184+
* @param defaultValue
185+
* value in case of exception
186+
* @param <E>
187+
* the type of the potential exception
188+
* @return the lifted function
189+
* @see #ignore()
190+
* @see #ignored(DoubleToIntFunctionWithException)
191+
* @throws NullPointerException
192+
* if function is null
193+
* @since 3.0.0
194+
*/
195+
static <E extends Exception> DoubleToIntFunction ignored(DoubleToIntFunctionWithException<E> function,
196+
int defaultValue) {
197+
verifyFunction(function);
198+
return new DoubleToIntFunctionWithException<E>() {
199+
200+
@Override
201+
public int applyAsInt(double value) throws E {
202+
return function.applyAsInt(value);
203+
}
204+
205+
@Override
206+
public int defaultValue() {
207+
return defaultValue;
208+
}
209+
210+
}.ignore();
211+
}
212+
178213
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,40 @@ static <E extends Exception> IntBinaryOperator ignored(IntBinaryOperatorWithExce
178178
return verifyFunction(function).ignore();
179179
}
180180

181+
/**
182+
* Converts a {@code IntBinaryOperatorWithException} to a lifted
183+
* {@code IntBinaryOperator} with a default value as return value in case of
184+
* exception.
185+
*
186+
* @param function
187+
* to be lifted
188+
* @param defaultValue
189+
* value in case of exception
190+
* @param <E>
191+
* the type of the potential exception
192+
* @return the lifted function
193+
* @see #ignore()
194+
* @see #ignored(IntBinaryOperatorWithException)
195+
* @throws NullPointerException
196+
* if function is null
197+
* @since 3.0.0
198+
*/
199+
static <E extends Exception> IntBinaryOperator ignored(IntBinaryOperatorWithException<E> function,
200+
int defaultValue) {
201+
verifyFunction(function);
202+
return new IntBinaryOperatorWithException<E>() {
203+
204+
@Override
205+
public int applyAsInt(int left, int right) throws E {
206+
return function.applyAsInt(left, right);
207+
}
208+
209+
@Override
210+
public int defaultValue() {
211+
return defaultValue;
212+
}
213+
214+
}.ignore();
215+
}
216+
181217
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,38 @@ static <E extends Exception> IntSupplier ignored(IntSupplierWithException<E> sup
176176
return verifySupplier(supplier).ignore();
177177
}
178178

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

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,38 @@ static <E extends Exception> IntUnaryOperator ignored(IntUnaryOperatorWithExcept
227227
return verifyFunction(function).ignore();
228228
}
229229

230+
/**
231+
* Converts a {@code IntUnaryOperatorWithException} to a lifted
232+
* {@code IntUnaryOperator} returning a default value in case of exception.
233+
*
234+
* @param function
235+
* to be lifted
236+
* @param defaultValue
237+
* value in case of exception
238+
* @param <E>
239+
* the type of the potential exception
240+
* @return the lifted function
241+
* @see #ignore()
242+
* @see #ignored(IntUnaryOperatorWithException)
243+
* @throws NullPointerException
244+
* if function is null
245+
* @since 3.0.0
246+
*/
247+
static <E extends Exception> IntUnaryOperator ignored(IntUnaryOperatorWithException<E> function, int defaultValue) {
248+
verifyFunction(function);
249+
return new IntUnaryOperatorWithException<E>() {
250+
251+
@Override
252+
public int applyAsInt(int operand) throws E {
253+
return function.applyAsInt(operand);
254+
}
255+
256+
@Override
257+
public int defaultValue() {
258+
return defaultValue;
259+
}
260+
261+
}.ignore();
262+
}
263+
230264
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,39 @@ static <E extends Exception> LongToIntFunction ignored(LongToIntFunctionWithExce
200200
return verifyFunction(function).ignore();
201201
}
202202

203+
/**
204+
* Converts a {@code LongToIntFunctionWithException} to a lifted
205+
* {@code LongToIntFunction} returning a default value in case of exception.
206+
*
207+
* @param function
208+
* to be lifted
209+
* @param defaultValue
210+
* value in case of exception
211+
* @param <E>
212+
* the type of the potential exception
213+
* @return the lifted function
214+
* @see #ignore()
215+
* @see #ignored(LongToIntFunctionWithException)
216+
* @throws NullPointerException
217+
* if function is null
218+
* @since 3.0.0
219+
*/
220+
static <E extends Exception> LongToIntFunction ignored(LongToIntFunctionWithException<E> function,
221+
int defaultValue) {
222+
verifyFunction(function);
223+
return new LongToIntFunctionWithException<E>() {
224+
225+
@Override
226+
public int applyAsInt(long value) throws E {
227+
return function.applyAsInt(value);
228+
}
229+
230+
@Override
231+
public int defaultValue() {
232+
return defaultValue;
233+
}
234+
235+
}.ignore();
236+
}
237+
203238
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,43 @@ static <T, U, E extends Exception> ToIntBiFunction<T, U> ignored(ToIntBiFunction
200200
return verifyFunction(function).ignore();
201201
}
202202

203+
/**
204+
* Converts a {@code ToIntBiFunctionWithException} to a lifted
205+
* {@code ToIntBiFunction} returning a default value in case of exception.
206+
*
207+
* @param function
208+
* to be lifted
209+
* @param defaultValue
210+
* value in case of exception
211+
* @param <T>
212+
* the type of the first argument to the function
213+
* @param <U>
214+
* the type of the second argument to the function
215+
* @param <E>
216+
* the type of the potential exception
217+
* @return the lifted function
218+
* @see #ignore()
219+
* @see #ignored(ToIntBiFunctionWithException)
220+
* @throws NullPointerException
221+
* if function is null
222+
* @since 3.0.0
223+
*/
224+
static <T, U, E extends Exception> ToIntBiFunction<T, U> ignored(ToIntBiFunctionWithException<T, U, E> function,
225+
int defaultValue) {
226+
verifyFunction(function);
227+
return new ToIntBiFunctionWithException<T, U, E>() {
228+
229+
@Override
230+
public int applyAsInt(T t, U u) throws E {
231+
return function.applyAsInt(t, u);
232+
}
233+
234+
@Override
235+
public int defaultValue() {
236+
return defaultValue;
237+
}
238+
239+
}.ignore();
240+
}
241+
203242
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,41 @@ static <T, E extends Exception> ToIntFunction<T> ignored(ToIntFunctionWithExcept
186186
return verifyFunction(function).ignore();
187187
}
188188

189+
/**
190+
* Converts a {@code ToLongFunctionWithException} to a lifted
191+
* {@code ToIntFunction} returning a default value in case of exception.
192+
*
193+
* @param function
194+
* to be lifted
195+
* @param defaultValue
196+
* value in case of exception.
197+
* @param <T>
198+
* the type of the input object to the function
199+
* @param <E>
200+
* the type of the potential exception
201+
* @return the lifted function
202+
* @see #ignore()
203+
* @see #ignored(ToIntFunctionWithException)
204+
* @throws NullPointerException
205+
* if function is null
206+
* @since 3.0.0
207+
*/
208+
static <T, E extends Exception> ToIntFunction<T> ignored(ToIntFunctionWithException<T, E> function,
209+
int defaultValue) {
210+
verifyFunction(function);
211+
return new ToIntFunctionWithException<T, E>() {
212+
213+
@Override
214+
public int applyAsInt(T value) throws E {
215+
return function.applyAsInt(value);
216+
}
217+
218+
@Override
219+
public int defaultValue() {
220+
return defaultValue;
221+
}
222+
223+
}.ignore();
224+
}
225+
189226
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,16 @@ public void testIgnoredException() {
7474
}).applyAsInt(1)).is(0);
7575
}
7676

77+
@Test
78+
public void testIgnoredDefaultNoException() {
79+
assertThat(DoubleToIntFunctionWithException.ignored(x -> 1, 2).applyAsInt(2)).is(1);
80+
}
81+
82+
@Test
83+
public void testIgnoredDefaultException() {
84+
assertThat(DoubleToIntFunctionWithException.ignored(y -> {
85+
throw new Exception();
86+
}, 1).applyAsInt(1)).is(1);
87+
}
88+
7789
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,16 @@ public void testIgnoredException() {
7373
throw new Exception();
7474
}).applyAsInt(1, 2)).is(0);
7575
}
76+
77+
@Test
78+
public void testIgnoredDefaultNoException() {
79+
assertThat(IntBinaryOperatorWithException.ignored((x, y) -> x + y, 1).applyAsInt(2, 1)).is(3);
80+
}
81+
82+
@Test
83+
public void testIgnoredDefaultException() {
84+
assertThat(IntBinaryOperatorWithException.ignored((x, y) -> {
85+
throw new Exception();
86+
}, 1).applyAsInt(1, 2)).is(1);
87+
}
7688
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,16 @@ public void testIgnoredException() {
7474
}).getAsInt()).is(0);
7575
}
7676

77+
@Test
78+
public void testIgnoredDefaultNoException() {
79+
assertThat(IntSupplierWithException.ignored(() -> 2, 1).getAsInt()).is(2);
80+
}
81+
82+
@Test
83+
public void testIgnoredDefaultException() {
84+
assertThat(IntSupplierWithException.ignored(() -> {
85+
throw new Exception();
86+
}, 1).getAsInt()).is(1);
87+
}
88+
7789
}

0 commit comments

Comments
 (0)