File tree Expand file tree Collapse file tree 4 files changed +33
-10
lines changed
src/main/java/javascalautils Expand file tree Collapse file tree 4 files changed +33
-10
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1717
1818import java .io .Serializable ;
1919import java .util .NoSuchElementException ;
20- import java .util .function .Function ;
2120import java .util .function .Predicate ;
2221import 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 }
Original file line number Diff line number Diff line change 1818import java .util .Iterator ;
1919import java .util .NoSuchElementException ;
2020import java .util .Optional ;
21- import java .util .function .Function ;
2221import java .util .function .Predicate ;
2322import java .util .function .Supplier ;
2423import 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.
Original file line number Diff line number Diff 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 /**
You can’t perform that action at this time.
0 commit comments