diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/TruthOperator.java |
Publish
Diffstat (limited to 'searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/TruthOperator.java')
-rw-r--r-- | searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/TruthOperator.java | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/TruthOperator.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/TruthOperator.java new file mode 100644 index 00000000000..26e8b183c21 --- /dev/null +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/TruthOperator.java @@ -0,0 +1,48 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.searchlib.rankingexpression.rule; + +import java.io.Serializable; + +/** + * A mathematical operator + * + * @author <a href="mailto:bratseth@yahoo-inc.com">Jon Bratseth</a> + */ +public enum TruthOperator implements Serializable { + + SMALLER("<") { public boolean evaluate(double x, double y) { return x<y; } }, + SMALLEREQUAL("<=") { public boolean evaluate(double x, double y) { return x<=y; } }, + EQUAL("==") { public boolean evaluate(double x, double y) { return x==y; } }, + APPROX_EQUAL("~=") { public boolean evaluate(double x, double y) { return approxEqual(x,y); } }, + LARGER(">") { public boolean evaluate(double x, double y) { return x>y; } }, + LARGEREQUAL(">=") { public boolean evaluate(double x, double y) { return x>=y; } }; + + private final String operatorString; + + TruthOperator(String operatorString) { + this.operatorString=operatorString; + } + + /** Perform the truth operation on the input */ + public abstract boolean evaluate(double x, double y); + + public @Override String toString() { return operatorString; } + + public static TruthOperator fromString(String string) { + for (TruthOperator operator : values()) + if (operator.toString().equals(string)) + return operator; + throw new IllegalArgumentException("Illegal truth operator '" + string + "'"); + } + + private static boolean approxEqual(double x,double y) { + if (y < -1.0 || y > 1.0) { + x = Math.nextAfter(x/y, 1.0); + y = 1.0; + } else { + x = Math.nextAfter(x, y); + } + return x==y; + } + +}
\ No newline at end of file |