Created
February 18, 2017 20:38
-
-
Save chRyNaN/d112df63b4a0671eb48168e0487cefde to your computer and use it in GitHub Desktop.
A Java 8 Optional representation using RxJava.
This file contains 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
import java.util.NoSuchElementException; | |
import rx.functions.Action1; | |
import rx.functions.Func1; | |
/** | |
* Created by ckeenan on 2/18/17. A Java 8 Optional representation using RxJava. | |
*/ | |
public class Optional<T> { | |
private static final Optional<?> EMPTY_OPTIONAL = new Optional<>(); | |
private final T value; | |
private Optional() { | |
this.value = null; | |
} | |
private Optional(final T value) { | |
if (value == null) { | |
throw new NullPointerException("The value provided to the Optional constructor must not be null."); | |
} | |
this.value = value; | |
} | |
@SuppressWarnings("unchecked") | |
public static <T> Optional<T> empty() { | |
return (Optional<T>) EMPTY_OPTIONAL; | |
} | |
public static <T> Optional<T> of(final T value) { | |
return new Optional<>(value); | |
} | |
public static <T> Optional<T> ofNullable(final T value) { | |
return value == null ? Optional.<T>empty() : of(value); | |
} | |
public T get() { | |
if (value == null) { | |
throw new NoSuchElementException("There is no value associated with this Optional object."); | |
} | |
return value; | |
} | |
public boolean ispresent() { | |
return value != null; | |
} | |
public void ifPresent(final Action1<T> action) { | |
if (value != null) { | |
action.call(value); | |
} | |
} | |
@SuppressWarnings("unchecked") | |
public Optional<T> filter(final Func1<T, Boolean> function) { | |
if (!ispresent()) { | |
return this; | |
} else { | |
return function.call(value) ? this : (Optional<T>) empty(); | |
} | |
} | |
public <U> Optional<U> map(final Func1<T, U> function) { | |
if (!ispresent()) { | |
return empty(); | |
} else { | |
return Optional.ofNullable(function.call(value)); | |
} | |
} | |
public <U> Optional<U> flatMap(final Func1<T, Optional<U>> function) { | |
if (!ispresent()) { | |
return empty(); | |
} else { | |
Optional<U> optional = function.call(value); | |
if (optional == null) { | |
throw new NullPointerException("Result of function in Optional flatMap method must not be null."); | |
} | |
return optional; | |
} | |
} | |
public T orElse(final T otherValue) { | |
return ispresent() ? value : otherValue; | |
} | |
public T orElseGet(final Func1<Void, T> function) { | |
return ispresent() ? value : function.call(null); | |
} | |
public <X extends Throwable> T orElseThrow(final Func1<Void, X> function) throws X { | |
if (ispresent()) { | |
return value; | |
} else { | |
throw function.call(null); | |
} | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
Optional<?> optional = (Optional<?>) o; | |
return value == optional.value || !(value == null || optional.value == null) && value.equals(optional.value); | |
} | |
@Override | |
public int hashCode() { | |
return value != null ? value.hashCode() : 0; | |
} | |
@Override | |
public String toString() { | |
return "Optional{" + | |
"value=" + value + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment