Skip to content

Commit b0bc916

Browse files
authored
Add methods $.checkNotNull(), Optional.map(function1) and Optional.orThrow(function).
1 parent 2f88c07 commit b0bc916

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/main/java/com/github/underscore/$.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,6 +2457,20 @@ public static int lastIndex(final int[] array) {
24572457
return array.length - 1;
24582458
}
24592459

2460+
public static <T> T checkNotNull(T reference) {
2461+
if (reference == null) {
2462+
throw new NullPointerException();
2463+
}
2464+
return reference;
2465+
}
2466+
2467+
public static <T> T checkNotNull(T reference, Object errorMessage) {
2468+
if (reference == null) {
2469+
throw new NullPointerException(String.valueOf(errorMessage));
2470+
}
2471+
return reference;
2472+
}
2473+
24602474
@SuppressWarnings("unchecked")
24612475
protected static <T> List<T> newArrayList() {
24622476
return new ArrayList<T>();

src/main/java/com/github/underscore/Optional.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ public boolean isPresent() {
5454
return !absent;
5555
}
5656

57+
public <U> Optional<U> map(Function1<? super T, ? extends U> mapper) {
58+
$.checkNotNull(mapper);
59+
if (!isPresent()) {
60+
return absent();
61+
} else {
62+
return Optional.fromNullable(mapper.apply(arg));
63+
}
64+
}
65+
66+
public <X extends Throwable> T orThrow(Function<? extends X> exceptionFunction) throws X {
67+
if (absent) {
68+
throw exceptionFunction.apply();
69+
} else {
70+
return arg;
71+
}
72+
}
73+
5774
@Override
5875
public boolean equals(final Object o) {
5976
if (this == o) {

src/test/java/com/github/underscore/UnderscoreTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,61 @@ public void optional() {
263263
assertEquals("1", Optional.of(1).or(2).toString());
264264
assertEquals(null, Optional.absent().orNull());
265265
assertEquals("1", Optional.of(1).orNull().toString());
266+
assertFalse(Optional.<Integer>absent().map(new Function1<Integer, String>() {
267+
public String apply(Integer arg) {
268+
return "" + arg;
269+
}
270+
}).isPresent());
271+
assertEquals("1", Optional.of(1).map(new Function1<Integer, String>() {
272+
public String apply(Integer arg) {
273+
return "" + arg;
274+
}
275+
}).get().toString());
266276
try {
267277
Optional.absent().get();
268278
fail("IllegalStateException expected");
269279
} catch (IllegalStateException ex) {
270280
}
271281
}
272282

283+
@Test(expected = Exception.class)
284+
public void optionalOrThrow() throws RuntimeException {
285+
Optional.absent().orThrow(new Function<RuntimeException>() {
286+
public RuntimeException apply() {
287+
return new RuntimeException();
288+
}
289+
});
290+
}
291+
292+
@Test
293+
public void optionalOrThrowWithValue() {
294+
assertEquals("1", Optional.of(1).orThrow(new Function<RuntimeException>() {
295+
public RuntimeException apply() {
296+
return new RuntimeException();
297+
}
298+
}).toString());
299+
}
300+
301+
@Test(expected = NullPointerException.class)
302+
public void checkNotNull() {
303+
$.checkNotNull(null);
304+
}
305+
306+
@Test
307+
public void checkNotNullWithObject() {
308+
assertEquals("123", $.checkNotNull("123"));
309+
}
310+
311+
@Test(expected = NullPointerException.class)
312+
public void checkNotNullWithMessage() {
313+
$.checkNotNull(null, "Error message");
314+
}
315+
316+
@Test
317+
public void checkNotNullWithObjectAndMessage() {
318+
assertEquals("123", $.checkNotNull("123", "Error message"));
319+
}
320+
273321
@Test
274322
@SuppressWarnings("unchecked")
275323
public void stackoverflow() {

0 commit comments

Comments
 (0)