Skip to content

Instantly share code, notes, and snippets.

@naosim
Last active October 7, 2015 22:48
Show Gist options
  • Save naosim/03cf9018e07d46db5f8a to your computer and use it in GitHub Desktop.
Save naosim/03cf9018e07d46db5f8a to your computer and use it in GitHub Desktop.
Either
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