Created
March 13, 2021 17:05
-
-
Save benjiman/591b133f984cee9cafe6591a2baed4a1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.benjiweber.example; | |
import org.junit.Test; | |
import java.util.Optional; | |
import static org.junit.Assert.assertEquals; | |
import static com.benjiweber.recordmixins.OptionalPatternMatchTest.None.*; | |
public class OptionalPatternMatchTest2 { | |
@Test | |
public void traditional_unwrap() { | |
Optional<String> thisIsPresent = Optional.of("Hello World"); | |
assertEquals( | |
"hello world", | |
thisIsPresent | |
.map(String::toLowerCase) | |
.orElse("absent") | |
); | |
} | |
@Test | |
public void unwrap_optional() { | |
Optional<String> thisIsPresent = Optional.of("Hello World"); | |
assertEquals( | |
"hello world", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_empty_optional() { | |
Optional<String> thisIsPresent = Optional.empty(); | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_wrong_type_optional() { | |
Optional<Integer> thisIsPresent = Optional.of(5); | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_wrong_type_raw_optional() { | |
Optional thisIsPresent = Optional.of(5); | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_null() { | |
Optional<String> thisIsPresent = null; | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_unboxed_null() { | |
String thisIsPresent = null; | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_unboxed_value() { | |
String thisIsPresent = "Hello World"; | |
assertEquals( | |
"hello world", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_unboxed_value_unknown_type() { | |
Object thisIsPresent = "Hello World"; | |
assertEquals( | |
"hello world", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_unboxed_value_wrong_type() { | |
Integer thisIsPresent = 5; | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_unboxed_absent_unknown_type() { | |
Object thisIsPresent = null; | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_object_optional_empty() { | |
Object thisIsPresent = Optional.empty(); | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_object_optional() { | |
Object thisIsPresent = Optional.of("Hello World"); | |
assertEquals( | |
"hello world", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_raw_optional() { | |
Optional thisIsPresent = Optional.of("Hello World"); | |
assertEquals( | |
"hello world", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_empty_raw_optional() { | |
Optional thisIsPresent = Optional.empty(); | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_optionals_all_the_way_down() { | |
var thisIsPresent = Optional.of(Optional.of(Optional.of(Optional.of("Hello World")))); | |
assertEquals( | |
"hello world", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
static Object unwrap(Object o) { | |
if (o instanceof Optional<?> opt) { | |
return opt.isPresent() ? unwrap(opt.get()) : None; | |
} else if (o != null) { | |
return o; | |
} else { | |
return None; | |
} | |
} | |
static class None { | |
private None() {} | |
public static final None None = new None(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment