Option implements Iterable<A> { private Option() { } private  Option(); abstract B fold(Match<A, B> visitor); abstract Option<A> foreach(Function<? super A, Void> f); abstract Option<B> fmap(Function<? super A, ? extends B> f); Option<B> map(Function<? super A, ? extends B> f); abstract Option<B> bind(Function<A, Option<B>> f); Option<B> flatMap(Function<A, Option<B>> f); abstract boolean isSome(); boolean isNone(); Option<A> orElse(Option<A> none); Option<A> orElse(Function0<Option<A>> none); Option<A> orError(T none); Option<Tuple<A, B>> and(Option<B> b); abstract A get(); abstract A getOrElse(A none); abstract A getOrElse(Function0<A> none); abstract A getOrElseNull(); abstract List<A> list(); abstract Opt<A> toOpt(); @Override abstract int hashCode(); @Override abstract boolean equals(Object o); static Option<A> some(final A a); static Option<A> none(); static Option<A> none(A example); static Option<A> none(Class<A> clazz); static Option<A> option(A a); static Option<A> fromOpt(Opt<A> a); @Deprecated static Function0<A> error(final String message); static Function<String, Boolean> eq(final String compare);  }
@Test public void testOption() { Option<String> s = some("test"); Option<String> n = none(); assertTrue(s.isSome()); assertTrue(n.isNone()); assertEquals("a test", s.fold(new Match<String, String>() { public String some(String s) { return "a " + s; } public String none() { return ""; } })); assertEquals("none", n.fold(new Match<String, String>() { public String some(String s) { return s; } public String none() { return "none"; } })); for (String x : n) { fail("should not happen"); } String r = null; for (String x : s) { r = x; } assertEquals("test", r); assertEquals("test", s.getOrElse("")); assertEquals("", n.getOrElse("")); Function<String, Integer> len = new Function<String, Integer>() { @Override public Integer apply(String s) { return s.length(); } }; assertTrue(s.map(len).getOrElse(-1) == 4); assertTrue(n.map(len).getOrElse(-1) == -1); }