eu.lucaventuri.functional.Either Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fibry Show documentation
Show all versions of fibry Show documentation
The first Java Actor System supporting fibers from Project Loom
package eu.lucaventuri.functional;
import eu.lucaventuri.common.ConsumerEx;
import eu.lucaventuri.common.Exceptions;
import java.util.Optional;
/** Functional Either, containing or one type or another */
public class Either {
private final L left;
private final R right;
private Either(L left, R right) {
this.left = left;
this.right = right;
}
public static Either left(L value) {
Exceptions.assertAndThrow(value != null, "Left value is null!");
return new Either(value, null);
}
public static Either right(R value) {
Exceptions.assertAndThrow(value != null, "Right value is null!");
return new Either(null, value);
}
public boolean isLeft() {
return left != null;
}
public boolean isRight() {
return right != null;
}
public L left() {
return left;
}
public R right() {
return right;
}
public Optional leftOpt() {
return Optional.ofNullable(left);
}
public Optional rightOpt() {
return Optional.ofNullable(right);
}
public void ifLeft(ConsumerEx consumer) throws E {
if (left != null)
consumer.accept(left);
}
public void ifRight(ConsumerEx consumer) throws E {
if (right != null)
consumer.accept(right);
}
public void ifEither(ConsumerEx consumerLeft, ConsumerEx consumerRight) throws E {
if (left != null)
consumerLeft.accept(left);
else
consumerRight.accept(right);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy