Last active
December 13, 2023 10:20
-
-
Save jbgi/3904e696fb27a2e33ae1 to your computer and use it in GitHub Desktop.
Generated code for @DaTa Expression
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
package org.derive4j.exemple; | |
import java.lang.Integer; | |
import java.lang.Object; | |
import java.lang.Override; | |
import java.util.Optional; | |
import java.util.function.Function; | |
import java.util.function.Supplier; | |
public final class Expressions { | |
private static final Expression.Cases<Optional<Integer>> valueGetter = Expressions.cases((value) -> Optional.of(value), | |
(left, right) -> Optional.empty(), | |
(left, right) -> Optional.empty(), | |
(expr) -> Optional.empty()); | |
private static final Expression.Cases<Optional<Expression>> leftGetter = Expressions.cases((value) -> Optional.empty(), | |
(left, right) -> Optional.of(left), | |
(left, right) -> Optional.of(left), | |
(expr) -> Optional.empty()); | |
private static final Expression.Cases<Optional<Expression>> rightGetter = Expressions.cases((value) -> Optional.empty(), | |
(left, right) -> Optional.of(right), | |
(left, right) -> Optional.of(right), | |
(expr) -> Optional.empty()); | |
private static final Expression.Cases<Optional<Expression>> exprGetter = Expressions.cases((value) -> Optional.empty(), | |
(left, right) -> Optional.empty(), | |
(left, right) -> Optional.empty(), | |
(expr) -> Optional.of(expr)); | |
private static final TotalMatchBuilderConst totalMatchBuilderConst = new TotalMatchBuilderConst(); | |
private Expressions() { | |
} | |
public static Expression Const(int value) { | |
return new Const(value); | |
} | |
public static Expression Add(Expression left, Expression right) { | |
return new Add(left, right); | |
} | |
public static Expression Mult(Expression left, Expression right) { | |
return new Mult(left, right); | |
} | |
public static Expression Neg(Expression expr) { | |
return new Neg(expr); | |
} | |
public static Expression lazy(Supplier<Expression> expression) { | |
return new Lazy(expression); | |
} | |
public static <R> Expression.Cases<R> cases(Function<Integer, R> Const, AddMapper<Expression, R> Add, | |
MultMapper<Expression, R> Mult, Function<Expression, R> Neg) { | |
return new LambdaCases<>(Const, Add, Mult, Neg); | |
} | |
public static <R> Function<Expression, R> cata(Function<Integer, R> Const, AddMapper<Supplier<R>, R> Add, | |
MultMapper<Supplier<R>, R> Mult, Function<Supplier<R>, R> Neg) { | |
Expression.Cases<R> cata = new Object() { | |
Expression.Cases<R> cata = Expressions.cases( | |
Const, | |
(left, right) -> Add.Add(() -> left.match(this.cata), | |
() -> right.match(this.cata)), | |
(left, right) -> Mult.Mult(() -> left.match(this.cata), | |
() -> right.match(this.cata)), (expr) -> Neg.apply(() -> expr.match(this.cata)) | |
); | |
; | |
}.cata; | |
return expression -> expression.match(cata); | |
} | |
public static Optional<Integer> getValue(Expression expression) { | |
return expression.match(valueGetter); | |
} | |
public static Optional<Expression> getLeft(Expression expression) { | |
return expression.match(leftGetter); | |
} | |
public static Optional<Expression> getRight(Expression expression) { | |
return expression.match(rightGetter); | |
} | |
public static Optional<Expression> getExpr(Expression expression) { | |
return expression.match(exprGetter); | |
} | |
public static Function<Expression, Expression> setValue(Integer newValue) { | |
return modValue(__ -> newValue); | |
} | |
public static Function<Expression, Expression> modValue(Function<Integer, Integer> valueMod) { | |
Expression.Cases<Expression> cases = Expressions.cases((value) -> Const(valueMod.apply(value)), | |
(left, right) -> Add(left, right), | |
(left, right) -> Mult(left, right), | |
(expr) -> Neg(expr)); | |
return expression -> expression.match(cases); | |
} | |
public static Function<Expression, Expression> setLeft(Expression newLeft) { | |
return modLeft(__ -> newLeft); | |
} | |
public static Function<Expression, Expression> modLeft(Function<Expression, Expression> leftMod) { | |
Expression.Cases<Expression> cases = Expressions.cases((value) -> Const(value), | |
(left, right) -> Add(leftMod.apply(left), right), | |
(left, right) -> Mult(leftMod.apply(left), right), | |
(expr) -> Neg(expr)); | |
return expression -> expression.match(cases); | |
} | |
public static Function<Expression, Expression> setRight(Expression newRight) { | |
return modRight(__ -> newRight); | |
} | |
public static Function<Expression, Expression> modRight(Function<Expression, Expression> rightMod) { | |
Expression.Cases<Expression> cases = Expressions.cases((value) -> Const(value), | |
(left, right) -> Add(left, rightMod.apply(right)), | |
(left, right) -> Mult(left, rightMod.apply(right)), | |
(expr) -> Neg(expr)); | |
return expression -> expression.match(cases); | |
} | |
public static Function<Expression, Expression> setExpr(Expression newExpr) { | |
return modExpr(__ -> newExpr); | |
} | |
public static Function<Expression, Expression> modExpr(Function<Expression, Expression> exprMod) { | |
Expression.Cases<Expression> cases = Expressions.cases((value) -> Const(value), | |
(left, right) -> Add(left, right), | |
(left, right) -> Mult(left, right), | |
(expr) -> Neg(exprMod.apply(expr))); | |
return expression -> expression.match(cases); | |
} | |
public static TotalMatchBuilderConst cases() { | |
return totalMatchBuilderConst; | |
} | |
private static final class Const extends Expression { | |
private final int value; | |
Const(int value) { | |
this.value = value; | |
} | |
@Override | |
public <R> R match(Expression.Cases<R> cases) { | |
return cases.Const(this.value); | |
} | |
} | |
private static final class Add extends Expression { | |
private final Expression left; | |
private final Expression right; | |
Add(Expression left, Expression right) { | |
this.left = left; | |
this.right = right; | |
} | |
@Override | |
public <R> R match(Expression.Cases<R> cases) { | |
return cases.Add(this.left, this.right); | |
} | |
} | |
private static final class Mult extends Expression { | |
private final Expression left; | |
private final Expression right; | |
Mult(Expression left, Expression right) { | |
this.left = left; | |
this.right = right; | |
} | |
@Override | |
public <R> R match(Expression.Cases<R> cases) { | |
return cases.Mult(this.left, this.right); | |
} | |
} | |
private static final class Neg extends Expression { | |
private final Expression expr; | |
Neg(Expression expr) { | |
this.expr = expr; | |
} | |
@Override | |
public <R> R match(Expression.Cases<R> cases) { | |
return cases.Neg(this.expr); | |
} | |
} | |
private static final class Lazy extends Expression { | |
private final Object lock = new Object(); | |
private Supplier<Expression> expression; | |
private volatile Expression evaluation; | |
Lazy(Supplier<Expression> expression) { | |
this.expression = expression; | |
} | |
private Expression eval() { | |
Expression _evaluation = this.evaluation; | |
if (_evaluation == null) { | |
synchronized (this.lock) { | |
_evaluation = this.evaluation; | |
if (_evaluation == null) { | |
this.evaluation = _evaluation = expression.get(); | |
this.expression = null; | |
} | |
} | |
} | |
return _evaluation; | |
} | |
@Override | |
public <R> R match(Expression.Cases<R> cases) { | |
return this.eval().match(cases); | |
} | |
} | |
public interface AddMapper<R_, R> { | |
R Add(R_ left, R_ right); | |
} | |
public interface MultMapper<R_, R> { | |
R Mult(R_ left, R_ right); | |
} | |
private static final class LambdaCases<R> implements Expression.Cases<R> { | |
private final Function<Integer, R> Const; | |
private final AddMapper<Expression, R> Add; | |
private final MultMapper<Expression, R> Mult; | |
private final Function<Expression, R> Neg; | |
LambdaCases(Function<Integer, R> Const, AddMapper<Expression, R> Add, MultMapper<Expression, R> Mult, Function<Expression, R> Neg) { | |
this.Const = Const; | |
this.Add = Add; | |
this.Mult = Mult; | |
this.Neg = Neg; | |
} | |
@Override | |
public R Const(int value) { | |
return this.Const.apply(value); | |
} | |
@Override | |
public R Add(Expression left, Expression right) { | |
return this.Add.Add(left, right); | |
} | |
@Override | |
public R Mult(Expression left, Expression right) { | |
return this.Mult.Mult(left, right); | |
} | |
@Override | |
public R Neg(Expression expr) { | |
return this.Neg.apply(expr); | |
} | |
} | |
public static final class TotalMatchBuilderConst { | |
private TotalMatchBuilderConst() { | |
} | |
public final <R> TotalMatchBuilderAdd<R> Const(Function<Integer, R> Const) { | |
return new TotalMatchBuilderAdd<>(Const); | |
} | |
public final <R> TotalMatchBuilderAdd<R> Const(R r) { | |
return this.Const((value) -> r); | |
} | |
public final <R> PartialMatchBuilderMult<R> Add(AddMapper<Expression, R> Add) { | |
return new PartialMatchBuilderMult<>(null, Add); | |
} | |
public final <R> PartialMatchBuilderMult<R> Add(R r) { | |
return this.Add((left, right) -> r); | |
} | |
public final <R> PartialMatchBuilderNeg<R> Mult(MultMapper<Expression, R> Mult) { | |
return new PartialMatchBuilderNeg<>(null, null, Mult); | |
} | |
public final <R> PartialMatchBuilderNeg<R> Mult(R r) { | |
return this.Mult((left, right) -> r); | |
} | |
public final <R> PartialMatchBuilder<R> Neg(Function<Expression, R> Neg) { | |
return new PartialMatchBuilder<>(null, null, null, Neg); | |
} | |
public final <R> PartialMatchBuilder<R> Neg(R r) { | |
return this.Neg((expr) -> r); | |
} | |
} | |
public static final class TotalMatchBuilderAdd<R> extends PartialMatchBuilder<R> { | |
private TotalMatchBuilderAdd(Function<Integer, R> Const) { | |
super(Const, null, null, null); | |
} | |
public final TotalMatchBuilderMult<R> Add(AddMapper<Expression, R> Add) { | |
return new TotalMatchBuilderMult<>(super.Const, Add); | |
} | |
public final TotalMatchBuilderMult<R> Add(R r) { | |
return this.Add((left, right) -> r); | |
} | |
public final PartialMatchBuilderNeg<R> Mult(MultMapper<Expression, R> Mult) { | |
return new PartialMatchBuilderNeg<>(super.Const, null, Mult); | |
} | |
public final PartialMatchBuilderNeg<R> Mult(R r) { | |
return this.Mult((left, right) -> r); | |
} | |
public final PartialMatchBuilder<R> Neg(Function<Expression, R> Neg) { | |
return new PartialMatchBuilder<>(super.Const, null, null, Neg); | |
} | |
public final PartialMatchBuilder<R> Neg(R r) { | |
return this.Neg((expr) -> r); | |
} | |
} | |
public static final class TotalMatchBuilderMult<R> extends PartialMatchBuilder<R> { | |
private TotalMatchBuilderMult(Function<Integer, R> Const, AddMapper<Expression, R> Add) { | |
super(Const, Add, null, null); | |
} | |
public final TotalMatchBuilderNeg<R> Mult(MultMapper<Expression, R> Mult) { | |
return new TotalMatchBuilderNeg<>(super.Const, super.Add, Mult); | |
} | |
public final TotalMatchBuilderNeg<R> Mult(R r) { | |
return this.Mult((left, right) -> r); | |
} | |
public final PartialMatchBuilder<R> Neg(Function<Expression, R> Neg) { | |
return new PartialMatchBuilder<>(super.Const, super.Add, null, Neg); | |
} | |
public final PartialMatchBuilder<R> Neg(R r) { | |
return this.Neg((expr) -> r); | |
} | |
} | |
public static final class TotalMatchBuilderNeg<R> extends PartialMatchBuilder<R> { | |
private TotalMatchBuilderNeg(Function<Integer, R> Const, AddMapper<Expression, R> Add, MultMapper<Expression, R> Mult) { | |
super(Const, Add, Mult, null); | |
} | |
public final Function<Expression, R> Neg(Function<Expression, R> Neg) { | |
Expression.Cases<R> cases = Expressions.cases(super.Const, super.Add, super.Mult, Neg); | |
return expression -> expression.match(cases); | |
} | |
public final Function<Expression, R> Neg(R r) { | |
return this.Neg((expr) -> r); | |
} | |
} | |
public static class PartialMatchBuilderAdd<R> extends PartialMatchBuilder<R> { | |
private PartialMatchBuilderAdd(Function<Integer, R> Const) { | |
super(Const, null, null, null); | |
} | |
public final PartialMatchBuilderMult<R> Add(AddMapper<Expression, R> Add) { | |
return new PartialMatchBuilderMult<>(super.Const, Add); | |
} | |
public final PartialMatchBuilderMult<R> Add(R r) { | |
return this.Add((left, right) -> r); | |
} | |
public final PartialMatchBuilderNeg<R> Mult(MultMapper<Expression, R> Mult) { | |
return new PartialMatchBuilderNeg<>(super.Const, null, Mult); | |
} | |
public final PartialMatchBuilderNeg<R> Mult(R r) { | |
return this.Mult((left, right) -> r); | |
} | |
public final PartialMatchBuilder<R> Neg(Function<Expression, R> Neg) { | |
return new PartialMatchBuilder<>(super.Const, null, null, Neg); | |
} | |
public final PartialMatchBuilder<R> Neg(R r) { | |
return this.Neg((expr) -> r); | |
} | |
} | |
public static class PartialMatchBuilderMult<R> extends PartialMatchBuilder<R> { | |
private PartialMatchBuilderMult(Function<Integer, R> Const, AddMapper<Expression, R> Add) { | |
super(Const, Add, null, null); | |
} | |
public final PartialMatchBuilderNeg<R> Mult(MultMapper<Expression, R> Mult) { | |
return new PartialMatchBuilderNeg<>(super.Const, super.Add, Mult); | |
} | |
public final PartialMatchBuilderNeg<R> Mult(R r) { | |
return this.Mult((left, right) -> r); | |
} | |
public final PartialMatchBuilder<R> Neg(Function<Expression, R> Neg) { | |
return new PartialMatchBuilder<>(super.Const, super.Add, null, Neg); | |
} | |
public final PartialMatchBuilder<R> Neg(R r) { | |
return this.Neg((expr) -> r); | |
} | |
} | |
public static class PartialMatchBuilderNeg<R> extends PartialMatchBuilder<R> { | |
private PartialMatchBuilderNeg(Function<Integer, R> Const, AddMapper<Expression, R> Add, MultMapper<Expression, R> Mult) { | |
super(Const, Add, Mult, null); | |
} | |
public final PartialMatchBuilder<R> Neg(Function<Expression, R> Neg) { | |
return new PartialMatchBuilder<>(super.Const, super.Add, super.Mult, Neg); | |
} | |
public final PartialMatchBuilder<R> Neg(R r) { | |
return this.Neg((expr) -> r); | |
} | |
} | |
public static class PartialMatchBuilder<R> { | |
private final Function<Integer, R> Const; | |
private final AddMapper<Expression, R> Add; | |
private final MultMapper<Expression, R> Mult; | |
private final Function<Expression, R> Neg; | |
private PartialMatchBuilder(Function<Integer, R> Const, AddMapper<Expression, R> Add, MultMapper<Expression, R> Mult, Function<Expression, R> Neg) { | |
this.Const = Const; | |
this.Add = Add; | |
this.Mult = Mult; | |
this.Neg = Neg; | |
} | |
public final Function<Expression, R> otherwise(Supplier<R> otherwise) { | |
Expression.Cases<R> cases = Expressions.cases(this.Const != null ? this.Const : (value) -> otherwise.get(), | |
this.Add != null ? this.Add : (left, right) -> otherwise.get(), | |
this.Mult != null ? this.Mult : (left, right) -> otherwise.get(), | |
this.Neg != null ? this.Neg : (expr) -> otherwise.get()); | |
return expression -> expression.match(cases); | |
} | |
public final Function<Expression, R> otherwise(R r) { | |
return this.otherwise(() -> r); | |
} | |
public final Function<Expression, Optional<R>> otherwiseEmpty() { | |
Expression.Cases<Optional<R>> cases = Expressions.cases((this.Const != null) ? (value) -> Optional.of(this.Const.apply(value)) | |
: (value) -> Optional.empty(), | |
(this.Add != null) ? (left, right) -> Optional.of(this.Add.Add(left, right)) | |
: (left, right) -> Optional.empty(), | |
(this.Mult != null) ? (left, right) -> Optional.of(this.Mult.Mult(left, right)) | |
: (left, right) -> Optional.empty(), | |
(this.Neg != null) ? (expr) -> Optional.of(this.Neg.apply(expr)) | |
: (expr) -> Optional.empty()); | |
return expression -> expression.match(cases); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment