-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from aNNiMON/exceptional-map
Add map function to Exceptional
- Loading branch information
Showing
4 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
stream/src/main/java/com/annimon/stream/function/ThrowableFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.annimon.stream.function; | ||
|
||
/** | ||
* Represents a function which produces result from input arguments and can throw an exception. | ||
* | ||
* @param <I> the type of the input of the function | ||
* @param <R> the type of the result of the function | ||
* @param <E> the type of the exception | ||
*/ | ||
@FunctionalInterface | ||
public interface ThrowableFunction<I, R, E extends Throwable> { | ||
|
||
/** | ||
* Applies this function to the given argument. | ||
* | ||
* @param value an argument | ||
* @return the function result | ||
* @throws E an exception | ||
*/ | ||
R apply(I value) throws E; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
stream/src/test/java/com/annimon/stream/function/ThrowableFunctionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.annimon.stream.function; | ||
|
||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Tests {@code ThrowableFunction}. | ||
* | ||
* @see com.annimon.stream.function.ThrowableFunction | ||
*/ | ||
public class ThrowableFunctionTest { | ||
|
||
@Test | ||
public void testApply() { | ||
assertEquals(100, (int) toInt.apply("100")); | ||
} | ||
|
||
|
||
@Test(expected = NumberFormatException.class) | ||
public void testApplyWithRuntimeException() { | ||
toInt.apply("oops"); | ||
} | ||
|
||
private static final ThrowableFunction<String, Integer, NumberFormatException> toInt | ||
= new ThrowableFunction<String, Integer, NumberFormatException>() { | ||
@Override | ||
public Integer apply(String value) throws NumberFormatException { | ||
return Integer.parseInt(value); | ||
} | ||
}; | ||
|
||
} |