Skip to content

Commit 5df5596

Browse files
committed
#43 Implemented support for throwable functions for Option.map/flatMap
1 parent c528410 commit 5df5596

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package javascalautils;
2+
3+
/**
4+
* Used when a function throws an exception which cannot be raised. <br>
5+
* This exception is then created and wrapped around the original exception.
6+
* @author Peter Nerg
7+
* @since 1.11
8+
*/
9+
public final class BrokenFunctionException extends RuntimeException {
10+
private static final long serialVersionUID = 3534543534523L;
11+
/**
12+
* Creates the exception
13+
* @param message The message
14+
* @param cause The underlying cause
15+
*/
16+
public BrokenFunctionException(String message, Throwable cause) {
17+
super(message, cause);
18+
}
19+
20+
}

src/main/java/javascalautils/None.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.io.Serializable;
1919
import java.util.NoSuchElementException;
20-
import java.util.function.Function;
2120
import java.util.function.Predicate;
2221
import java.util.function.Supplier;
2322

@@ -81,7 +80,7 @@ public boolean exists(Predicate<T> predicate) {
8180
* @since 1.0
8281
*/
8382
@SuppressWarnings("unchecked")
84-
public <R> Option<R> map(Function<T, R> function) {
83+
public <R> Option<R> map(ThrowableFunction1<T, R> function) {
8584
return (Option<R>) this;
8685
}
8786

@@ -93,7 +92,7 @@ public <R> Option<R> map(Function<T, R> function) {
9392
* @since 1.0
9493
*/
9594
@Override
96-
public <R> Option<R> flatMap(Function<T, Option<R>> function) {
95+
public <R> Option<R> flatMap(ThrowableFunction1<T, Option<R>> function) {
9796
// uses the Map method as it anyways produces the same result
9897
return map(null);
9998
}

src/main/java/javascalautils/Option.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.Iterator;
1919
import java.util.NoSuchElementException;
2020
import java.util.Optional;
21-
import java.util.function.Function;
2221
import java.util.function.Predicate;
2322
import java.util.function.Supplier;
2423
import java.util.stream.Stream;
@@ -275,7 +274,7 @@ default boolean isEmpty() {
275274
* @return The Option containing the mapped value
276275
* @since 1.0
277276
*/
278-
<R> Option<R> map(Function<T, R> function);
277+
<R> Option<R> map(ThrowableFunction1<T, R> function);
279278

280279
/**
281280
* Returns an Option consisting of the result of applying the given function to the current {@link Some}. <br>
@@ -288,7 +287,7 @@ default boolean isEmpty() {
288287
* @return The Option containing the mapped value
289288
* @since 1.2
290289
*/
291-
<R> Option<R> flatMap(Function<T, Option<R>> function);
290+
<R> Option<R> flatMap(ThrowableFunction1<T, Option<R>> function);
292291

293292
/**
294293
* Returns this Option if it is nonempty, otherwise return the result of provided by the supplier.

src/main/java/javascalautils/Some.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public T getOrElse(Supplier<T> s) {
9393
* @since 1.0
9494
*/
9595
@Override
96-
public <R> Option<R> map(Function<T, R> f) {
97-
return Option.apply(f.apply(value));
96+
public <R> Option<R> map(ThrowableFunction1<T, R> f) {
97+
return flatMap(v -> Option.apply(f.apply(v)));
9898
}
9999

100100
/**
@@ -103,8 +103,13 @@ public <R> Option<R> map(Function<T, R> f) {
103103
* @since 1.2
104104
*/
105105
@Override
106-
public <R> Option<R> flatMap(Function<T, Option<R>> function) {
107-
return function.apply(value);
106+
public <R> Option<R> flatMap(ThrowableFunction1<T, Option<R>> function) {
107+
try {
108+
return function.apply(value);
109+
}
110+
catch (Throwable ex) {
111+
throw new BrokenFunctionException("Caught exception while applying function", ex);
112+
}
108113
}
109114

110115
/**

0 commit comments

Comments
 (0)