UnboundPredicate(Operation op, NamedReference namedRef, T value) { super(op, namedRef, Literals.from(value)); }
@Override @SuppressWarnings("unchecked") public <T> Literal<T> to(Type type) { switch (type.typeId()) { case FLOAT: if ((double) Float.MAX_VALUE < value()) { return aboveMax(); } else if ((double) -Float.MAX_VALUE > value()) { // Compare with -Float.MAX_VALUE because it is the most negative float value. // Float.MIN_VALUE is the smallest non-negative floating point value. return belowMin(); } return (Literal<T>) new FloatLiteral(value().floatValue()); case DOUBLE: return (Literal<T>) this; case DECIMAL: int scale = ((Types.DecimalType) type).scale(); return (Literal<T>) new DecimalLiteral( BigDecimal.valueOf(value()).setScale(scale, RoundingMode.HALF_UP)); default: return null; } } }
@Test public void testLongToIntegerConversion() { Literal<Long> lit = Literal.of(34L); Literal<Integer> intLit = lit.to(Types.IntegerType.get()); Assert.assertEquals("Value should match", 34, (int) intLit.value()); Assert.assertEquals("Values above Integer.MAX_VALUE should be Literals.aboveMax()", Literals.aboveMax(), Literal.of((long) Integer.MAX_VALUE + 1L).to(Types.IntegerType.get())); Assert.assertEquals("Values below Integer.MIN_VALUE should be Literals.belowMin()", Literals.belowMin(), Literal.of((long) Integer.MIN_VALUE - 1L).to(Types.IntegerType.get())); }
@Test public void testDoubleToFloatConversion() { Literal<Double> lit = Literal.of(34.56D); Literal<Float> doubleLit = lit.to(Types.FloatType.get()); Assert.assertEquals("Value should match", 34.56F, doubleLit.value(), 0.001D); // this adjusts Float.MAX_VALUE using multipliers because most integer adjustments are lost by // floating point precision. Assert.assertEquals("Values above Float.MAX_VALUE should be Literals.aboveMax()", Literals.aboveMax(), Literal.of(2 * ((double) Float.MAX_VALUE)).to(Types.FloatType.get())); Assert.assertEquals("Values below Float.MIN_VALUE should be Literals.belowMin()", Literals.belowMin(), Literal.of(-2 * ((double) Float.MAX_VALUE)).to(Types.FloatType.get())); }