aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2021-11-03 16:56:45 +0100
committerHenning Baldersheim <balder@yahoo-inc.com>2021-11-03 16:56:45 +0100
commit4cc0c0885fd8b5f5e2fde130cfe409bbc9990455 (patch)
tree09e0d16df9c0db8b6bafaf82ee865844b86e5886 /searchlib/src/main/java
parent9d5f4971e89870ec61b1b309287c563ead7d8b75 (diff)
Add a LongValue to preserve integer numbers.
Diffstat (limited to 'searchlib/src/main/java')
-rw-r--r--searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/LongValue.java120
-rw-r--r--searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/Value.java6
2 files changed, 124 insertions, 2 deletions
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/LongValue.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/LongValue.java
new file mode 100644
index 00000000000..21a998d3234
--- /dev/null
+++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/LongValue.java
@@ -0,0 +1,120 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.searchlib.rankingexpression.evaluation;
+
+/**
+ * A representation for integer numbers
+ *
+ * @author balder
+ */
+public class LongValue extends DoubleCompatibleValue {
+ private final long value;
+
+ public LongValue(long value) {
+ this.value = value;
+ }
+ @Override
+ public double asDouble() {
+ return value;
+ }
+ @Override
+ public boolean asBoolean() {
+ return value != 0;
+ }
+
+ @Override
+ public Value asMutable() {
+ return new LongValue(value);
+ }
+
+ @Override
+ public String toString() {
+ return String.valueOf(value);
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (this==other) return true;
+ if ( ! (other instanceof Value)) return false;
+ if ( ! ((Value) other).hasDouble()) return false;
+ return new DoubleValue(value).equals(other);
+ }
+
+ @Override
+ public int hashCode() {
+ return Long.hashCode(value);
+ }
+
+ @Override
+ public DoubleValue negate() {
+ return new DoubleValue(-value);
+ }
+
+ private UnsupportedOperationException unsupported(String operation, Value value) {
+ return new UnsupportedOperationException("Cannot perform " + operation + " on " + value + " and " + this);
+ }
+
+ /** Returns this or a mutable copy assigned the given value */
+ private DoubleValue mutable(double value) {
+ return new DoubleValue(value);
+ }
+
+ @Override
+ public Value add(Value value) {
+ if (value instanceof TensorValue)
+ return value.add(this);
+
+ try {
+ return mutable(this.value + value.asDouble());
+ }
+ catch (UnsupportedOperationException e) {
+ throw unsupported("add",value);
+ }
+ }
+
+ @Override
+ public Value subtract(Value value) {
+ if (value instanceof TensorValue)
+ return value.negate().add(this);
+
+ try {
+ return mutable(this.value - value.asDouble());
+ }
+ catch (UnsupportedOperationException e) {
+ throw unsupported("subtract",value);
+ }
+ }
+
+ @Override
+ public Value multiply(Value value) {
+ if (value instanceof TensorValue)
+ return value.multiply(this);
+
+ try {
+ return mutable(this.value * value.asDouble());
+ }
+ catch (UnsupportedOperationException e) {
+ throw unsupported("multiply", value);
+ }
+ }
+
+ @Override
+ public Value divide(Value value) {
+ try {
+ return mutable(this.value / value.asDouble());
+ }
+ catch (UnsupportedOperationException e) {
+ throw unsupported("divide",value);
+ }
+ }
+
+ @Override
+ public Value modulo(Value value) {
+ try {
+ return mutable(this.value % value.asDouble());
+ }
+ catch (UnsupportedOperationException e) {
+ throw unsupported("modulo",value);
+ }
+ }
+
+}
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/Value.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/Value.java
index 793a0516be3..6490c69894f 100644
--- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/Value.java
+++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/Value.java
@@ -116,8 +116,10 @@ public abstract class Value {
return new StringValue(value);
else if (value.startsWith("{"))
return new TensorValue(Tensor.from(value));
- else
- return new DoubleValue(Double.parseDouble(value));
+ else if ((value.indexOf('.') == -1) && (value.indexOf('e') == -1) && (value.indexOf('E') == -1))
+ return new LongValue(Long.parseLong(value));
+ else
+ return new DoubleValue(Double.parseDouble(value));
}
public static Value of(Tensor tensor) {