Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 118 additions & 1 deletion src/test/java/option/OptionalExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@

import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import static org.junit.Assert.assertEquals;

public class OptionalExample {

@Test
public static <T1, T2, R> Optional<R> zipMap(Optional<T1> o1, Optional<T2> o2, BiFunction<T1, T2, R> bf) {
return o1.flatMap(t1 -> o2.map(t2 -> bf.apply(t1, t2)));
}

public static <T1, T2, R> Optional<R> zipMap1(Optional<T1> o1, Optional<T2> o2, BiFunction<T1, T2, R> bf) {
Optional<Optional<R>> optionalOptional = o1.map(t1 -> o2.map(t2 -> bf.apply(t1, t2)));
return optionalOptional.orElse(Optional.empty());
}

@Test(expected = UnsupportedOperationException.class)
public void get() {
final Optional<String> o1 = Optional.empty();

Expand Down Expand Up @@ -50,6 +62,111 @@ public void map() {
assertEquals(expected, actual);
}

@Test
public void filter() throws Exception {
Optional<String> o1 = getOptional();

Predicate<String> p = s -> s.equals("abc");

Optional<String> expected = o1.filter(p);
Optional<String> actual;

if (o1.isPresent()) {
if (p.test(o1.get())) {
actual = o1;
} else {
actual = Optional.empty();
}
} else {
actual = Optional.empty();
}

assertEquals(expected, actual);
}

@Test
public void flatMap() throws Exception {
Optional<String> o1 = getOptional();

Function<String, Optional<String>> f = s -> Optional.of("test");

Optional<String> expected = o1.flatMap(f);
Optional<String> actual;

if (o1.isPresent()) {
actual = f.apply(o1.get());
} else {
actual = Optional.empty();
}

assertEquals(expected, actual);
}

@Test
public void orElse() throws Exception {
Optional<String> o1 = getOptional();

String str = "test";

String expected = o1.orElse(str);
String actual;

if (o1.isPresent()) {
actual = o1.get();
} else {
actual = str;
}

assertEquals(expected, actual);
}

@Test
public void orElseGet() throws Exception {
Optional<String> o1 = getOptional();

Supplier<String> supplier = () -> "test";

String expected = o1.orElseGet(supplier);
String actual;

if (o1.isPresent()) {
actual = o1.get();
} else {
actual = supplier.get();
}

assertEquals(expected, actual);
}

@Test
public void orElseThrow() throws Exception {
Optional<String> o1 = getOptional();
Supplier<NullPointerException> thrower = NullPointerException::new;

boolean thrownedExpected = false;
String expected = null;
try {
expected = o1.orElseThrow(thrower);
} catch (NullPointerException e) {
thrownedExpected = true;
}

boolean thrownedActual = false;
String actual = null;
try {
if (o1.isPresent()) {
actual = o1.get();
} else {
thrower.get();
}
} catch (NullPointerException e) {
thrownedActual = true;
}

assertEquals(thrownedExpected, thrownedActual);
assertEquals(expected, actual);
}

private Optional<String> getOptional() {
return ThreadLocalRandom.current().nextBoolean()
? Optional.empty()
Expand Down