[main] commons.data with Either type, move muscript.dynamic to commons.data.dynamic

This commit is contained in:
Johannes Frohnmeyer 2022-06-29 15:41:44 +02:00
parent 34c431efc1
commit 62ab9e930d
Signed by: Johannes
GPG Key ID: E76429612C2929F4
51 changed files with 201 additions and 111 deletions

View File

@ -1,4 +1,4 @@
package io.gitlab.jfronny.muscript.dynamic;
package io.gitlab.jfronny.commons.data.dynamic;
public interface DBool extends Dynamic<Boolean> {
}

View File

@ -1,4 +1,4 @@
package io.gitlab.jfronny.muscript.dynamic;
package io.gitlab.jfronny.commons.data.dynamic;
import java.util.function.*;

View File

@ -1,4 +1,4 @@
package io.gitlab.jfronny.muscript.dynamic;
package io.gitlab.jfronny.commons.data.dynamic;
import io.gitlab.jfronny.commons.StringFormatter;

View File

@ -1,4 +1,4 @@
package io.gitlab.jfronny.muscript.dynamic;
package io.gitlab.jfronny.commons.data.dynamic;
import java.util.*;

View File

@ -1,4 +1,4 @@
package io.gitlab.jfronny.muscript.dynamic;
package io.gitlab.jfronny.commons.data.dynamic;
import io.gitlab.jfronny.commons.StringFormatter;

View File

@ -1,4 +1,4 @@
package io.gitlab.jfronny.muscript.dynamic;
package io.gitlab.jfronny.commons.data.dynamic;
import java.util.*;

View File

@ -1,4 +1,4 @@
package io.gitlab.jfronny.muscript.dynamic;
package io.gitlab.jfronny.commons.data.dynamic;
public final class DNull implements Dynamic<Object> {
@Override

View File

@ -1,4 +1,4 @@
package io.gitlab.jfronny.muscript.dynamic;
package io.gitlab.jfronny.commons.data.dynamic;
public interface DNumber extends Dynamic<Double> {
}

View File

@ -1,6 +1,6 @@
package io.gitlab.jfronny.muscript.dynamic;
package io.gitlab.jfronny.commons.data.dynamic;
import java.util.*;
import java.util.Map;
public interface DObject extends Dynamic<Map<String, Dynamic<?>>> {
default Dynamic<?> get(String key) {

View File

@ -1,4 +1,4 @@
package io.gitlab.jfronny.muscript.dynamic;
package io.gitlab.jfronny.commons.data.dynamic;
public interface DString extends Dynamic<String> {
}

View File

@ -1,6 +1,6 @@
package io.gitlab.jfronny.muscript.dynamic;
package io.gitlab.jfronny.commons.data.dynamic;
import io.gitlab.jfronny.commons.*;
import io.gitlab.jfronny.commons.StringFormatter;
public interface Dynamic<T> {
T getValue();

View File

@ -1,6 +1,6 @@
package io.gitlab.jfronny.muscript.dynamic;
package io.gitlab.jfronny.commons.data.dynamic;
import io.gitlab.jfronny.muscript.error.*;
import io.gitlab.jfronny.muscript.error.LocationalException;
public class DynamicTypeConversionException extends RuntimeException {
private final String target;

View File

@ -1,6 +1,7 @@
package io.gitlab.jfronny.muscript;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.DObject;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import java.util.*;

View File

@ -1,11 +1,13 @@
package io.gitlab.jfronny.muscript;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.DList;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import java.text.*;
import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import static io.gitlab.jfronny.muscript.dynamic.DFinal.*;
import static io.gitlab.jfronny.commons.data.dynamic.DFinal.*;
public class StandardLib {
private static final Random rnd = new Random();

View File

@ -1,8 +1,8 @@
package io.gitlab.jfronny.muscript.compiler.expr;
import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.Type;
import io.gitlab.jfronny.muscript.compiler.expr.dynamic.unpack.*;
import io.gitlab.jfronny.muscript.dynamic.*;
public abstract non-sealed class DynamicExpr extends Expr<Dynamic<?>> {
public DynamicExpr(int character) {

View File

@ -1,11 +1,11 @@
package io.gitlab.jfronny.muscript.compiler.expr;
import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.compiler.expr.annotations.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.Type;
import io.gitlab.jfronny.muscript.compiler.expr.annotations.CanThrow;
import io.gitlab.jfronny.muscript.compiler.expr.common.literal.*;
import io.gitlab.jfronny.muscript.compiler.expr.string.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.muscript.error.*;
import io.gitlab.jfronny.muscript.compiler.expr.string.StringCoerce;
import io.gitlab.jfronny.muscript.error.TypeMismatchException;
@CanThrow
public sealed abstract class Expr<T> permits BoolExpr, DynamicExpr, NullLiteral, NumberExpr, StringExpr {

View File

@ -1,10 +1,11 @@
package io.gitlab.jfronny.muscript.compiler.expr;
import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.compiler.expr.annotations.*;
import io.gitlab.jfronny.muscript.compiler.expr.common.literal.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.muscript.error.*;
import io.gitlab.jfronny.commons.data.dynamic.DNull;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.Type;
import io.gitlab.jfronny.muscript.compiler.expr.annotations.CanThrow;
import io.gitlab.jfronny.muscript.compiler.expr.common.literal.DynamicLiteral;
import io.gitlab.jfronny.muscript.error.LocationalException;
@CanThrow
public final class NullLiteral extends Expr<Object> {

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.bool;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.BoolExpr;
public class And extends BoolExpr {
private final BoolExpr left;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.bool;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.BoolExpr;
public class Not extends BoolExpr {
private final BoolExpr inner;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.bool;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.BoolExpr;
public class Or extends BoolExpr {
private final BoolExpr left;

View File

@ -1,7 +1,8 @@
package io.gitlab.jfronny.muscript.compiler.expr.common;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.BoolExpr;
import io.gitlab.jfronny.muscript.compiler.expr.Expr;
import java.util.*;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.common.conditional;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.BoolExpr;
public class BoolConditional extends BoolExpr {
public final BoolExpr condition;

View File

@ -1,8 +1,9 @@
package io.gitlab.jfronny.muscript.compiler.expr.common.conditional;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.muscript.error.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.BoolExpr;
import io.gitlab.jfronny.muscript.compiler.expr.DynamicExpr;
import io.gitlab.jfronny.muscript.error.TypeMismatchException;
public class DynamicConditional extends DynamicExpr {
public final BoolExpr condition;

View File

@ -1,7 +1,8 @@
package io.gitlab.jfronny.muscript.compiler.expr.common.conditional;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.BoolExpr;
import io.gitlab.jfronny.muscript.compiler.expr.NumberExpr;
public class NumberConditional extends NumberExpr {
public final BoolExpr condition;

View File

@ -1,7 +1,8 @@
package io.gitlab.jfronny.muscript.compiler.expr.common.conditional;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.BoolExpr;
import io.gitlab.jfronny.muscript.compiler.expr.StringExpr;
public class StringConditional extends StringExpr {
public final BoolExpr condition;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.common.literal;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.BoolExpr;
public final class BoolLiteral extends BoolExpr {
private final boolean value;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.common.literal;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.DynamicExpr;
public final class DynamicLiteral<T> extends DynamicExpr {
private final Dynamic<T> value;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.common.literal;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.NumberExpr;
public final class NumberLiteral extends NumberExpr {
private final double value;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.common.literal;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.StringExpr;
public final class StringLiteral extends StringExpr {
private final String value;

View File

@ -1,8 +1,8 @@
package io.gitlab.jfronny.muscript.compiler.expr.dynamic;
import io.gitlab.jfronny.commons.data.dynamic.*;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.compiler.expr.annotations.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import java.util.*;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.dynamic;
import io.gitlab.jfronny.commons.data.dynamic.*;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
public class DynamicCoerce extends DynamicExpr {
private final Expr<?> inner;

View File

@ -1,12 +1,14 @@
package io.gitlab.jfronny.muscript.compiler.expr.dynamic;
import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.commons.data.dynamic.*;
import io.gitlab.jfronny.muscript.compiler.Type;
import io.gitlab.jfronny.muscript.compiler.expr.DynamicExpr;
import io.gitlab.jfronny.muscript.compiler.expr.Expr;
import io.gitlab.jfronny.muscript.compiler.expr.annotations.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.muscript.error.*;
import io.gitlab.jfronny.muscript.error.TypeMismatchException;
@CanThrow @UncheckedDynamic
@CanThrow
@UncheckedDynamic
public class Get extends DynamicExpr {
private final DynamicExpr left;
private final Expr<?> name;

View File

@ -1,11 +1,13 @@
package io.gitlab.jfronny.muscript.compiler.expr.dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.compiler.expr.annotations.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.muscript.error.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.DynamicExpr;
import io.gitlab.jfronny.muscript.compiler.expr.annotations.CanThrow;
import io.gitlab.jfronny.muscript.compiler.expr.annotations.UncheckedDynamic;
import io.gitlab.jfronny.muscript.error.LocationalException;
@CanThrow @UncheckedDynamic
@CanThrow
@UncheckedDynamic
public class Variable extends DynamicExpr {
private final String name;

View File

@ -1,10 +1,11 @@
package io.gitlab.jfronny.muscript.compiler.expr.dynamic.unpack;
import io.gitlab.jfronny.commons.data.dynamic.*;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.compiler.expr.annotations.*;
import io.gitlab.jfronny.muscript.dynamic.*;
@CanThrow @UncheckedDynamic
@CanThrow
@UncheckedDynamic
public class BoolUnpack extends BoolExpr {
private final DynamicExpr inner;

View File

@ -1,10 +1,13 @@
package io.gitlab.jfronny.muscript.compiler.expr.dynamic.unpack;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.commons.data.dynamic.DynamicTypeConversionException;
import io.gitlab.jfronny.muscript.compiler.expr.DynamicExpr;
import io.gitlab.jfronny.muscript.compiler.expr.NumberExpr;
import io.gitlab.jfronny.muscript.compiler.expr.annotations.*;
import io.gitlab.jfronny.muscript.dynamic.*;
@CanThrow @UncheckedDynamic
@CanThrow
@UncheckedDynamic
public class NumberUnpack extends NumberExpr {
private final DynamicExpr inner;

View File

@ -1,10 +1,14 @@
package io.gitlab.jfronny.muscript.compiler.expr.dynamic.unpack;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.compiler.expr.annotations.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.commons.data.dynamic.DynamicTypeConversionException;
import io.gitlab.jfronny.muscript.compiler.expr.DynamicExpr;
import io.gitlab.jfronny.muscript.compiler.expr.StringExpr;
import io.gitlab.jfronny.muscript.compiler.expr.annotations.CanThrow;
import io.gitlab.jfronny.muscript.compiler.expr.annotations.UncheckedDynamic;
@CanThrow @UncheckedDynamic
@CanThrow
@UncheckedDynamic
public class StringUnpack extends StringExpr {
private final DynamicExpr inner;

View File

@ -1,7 +1,8 @@
package io.gitlab.jfronny.muscript.compiler.expr.number.compare;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.BoolExpr;
import io.gitlab.jfronny.muscript.compiler.expr.NumberExpr;
public class Greater extends BoolExpr {
private final NumberExpr left;

View File

@ -1,7 +1,8 @@
package io.gitlab.jfronny.muscript.compiler.expr.number.compare;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.BoolExpr;
import io.gitlab.jfronny.muscript.compiler.expr.NumberExpr;
public class Less extends BoolExpr {
private final NumberExpr left;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.number.math;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.NumberExpr;
public class Divide extends NumberExpr {
private final NumberExpr dividend;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.number.math;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.NumberExpr;
public class Invert extends NumberExpr {
private final NumberExpr inner;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.number.math;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.NumberExpr;
public class Minus extends NumberExpr {
private final NumberExpr minuend;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.number.math;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.NumberExpr;
public class Modulo extends NumberExpr {
private final NumberExpr dividend;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.number.math;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.NumberExpr;
public class Multiply extends NumberExpr {
private final NumberExpr multiplier;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.number.math;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.NumberExpr;
public class Plus extends NumberExpr {
private final NumberExpr augend;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.number.math;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.NumberExpr;
public class Power extends NumberExpr {
private final NumberExpr base;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.muscript.compiler.expr.string;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
public class Concatenate extends StringExpr {
private final StringExpr left;

View File

@ -1,8 +1,9 @@
package io.gitlab.jfronny.muscript.compiler.expr.string;
import io.gitlab.jfronny.commons.*;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.StringFormatter;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.Expr;
import io.gitlab.jfronny.muscript.compiler.expr.StringExpr;
public class StringCoerce extends StringExpr {
private final Expr<?> inner;

View File

@ -1,10 +1,10 @@
package io.gitlab.jfronny.muscript.compiler.expr.unresolved;
import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.Type;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.compiler.expr.annotations.*;
import io.gitlab.jfronny.muscript.compiler.expr.annotations.CanThrow;
import io.gitlab.jfronny.muscript.compiler.expr.common.conditional.*;
import io.gitlab.jfronny.muscript.dynamic.*;
@CanThrow
public class UnresolvedConditional extends DynamicExpr {

View File

@ -1,9 +1,10 @@
package io.gitlab.jfronny.muscript.debug;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import java.lang.reflect.*;
import java.util.*;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
public class ObjectGraphPrinter {
public static String printGraph(Object o) throws IllegalAccessException {

View File

@ -1,13 +1,13 @@
package io.gitlab.jfronny.muscript.test;
import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.debug.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.Parser;
import io.gitlab.jfronny.muscript.compiler.expr.Expr;
import io.gitlab.jfronny.muscript.debug.ObjectGraphPrinter;
import java.util.*;
import static io.gitlab.jfronny.muscript.dynamic.DFinal.*;
import static io.gitlab.jfronny.commons.data.dynamic.DFinal.*;
public class MuTestUtil {
public static double number(String source) {

View File

@ -0,0 +1,66 @@
package io.gitlab.jfronny.commons.data;
import java.util.function.Consumer;
import java.util.function.Function;
@SuppressWarnings("unchecked")
public final class Either<T1, T2> {
public static <T1, T2> Either<T1, T2> left(T1 value) {
return new Either<>(value, true);
}
public static <T1, T2> Either<T1, T2> right(T2 value) {
return new Either<>(value, false);
}
private final Object value;
private final boolean isLeft;
private Either(Object value, boolean isLeft) {
this.value = value;
this.isLeft = isLeft;
}
public boolean isLeft() {
return isLeft;
}
public boolean isRight() {
return !isLeft;
}
public T1 left() {
if (!isLeft) throw new IllegalStateException("This Either does not represent a left value");
return (T1) value;
}
public T2 right() {
if (isLeft) throw new IllegalStateException("This Either does not represent a right value");
return (T2) value;
}
public void apply(Consumer<? super T1> lFunc, Consumer<? super T2> rFunc) {
if (isLeft) relax(lFunc).accept(value);
else relax(rFunc).accept(value);
}
public <T3> T3 fold(Function<? super T1, ? extends T3> lFunc, Function<? super T2, ? extends T3> rFunc) {
return isLeft ? relax(lFunc).apply(value) : relax(rFunc).apply(value);
}
public <T3> Either<T3, T2> mapLeft(Function<? super T1, ? extends T3> func) {
return isLeft ? Either.left(relax(func).apply(value)) : Either.right((T2) value);
}
public <T3> Either<T1, T3> mapRight(Function<? super T2, ? extends T3> func) {
return isLeft ? Either.left((T1) value) : Either.right(relax(func).apply(value));
}
private static Consumer<Object> relax(Consumer<?> func) {
return (Consumer<Object>) func;
}
private static <T> Function<Object, T> relax(Function<?, T> func) {
return (Function<Object, T>) func;
}
}