diff options
author | Jon Bratseth <bratseth@gmail.com> | 2022-09-27 11:37:13 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2022-09-27 11:37:13 +0200 |
commit | 6b97667072635b74fdd784b132f77722b1c12be4 (patch) | |
tree | b801cb420367da166a8675de8d3f0ff0fa28eaf1 /searchlib/src/main | |
parent | 373eac0314d2c916c6203d49474cf5adaa775b1c (diff) |
Simplify: Use lambda
Diffstat (limited to 'searchlib/src/main')
-rw-r--r-- | searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ArithmeticOperator.java | 58 |
1 files changed, 32 insertions, 26 deletions
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ArithmeticOperator.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ArithmeticOperator.java index 959045a63a0..0a657c8ce58 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ArithmeticOperator.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ArithmeticOperator.java @@ -4,6 +4,7 @@ package com.yahoo.searchlib.rankingexpression.rule; import com.yahoo.searchlib.rankingexpression.evaluation.Value; import java.util.List; +import java.util.function.BiFunction; /** * A mathematical operator @@ -12,40 +13,43 @@ import java.util.List; */ public enum ArithmeticOperator { - OR(0, "||") { public Value evaluate(Value x, Value y) { - return x.or(y); - }}, - AND(1, "&&") { public Value evaluate(Value x, Value y) { - return x.and(y); - }}, - PLUS(2, "+") { public Value evaluate(Value x, Value y) { - return x.add(y); - }}, - MINUS(3, "-") { public Value evaluate(Value x, Value y) { - return x.subtract(y); - }}, - MULTIPLY(4, "*") { public Value evaluate(Value x, Value y) { - return x.multiply(y); - }}, - DIVIDE(5, "/") { public Value evaluate(Value x, Value y) { - return x.divide(y); - }}, - MODULO(6, "%") { public Value evaluate(Value x, Value y) { - return x.modulo(y); - }}, - POWER(7, "^") { public Value evaluate(Value x, Value y) { - return x.power(y); - }}; +/* +struct Sub : OperatorHelper<Sub> { Sub() : Helper("-", 101, LEFT) {}}; +struct Mul : OperatorHelper<Mul> { Mul() : Helper("*", 102, LEFT) {}}; +struct Div : OperatorHelper<Div> { Div() : Helper("/", 102, LEFT) {}}; +struct Mod : OperatorHelper<Mod> { Mod() : Helper("%", 102, LEFT) {}}; +struct Pow : OperatorHelper<Pow> { Pow() : Helper("^", 103, RIGHT) {}}; +struct Equal : OperatorHelper<Equal> { Equal() : Helper("==", 10, LEFT) {}}; +struct NotEqual : OperatorHelper<NotEqual> { NotEqual() : Helper("!=", 10, LEFT) {}}; +struct Approx : OperatorHelper<Approx> { Approx() : Helper("~=", 10, LEFT) {}}; +struct Less : OperatorHelper<Less> { Less() : Helper("<", 10, LEFT) {}}; +struct LessEqual : OperatorHelper<LessEqual> { LessEqual() : Helper("<=", 10, LEFT) {}}; +struct Greater : OperatorHelper<Greater> { Greater() : Helper(">", 10, LEFT) {}}; +struct GreaterEqual : OperatorHelper<GreaterEqual> { GreaterEqual() : Helper(">=", 10, LEFT) {}}; +struct And : OperatorHelper<And> { And() : Helper("&&", 2, LEFT) {}}; +struct Or : OperatorHelper<Or> { Or() : Helper("||", 1, LEFT) {}}; + */ + + OR(0, "||", (x, y) -> x.or(y)), + AND(1, "&&", (x, y) -> x.and(y)), + PLUS(2, "+", (x, y) -> x.add(y)), + MINUS(3, "-", (x, y) -> x.subtract(y)), + MULTIPLY(4, "*", (x, y) -> x.multiply(y)), + DIVIDE(5, "/", (x, y) -> x.divide(y)), + MODULO(6, "%", (x, y) -> x.modulo(y)), + POWER(7, "^", (x, y) -> x.power(y)); /** A list of all the operators in this in order of decreasing precedence */ public static final List<ArithmeticOperator> operatorsByPrecedence = List.of(POWER, MODULO, DIVIDE, MULTIPLY, MINUS, PLUS, AND, OR); private final int precedence; private final String image; + private final BiFunction<Value, Value, Value> function; - private ArithmeticOperator(int precedence, String image) { + ArithmeticOperator(int precedence, String image, BiFunction<Value, Value, Value> function) { this.precedence = precedence; this.image = image; + this.function = function; } /** Returns true if this operator has precedence over the given operator */ @@ -53,7 +57,9 @@ public enum ArithmeticOperator { return precedence > op.precedence; } - public abstract Value evaluate(Value x, Value y); + public final Value evaluate(Value x, Value y) { + return function.apply(x, y); + } @Override public String toString() { |