diff --git a/querydsl-core/src/main/java/com/mysema/query/types/expr/NumberExpression.java b/querydsl-core/src/main/java/com/mysema/query/types/expr/NumberExpression.java index 720bc8cd3..76493daad 100644 --- a/querydsl-core/src/main/java/com/mysema/query/types/expr/NumberExpression.java +++ b/querydsl-core/src/main/java/com/mysema/query/types/expr/NumberExpression.java @@ -13,8 +13,6 @@ */ package com.mysema.query.types.expr; -import java.math.BigDecimal; -import java.math.BigInteger; import java.util.ArrayList; import java.util.List; @@ -148,30 +146,8 @@ public abstract class NumberExpression> extends return castToNum(Byte.class); } - @SuppressWarnings("unchecked") private T cast(Number number) { - Class type = (Class) getType(); - if (type.isAssignableFrom(number.getClass())) { - return (T) number; - } else if (Byte.class.equals(type)) { - return (T) Byte.valueOf(number.byteValue()); - } else if (Double.class.equals(type)) { - return (T) Double.valueOf(number.doubleValue()); - } else if (Float.class.equals(type)) { - return (T) Float.valueOf(number.floatValue()); - } else if (Integer.class.equals(type)) { - return (T) Integer.valueOf(number.intValue()); - } else if (Long.class.equals(type)) { - return (T) Long.valueOf(number.longValue()); - } else if (Short.class.equals(type)) { - return (T) Short.valueOf(number.shortValue()); - } else if (BigInteger.class.equals(type)) { - return (T) new BigInteger(String.valueOf(number.longValue())); - } else if (BigDecimal.class.equals(type)) { - return (T) new BigDecimal(number.toString()); - } else { - throw new IllegalArgumentException("Unsupported target type : " + type.getName()); - } + return MathUtils.cast(number, getType()); } @Override @@ -355,14 +331,14 @@ public abstract class NumberExpression> extends public final > BooleanExpression between(@Nullable A from, @Nullable A to) { if (from == null) { if (to != null) { - return BooleanOperation.create(Ops.LOE, mixin, ConstantImpl.create(to)); + return loe(to); } else { throw new IllegalArgumentException("Either from or to needs to be non-null"); } } else if (to == null) { - return BooleanOperation.create(Ops.GOE, mixin, ConstantImpl.create(from)); + return goe(from); } else { - return BooleanOperation.create(Ops.BETWEEN, mixin, ConstantImpl.create(from), ConstantImpl.create(to)); + return between(ConstantImpl.create(cast(from)), ConstantImpl.create(cast(to))); } } diff --git a/querydsl-core/src/main/java/com/mysema/util/MathUtils.java b/querydsl-core/src/main/java/com/mysema/util/MathUtils.java index 2fcb05b96..c899302c0 100644 --- a/querydsl-core/src/main/java/com/mysema/util/MathUtils.java +++ b/querydsl-core/src/main/java/com/mysema/util/MathUtils.java @@ -63,7 +63,7 @@ public final class MathUtils { rv = type.cast(new BigInteger(num.toString())); } } else { - throw new IllegalArgumentException(String.format("Illegal type : %s", type.getSimpleName())); + throw new IllegalArgumentException(String.format("Unsupported target type : %s", type.getSimpleName())); } return rv; } diff --git a/querydsl-core/src/test/java/com/mysema/query/types/expr/NumberExpressionTest.java b/querydsl-core/src/test/java/com/mysema/query/types/expr/NumberExpressionTest.java index e3f086dc2..bb7c3b85a 100644 --- a/querydsl-core/src/test/java/com/mysema/query/types/expr/NumberExpressionTest.java +++ b/querydsl-core/src/test/java/com/mysema/query/types/expr/NumberExpressionTest.java @@ -25,12 +25,12 @@ public class NumberExpressionTest { @Test public void Between_Start_Given() { - assertEquals(intPath.goe(1), intPath.between(1, null)); + assertEquals(intPath.goe(1L), intPath.between(1L, null)); } @Test public void Between_End_Given() { - assertEquals(intPath.loe(3), intPath.between(null, 3)); + assertEquals(intPath.loe(3L), intPath.between(null, 3L)); } } diff --git a/querydsl-core/src/test/java/com/mysema/query/util/MathUtilsTest.java b/querydsl-core/src/test/java/com/mysema/query/util/MathUtilsTest.java deleted file mode 100644 index 79cc496e2..000000000 --- a/querydsl-core/src/test/java/com/mysema/query/util/MathUtilsTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2011, Mysema Ltd - * - * 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.mysema.query.util; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -import com.mysema.util.MathUtils; - -public class MathUtilsTest { - - @Test - public void Sum() { - assertEquals(Integer.valueOf(5), MathUtils.sum(2, 3.0)); - } - - @Test - public void Difference() { - assertEquals(Integer.valueOf(2), MathUtils.difference(5, 3.0)); - } - - @Test - public void Cast_Integer_To_Long() { - assertEquals(Long.valueOf(2), MathUtils.cast(2, Long.class)); - } - - @Test - public void Cast_Double_To_Long() { - assertEquals(Long.valueOf(3), MathUtils.cast(3.2, Long.class)); - } - -} diff --git a/querydsl-core/src/test/java/com/mysema/util/MathUtilsTest.java b/querydsl-core/src/test/java/com/mysema/util/MathUtilsTest.java index 06b4cdd8f..6ed5ea139 100644 --- a/querydsl-core/src/test/java/com/mysema/util/MathUtilsTest.java +++ b/querydsl-core/src/test/java/com/mysema/util/MathUtilsTest.java @@ -22,6 +22,16 @@ import org.junit.Test; public class MathUtilsTest { + @Test + public void Sum() { + assertEquals(Integer.valueOf(5), MathUtils.sum(2, 3.0)); + } + + @Test + public void Difference() { + assertEquals(Integer.valueOf(2), MathUtils.difference(5, 3.0)); + } + @Test public void Cast() { Integer value = Integer.valueOf(1); @@ -33,6 +43,15 @@ public class MathUtilsTest { assertEquals(Long.class, MathUtils.cast(value, Long.class).getClass()); assertEquals(Short.class, MathUtils.cast(value, Short.class).getClass()); assertEquals(Byte.class, MathUtils.cast(value, Byte.class).getClass()); + + assertEquals(BigDecimal.ONE, MathUtils.cast(value, BigDecimal.class)); + assertEquals(BigInteger.ONE, MathUtils.cast(value, BigInteger.class)); + assertEquals(Double.valueOf(1), MathUtils.cast(value, Double.class)); + assertEquals(Float.valueOf(1), MathUtils.cast(value, Float.class)); + assertEquals(Integer.valueOf(1), MathUtils.cast(value, Integer.class)); + assertEquals(Long.valueOf(1), MathUtils.cast(value, Long.class)); + assertEquals(Short.valueOf((short) 1), MathUtils.cast(value, Short.class)); + assertEquals(Byte.valueOf((byte) 1), MathUtils.cast(value, Byte.class)); } }