Created
February 28, 2016 09:12
-
-
Save methylene/e1b1e60ac12791f21fab 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 foo.bar; | |
import io.atlassian.fugue.Option; | |
import org.junit.Test; | |
import java.util.Objects; | |
import java.util.Optional; | |
// https://developer.atlassian.com/blog/2015/08/optional-broken/ | |
public class CoreTest { | |
@Test | |
public void optionTest() { | |
Optional.<String>empty() | |
.map(this::notNullString) | |
.ifPresent(x -> System.out.println("empty map: " + x)); | |
Option.<String>none() | |
.map(this::notNullString) | |
.forEach(x -> System.out.println("none map: " + x)); | |
Optional.<String>empty() | |
.flatMap(this::notEmptyString) | |
.ifPresent(x -> System.out.println("empty flatmap: " + x)); | |
Option.<String>none() | |
.flatMap(this::someString) | |
.forEach(x -> System.out.println("none flatmap: " + x)); | |
Optional.of("x").map(this::makeNull) | |
.map(this::notNullString) | |
.ifPresent(x -> System.out.println("mapping null, optional: " + x)); | |
Option.some("x").map(this::makeNull) | |
.map(this::notNullString) | |
.forEach(x -> System.out.println("mapping null, option: " + x)); | |
} | |
public String makeNull(String x) { | |
return null; | |
} | |
public String notNullString(String x) { | |
return Objects.toString(x, "got null"); | |
} | |
public Optional<String> notEmptyString(String x) { | |
return Optional.of(notNullString(x)); | |
} | |
public Option<String> someString(String x) { | |
return Option.some(notNullString(x)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment