com.purbon.kafka.topology.utils.Either Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of julie-ops Show documentation
Show all versions of julie-ops Show documentation
A helper project for Kafka Platform teams to build an automated Topic, Configuration, Schemas, and more,
Management solution.
The newest version!
package com.purbon.kafka.topology.utils;
import java.util.Optional;
import java.util.function.Function;
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) {
return new Either(value, null);
}
public static Either Right(R value) {
return new Either(null, value);
}
public Optional getLeft() {
return Optional.ofNullable(left);
}
public Optional getRight() {
return Optional.ofNullable(right);
}
public boolean isLeft() {
return left != null;
}
public boolean isRight() {
return right != null;
}
public Optional mapLeft(Function mapper) {
if (isLeft()) {
return Optional.of(mapper.apply(left));
}
return Optional.empty();
}
public Optional mapRight(Function mapper) {
if (isRight()) {
return Optional.of(mapper.apply(right));
}
return Optional.empty();
}
public static Function lift(CheckedFunction function) {
return t -> {
try {
return Either.Left(function.apply(t));
} catch (Exception ex) {
return Either.Right(ex);
}
};
}
public String toString() {
if (isLeft()) {
return "Left(" + left + ")";
}
return "Right(" + right + ")";
}
}