From 9664daf8b3d109ea34964cd0584a903190112c47 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Sat, 31 Dec 2016 17:53:48 +0000 Subject: [PATCH] ditch BIG_INT value type git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/exprs@1077 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/expr/Value.java | 7 +-- .../jackcess/impl/expr/BaseDateValue.java | 7 --- .../jackcess/impl/expr/BaseDelayedValue.java | 5 -- .../jackcess/impl/expr/BaseValue.java | 5 -- .../jackcess/impl/expr/BigDecimalValue.java | 6 -- .../jackcess/impl/expr/BigIntegerValue.java | 62 ------------------- .../jackcess/impl/expr/BuiltinOperators.java | 51 +++------------ .../jackcess/impl/expr/DoubleValue.java | 6 -- .../jackcess/impl/expr/LongValue.java | 6 -- 9 files changed, 12 insertions(+), 143 deletions(-) delete mode 100644 src/main/java/com/healthmarketscience/jackcess/impl/expr/BigIntegerValue.java diff --git a/src/main/java/com/healthmarketscience/jackcess/expr/Value.java b/src/main/java/com/healthmarketscience/jackcess/expr/Value.java index 23a29ab..2996772 100644 --- a/src/main/java/com/healthmarketscience/jackcess/expr/Value.java +++ b/src/main/java/com/healthmarketscience/jackcess/expr/Value.java @@ -17,7 +17,6 @@ limitations under the License. package com.healthmarketscience.jackcess.expr; import java.math.BigDecimal; -import java.math.BigInteger; import java.util.Date; /** @@ -28,7 +27,7 @@ public interface Value { public enum Type { - NULL, STRING, DATE, TIME, DATE_TIME, LONG, DOUBLE, BIG_INT, BIG_DEC; + NULL, STRING, DATE, TIME, DATE_TIME, LONG, DOUBLE, BIG_DEC; public boolean isNumeric() { return inRange(LONG, BIG_DEC); @@ -36,7 +35,7 @@ public interface Value public boolean isIntegral() { // note when BOOLEAN is converted to number, it is integral - return ((this == LONG) || (this == BIG_INT)); + return (this == LONG); } public boolean isTemporal() { @@ -69,7 +68,5 @@ public interface Value public Double getAsDouble(); - public BigInteger getAsBigInteger(); - public BigDecimal getAsBigDecimal(); } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseDateValue.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseDateValue.java index 68ad69f..1c96ba8 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseDateValue.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseDateValue.java @@ -17,11 +17,9 @@ limitations under the License. package com.healthmarketscience.jackcess.impl.expr; import java.math.BigDecimal; -import java.math.BigInteger; import java.text.DateFormat; import java.util.Date; -import com.healthmarketscience.jackcess.expr.Value; import com.healthmarketscience.jackcess.impl.ColumnImpl; /** @@ -72,11 +70,6 @@ public abstract class BaseDateValue extends BaseValue return getNumber(); } - @Override - public BigInteger getAsBigInteger() { - return getAsBigDecimal().toBigInteger(); - } - @Override public BigDecimal getAsBigDecimal() { return BigDecimal.valueOf(getNumber()); diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseDelayedValue.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseDelayedValue.java index 87910be..c75f2b5 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseDelayedValue.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseDelayedValue.java @@ -3,7 +3,6 @@ package com.healthmarketscience.jackcess.impl.expr; import java.math.BigDecimal; -import java.math.BigInteger; import java.util.Date; import com.healthmarketscience.jackcess.expr.Value; @@ -58,10 +57,6 @@ public abstract class BaseDelayedValue implements Value return getDelegate().getAsDouble(); } - public BigInteger getAsBigInteger() { - return getDelegate().getAsBigInteger(); - } - public BigDecimal getAsBigDecimal() { return getDelegate().getAsBigDecimal(); } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseValue.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseValue.java index db40372..109412b 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseValue.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BaseValue.java @@ -17,7 +17,6 @@ limitations under the License. package com.healthmarketscience.jackcess.impl.expr; import java.math.BigDecimal; -import java.math.BigInteger; import java.util.Date; import com.healthmarketscience.jackcess.expr.Value; @@ -52,10 +51,6 @@ public abstract class BaseValue implements Value throw invalidConversion(Value.Type.DOUBLE); } - public BigInteger getAsBigInteger() { - throw invalidConversion(Value.Type.BIG_INT); - } - public BigDecimal getAsBigDecimal() { throw invalidConversion(Value.Type.BIG_DEC); } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BigDecimalValue.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BigDecimalValue.java index d3ff58e..3b64c51 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BigDecimalValue.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BigDecimalValue.java @@ -17,7 +17,6 @@ limitations under the License. package com.healthmarketscience.jackcess.impl.expr; import java.math.BigDecimal; -import java.math.BigInteger; /** * @@ -55,11 +54,6 @@ public class BigDecimalValue extends BaseNumericValue return _val.toPlainString(); } - @Override - public BigInteger getAsBigInteger() { - return _val.toBigInteger(); - } - @Override public BigDecimal getAsBigDecimal() { return _val; diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BigIntegerValue.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BigIntegerValue.java deleted file mode 100644 index fce73ec..0000000 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BigIntegerValue.java +++ /dev/null @@ -1,62 +0,0 @@ -/* -Copyright (c) 2016 James Ahlborn - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package com.healthmarketscience.jackcess.impl.expr; - -import java.math.BigDecimal; -import java.math.BigInteger; - -/** - * - * @author James Ahlborn - */ -public class BigIntegerValue extends BaseNumericValue -{ - private final BigInteger _val; - - public BigIntegerValue(BigInteger val) - { - _val = val; - } - - public Type getType() { - return Type.BIG_INT; - } - - public Object get() { - return _val; - } - - @Override - protected Number getNumber() { - return _val; - } - - @Override - public boolean getAsBoolean() { - return (_val.compareTo(BigInteger.ZERO) != 0L); - } - - @Override - public BigInteger getAsBigInteger() { - return _val; - } - - @Override - public BigDecimal getAsBigDecimal() { - return new BigDecimal(_val); - } -} diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BuiltinOperators.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BuiltinOperators.java index b05bc16..0329426 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BuiltinOperators.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BuiltinOperators.java @@ -19,7 +19,6 @@ package com.healthmarketscience.jackcess.impl.expr; import java.math.BigDecimal; import java.math.BigInteger; import java.text.DateFormat; -import java.text.Format; import java.util.Date; import java.util.regex.Pattern; @@ -86,8 +85,6 @@ public class BuiltinOperators return toValue(-param1.getAsLong()); case DOUBLE: return toValue(-param1.getAsDouble()); - case BIG_INT: - return toValue(param1.getAsBigInteger().negate()); case BIG_DEC: return toValue(param1.getAsBigDecimal().negate()); default: @@ -117,8 +114,6 @@ public class BuiltinOperators return toValue(param1.getAsLong() + param2.getAsLong()); case DOUBLE: return toValue(param1.getAsDouble() + param2.getAsDouble()); - case BIG_INT: - return toValue(param1.getAsBigInteger().add(param2.getAsBigInteger())); case BIG_DEC: return toValue(param1.getAsBigDecimal().add(param2.getAsBigDecimal())); default: @@ -146,8 +141,6 @@ public class BuiltinOperators return toValue(param1.getAsLong() - param2.getAsLong()); case DOUBLE: return toValue(param1.getAsDouble() - param2.getAsDouble()); - case BIG_INT: - return toValue(param1.getAsBigInteger().subtract(param2.getAsBigInteger())); case BIG_DEC: return toValue(param1.getAsBigDecimal().subtract(param2.getAsBigDecimal())); default: @@ -172,8 +165,6 @@ public class BuiltinOperators return toValue(param1.getAsLong() * param2.getAsLong()); case DOUBLE: return toValue(param1.getAsDouble() * param2.getAsDouble()); - case BIG_INT: - return toValue(param1.getAsBigInteger().multiply(param2.getAsBigInteger())); case BIG_DEC: return toValue(param1.getAsBigDecimal().multiply(param2.getAsBigDecimal())); default: @@ -207,14 +198,6 @@ public class BuiltinOperators throw new ArithmeticException(DIV_BY_ZERO); } return toValue(param1.getAsDouble() / d2); - case BIG_INT: - BigInteger bip1 = param1.getAsBigInteger(); - BigInteger bip2 = param2.getAsBigInteger(); - BigInteger[] res = bip1.divideAndRemainder(bip2); - if(res[1].compareTo(BigInteger.ZERO) == 0) { - return toValue(res[0]); - } - return toValue(new BigDecimal(bip1).divide(new BigDecimal(bip2))); case BIG_DEC: return toValue(param1.getAsBigDecimal().divide(param2.getAsBigDecimal())); default: @@ -242,10 +225,9 @@ public class BuiltinOperators case DOUBLE: wasDouble = true; // fallthrough - case BIG_INT: case BIG_DEC: - BigInteger result = param1.getAsBigInteger().divide( - param2.getAsBigInteger()); + BigInteger result = getAsBigInteger(param1).divide( + getAsBigInteger(param2)); return (wasDouble ? toValue(result.longValue()) : toValue(result)); default: throw new RuntimeException("Unexpected type " + mathType); @@ -264,17 +246,8 @@ public class BuiltinOperators double result = Math.pow(param1.getAsDouble(), param2.getAsDouble()); // attempt to convert integral types back to integrals if possible - switch(mathType) { - case LONG: - if(isIntegral(result)) { - return toValue((long)result); - } - break; - case BIG_INT: - if(isIntegral(result)) { - return toValue(BigDecimal.valueOf(result).toBigInteger()); - } - break; + if((mathType == Value.Type.LONG) && isIntegral(result)) { + return toValue((long)result); } return toValue(result); @@ -300,10 +273,9 @@ public class BuiltinOperators case DOUBLE: wasDouble = true; // fallthrough - case BIG_INT: case BIG_DEC: - BigInteger bi1 = param1.getAsBigInteger(); - BigInteger bi2 = param2.getAsBigInteger().abs(); + BigInteger bi1 = getAsBigInteger(param1); + BigInteger bi2 = getAsBigInteger(param2).abs(); if(bi2.signum() == 0) { throw new ArithmeticException(DIV_BY_ZERO); } @@ -585,8 +557,6 @@ public class BuiltinOperators return param1.getAsLong().compareTo(param2.getAsLong()); case DOUBLE: return param1.getAsDouble().compareTo(param2.getAsDouble()); - case BIG_INT: - return param1.getAsBigInteger().compareTo(param2.getAsBigInteger()); case BIG_DEC: return param1.getAsBigDecimal().compareTo(param2.getAsBigDecimal()); default: @@ -594,10 +564,6 @@ public class BuiltinOperators } } - private static int compare(long l1, long l2) { - return ((l1 < l2) ? -1 : ((l1 > l2) ? 1 : 0)); - } - public static Value toValue(boolean b) { return (b ? TRUE_VAL : FALSE_VAL); } @@ -615,7 +581,7 @@ public class BuiltinOperators } public static Value toValue(BigInteger s) { - return new BigIntegerValue(s); + return toValue(new BigDecimal(s)); } public static Value toValue(BigDecimal s) { @@ -734,4 +700,7 @@ public class BuiltinOperators return ((d == Math.rint(d)) && !Double.isInfinite(d) && !Double.isNaN(d)); } + private static BigInteger getAsBigInteger(Value v) { + return v.getAsBigDecimal().toBigInteger(); + } } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DoubleValue.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DoubleValue.java index edaf887..7fcd840 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DoubleValue.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DoubleValue.java @@ -17,7 +17,6 @@ limitations under the License. package com.healthmarketscience.jackcess.impl.expr; import java.math.BigDecimal; -import java.math.BigInteger; /** * @@ -55,11 +54,6 @@ public class DoubleValue extends BaseNumericValue return _val; } - @Override - public BigInteger getAsBigInteger() { - return getAsBigDecimal().toBigInteger(); - } - @Override public BigDecimal getAsBigDecimal() { return BigDecimal.valueOf(_val); diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/LongValue.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/LongValue.java index 8501082..16ac83d 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/LongValue.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/LongValue.java @@ -17,7 +17,6 @@ limitations under the License. package com.healthmarketscience.jackcess.impl.expr; import java.math.BigDecimal; -import java.math.BigInteger; /** * @@ -55,11 +54,6 @@ public class LongValue extends BaseNumericValue return _val; } - @Override - public BigInteger getAsBigInteger() { - return BigInteger.valueOf(_val); - } - @Override public BigDecimal getAsBigDecimal() { return BigDecimal.valueOf(_val); -- 2.39.5