Skip to content

Instantly share code, notes, and snippets.

@methylene
Created February 28, 2016 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save methylene/e1b1e60ac12791f21fab to your computer and use it in GitHub Desktop.
Save methylene/e1b1e60ac12791f21fab to your computer and use it in GitHub Desktop.
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