Created
June 23, 2013 09:15
-
-
Save ppetr/5844381 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
public final class None<T> | |
extends AbstractCollection<T> | |
implements Option<T> | |
{ | |
public None() {} | |
public T get() { | |
throw new IllegalArgumentException("No value"); | |
} | |
public final T getOrElse(T value) { | |
return value; | |
} | |
public boolean isEmpty() { | |
return true; | |
} | |
@Override | |
public java.util.Iterator<T> iterator() { | |
return java.util.Collections.<T>emptySet().iterator(); | |
} | |
@Override | |
public final int size() { | |
return 0; | |
} | |
} |
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
public interface Option<T> | |
extends java.util.Collection<T> | |
{ | |
public T get(); | |
public T getOrElse(T deflt); | |
public boolean isEmpty(); | |
} |
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
public final class Some<T> | |
extends AbstractCollection<T> | |
implements Option<T> | |
{ | |
private final T value; | |
public Some(T value) { | |
this.value = value; | |
} | |
public T get() { | |
return value; | |
} | |
public final T getOrElse(T ignored) { | |
return get(); | |
} | |
public boolean isEmpty() { | |
return false; | |
} | |
@Override | |
public final java.util.Iterator<T> iterator() { | |
return java.util.Collections.<T>singleton(value).iterator(); | |
} | |
@Override | |
public final int size() { | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment