Last active
October 7, 2015 22:48
-
-
Save naosim/03cf9018e07d46db5f8a to your computer and use it in GitHub Desktop.
Either
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
interface Either<L, R> { | |
default Optional<L> left() { return Optional.empty(); } | |
default Optional<R> right() { return Optional.empty(); } | |
static <L, R> Either<L, R> left(L left) { | |
Optional<L> leftValue = Optional.ofNullable(left); | |
if(!leftValue.isPresent()) throw new NullPointerException("left value is null"); | |
return new Either<L, R>() { | |
@Override | |
public Optional<L> left() { | |
return leftValue; | |
} | |
}; | |
} | |
static <L, R> Either<L, R> right(R right) { | |
Optional<R> rightValue = Optional.ofNullable(right); | |
if(!rightValue.isPresent()) throw new NullPointerException("right value is null"); | |
return new Either<L, R>() { | |
@Override | |
public Optional<R> right() { | |
return rightValue; | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment