mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
Merge pull request #1045 from querydsl/numberexpression_cast
Merge NumberExpression.cast() and MathUtils.cast()
This commit is contained in:
commit
fd572698d3
@ -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<T extends Number & Comparable<?>> extends
|
||||
return castToNum(Byte.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private T cast(Number number) {
|
||||
Class<T> type = (Class<T>) 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<T extends Number & Comparable<?>> extends
|
||||
public final <A extends Number & Comparable<?>> 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)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user