Created
January 4, 2013 13:42
-
-
Save anonymous/4452675 to your computer and use it in GitHub Desktop.
Here is an Option class (and sub-classes) compatible with JDK 8. Its implementation has been inspired from Scala and Tony Morris' article http://blog.tmorris.net/maybe-in-java/
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 my.util; | |
import java.util.Collections; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.NoSuchElementException; | |
import java.util.function.Function; | |
public abstract class Option<T> implements Iterable<T> { | |
private Option() { | |
// idiom that declares Option final for outer classes only | |
// equivalent to keyword *sealed* from Scala | |
} | |
public abstract boolean isEmpty(); | |
public abstract T get(); | |
public <U> Option<U> map(Function<? super T, U> function) { | |
if (isEmpty()) { | |
return None(); | |
} | |
return Some(function.apply(get())); | |
} | |
public <U> Option<U> flatMap(Function<? super T, Option<U>> function) { | |
if (isEmpty()) { | |
return None(); | |
} | |
return function.apply(get()); | |
} | |
public static <U> Option<U> Some(U value) { | |
return new Some<U>() { | |
@Override | |
public U get() { | |
return value; | |
} | |
}; | |
} | |
public static <U> Option<U> None() { | |
return (Option<U>) NONE; | |
} | |
public List<T> toList() { | |
if (isEmpty()) { | |
return Collections.emptyList(); | |
} | |
return Collections.singletonList(get()); | |
} | |
@Override | |
public Iterator<T> iterator() { | |
return toList().iterator(); | |
} | |
private static final Option<?> NONE = new Option<Object>() { | |
@Override | |
public boolean isEmpty() { | |
return true; | |
} | |
@Override | |
public Object get() { | |
throw new NoSuchElementException(); | |
} | |
}; | |
private static abstract class Some<U> extends Option<U> { | |
@Override | |
public boolean isEmpty() { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The implementation here is not complete. You can add operations like getOrElse or filter.
Here are two uses of this class.
Suppose that service1.getInteger() and service2.getInteger() both return Option.
If you get 1 from service1 and 2 from service2, then:
If one of service1 or service2 return None, then:
Here is another use:
If you get 1 from service1 and 2 from service2, then result == 3.
If one of service1 or service2 return None, then result == 0.