private void testAndSetResult(final Number theResult) { if (_result == null) _result = theResult; else { final NumericComparisonVisitor visitor = new NumericComparisonVisitor(); final NumericPromotion promoter = new NumericPromotion(); promoter.promote(_result, theResult); promoter.accept(visitor); if (visitor.getComparison() == 0) _result = theResult; else _result = null; } }
/** * Takes an array of numeric values and converts all of them into the same _type, with 'minType' defining the minimum common _type. type become The common * type everything was converted to. */ private void promote(final Type minType, final Number... nums) { Type largest = findHighestType(nums); if (largest.isLessThan(minType)) largest = minType; prepArray(largest, nums.length); for (int i = 0; i < nums.length; i++) promote(nums[i], i, largest); _type = largest; }
/** * Return the highest _type seen in an array of Numbers. */ private static Type findHighestType(final Number[] nums) { Type largest = Type.BYTE; for (final Number num : nums) { final Type type = findType(num); if (largest.isLessThan(type)) largest = type; } return largest; }
public void promote(final Number... nums) { promote(Type.BIGINTEGER, nums); }
@Before public void setUp() { _promoter = new NumericPromotion(); }
private static void promotionTester(final NumericPromotion promoter, final Number... results) { promoter.accept(new EqualityAssertion(results)); }
public void promote(final Number... nums) { promote(Type.BIGINTEGER, nums); }
private void testAndSetResult(final Number theResult) { if (_result == null) _result = theResult; else { final NumericComparisonVisitor visitor = new NumericComparisonVisitor(); final NumericPromotion promoter = new NumericPromotion(); promoter.promote(_result, theResult); promoter.accept(visitor); if (visitor.getComparison() == 0) _result = theResult; else _result = null; } }
/** * Takes an array of numeric values and converts all of them into the same _type, with 'minType' defining the minimum common _type. type become The common * type everything was converted to. */ private void promote(final Type minType, final Number... nums) { Type largest = findHighestType(nums); if (largest.isLessThan(minType)) largest = minType; prepArray(largest, nums.length); for (int i = 0; i < nums.length; i++) promote(nums[i], i, largest); _type = largest; }
@Test public void decimalAndDouble() { final double pi = Math.PI; final BigDecimal pidec = new BigDecimal(Double.toString(pi)).multiply(new BigDecimal(Double.toString(pi))); _promoter.promote(pidec, pi); promotionTester(_promoter, pidec.doubleValue(), pi); _promoter.promote(pi, pidec); promotionTester(_promoter, pi, pidec.doubleValue()); }
/** * Return the highest _type seen in an array of Numbers. */ private static Type findHighestType(final Number[] nums) { Type largest = Type.BYTE; for (final Number num : nums) { final Type type = findType(num); if (largest.isLessThan(type)) largest = type; } return largest; }
@Override protected boolean test(final Literal a, final Literal b) { final Object aval = a.getValue(); final Object bval = b.getValue(); // Numbers are a special case, since they can be promoted from Integers and Decimals to Floats and Doubles. if (aval instanceof Number && bval instanceof Number) { final NumericPromotion promoter = new NumericPromotion(); final Number anum = (Number) aval; final Number bnum = (Number) bval; promoter.promote(anum, bnum); final NumericComparisonVisitor visitor = new NumericComparisonVisitor(); promoter.accept(visitor); if (visitor.getComparison() == 0) return true ^ _flip; return false ^ _flip; } if (a.getValue() != null && b.getValue() != null) return (aval.getClass().equals(bval.getClass()) && aval.equals(bval)) ^ _flip; return false; } }
@Test public void longAndBigInt() { final long l = 40000; final BigInteger big = new BigInteger("99999999999999999999999999999999999"); final BigInteger bigl = new BigInteger((new Long(l)).toString()); _promoter.promote(l, big); promotionTester(_promoter, bigl, big); _promoter.promote(big, l); promotionTester(_promoter, big, bigl); }
final Type type1 = findType(arg);
@Override protected boolean test(final Literal a, final Literal b) { final Object aval = a.getValue(); final Object bval = b.getValue(); // Numbers are a special case, since they can be promoted from Integers and Decimals to Floats and Doubles. if (aval instanceof Number && bval instanceof Number) { final NumericPromotion promoter = new NumericPromotion(); final Number anum = (Number) aval; final Number bnum = (Number) bval; promoter.promote(anum, bnum); final NumericComparisonVisitor visitor = new NumericComparisonVisitor(); promoter.accept(visitor); if (visitor.getComparison() == 0) return true ^ _flip; return false ^ _flip; } if (a.getValue() != null && b.getValue() != null) return (aval.getClass().equals(bval.getClass()) && aval.equals(bval)) ^ _flip; return false; } }
final Type type1 = findType(arg);
final NumericPromotion promoter = new NumericPromotion(); promoter.promote(numArgs); final FunctionApplicationVisitor visitor = new FunctionApplicationVisitor(_function, expectedNum); promoter.accept(visitor); promoter.promote(result); promoter.accept(converter); resultLit = converter.getLiteral();
@Test public void bigIntAndFloat() { final BigInteger big = new BigInteger("9999999999999999999999999999999999999999999999"); final float fl = 9876543210.0123456789f; final float bigf = big.floatValue(); _promoter.promote(big, fl); promotionTester(_promoter, bigf, fl); _promoter.promote(fl, big); promotionTester(_promoter, fl, bigf); }
final NumericPromotion promoter = new NumericPromotion(); promoter.promote(numArgs); final FunctionApplicationVisitor visitor = new FunctionApplicationVisitor(_function, expectedNum); promoter.accept(visitor); promoter.promote(result); promoter.accept(converter); resultLit = converter.getLiteral();