diff --git a/querydsl-core/src/main/java/com/mysema/query/types/expr/ENumber.java b/querydsl-core/src/main/java/com/mysema/query/types/expr/ENumber.java index d907d6c01..661ffdd1a 100644 --- a/querydsl-core/src/main/java/com/mysema/query/types/expr/ENumber.java +++ b/querydsl-core/src/main/java/com/mysema/query/types/expr/ENumber.java @@ -26,20 +26,20 @@ import com.mysema.query.types.operation.Ops.MathOps; @SuppressWarnings("serial") public abstract class ENumber> extends EComparableBase { - private static final ENumber random; + @SuppressWarnings("unchecked") + private static final ENumber[] bytes = new ENumber[256]; @SuppressWarnings("unchecked") private static final ENumber[] ints = new ENumber[256]; @SuppressWarnings("unchecked") - private static final ENumber[] bytes = new ENumber[256]; + private static final ENumber[] longs = new ENumber[256]; + + private static final ENumber random; @SuppressWarnings("unchecked") private static final ENumber[] shorts = new ENumber[256]; - @SuppressWarnings("unchecked") - private static final ENumber[] longs = new ENumber[256]; - static{ random = ONumber.create(Double.class, MathOps.RANDOM); for (int i = 0; i < 256; i++){ @@ -50,18 +50,6 @@ public abstract class ENumber> extends ECompara } } - /** - * Factory method - * - * @param - * @param val - * @return - */ - @SuppressWarnings("unchecked") - public static > ENumber create(T val){ - return new ENumberConst((Class)val.getClass(), Assert.notNull(val,"val is null")); - } - public static ENumber create(byte i){ if (i >= 0 && i < 256){ return bytes[i]; @@ -70,11 +58,11 @@ public abstract class ENumber> extends ECompara } } - public static ENumber create(short i){ + public static ENumber create(int i){ if (i >= 0 && i < 256){ - return shorts[i]; + return ints[i]; }else{ - return new ENumberConst(Short.class, Short.valueOf(i)); + return new ENumberConst(Integer.class, Integer.valueOf(i)); } } @@ -86,14 +74,26 @@ public abstract class ENumber> extends ECompara } } - public static ENumber create(int i){ + public static ENumber create(short i){ if (i >= 0 && i < 256){ - return ints[i]; + return shorts[i]; }else{ - return new ENumberConst(Integer.class, Integer.valueOf(i)); + return new ENumberConst(Short.class, Short.valueOf(i)); } } + /** + * Factory method + * + * @param + * @param val + * @return + */ + @SuppressWarnings("unchecked") + public static > ENumber create(T val){ + return new ENumberConst((Class)val.getClass(), Assert.notNull(val,"val is null")); + } + /** * @return max(left, right) */ @@ -120,6 +120,8 @@ public abstract class ENumber> extends ECompara private volatile ENumber avg, sqrt; + private volatile ENumber negation; + private volatile ENumber round, floor, ceil; public ENumber(Class type) { @@ -140,18 +142,18 @@ public abstract class ENumber> extends ECompara * @param right * @return this + right */ - public ENumber add(Number right) { - return ONumber.create(getType(), Ops.ADD, this, ENumber.create(right)); + public > ENumber add(Expr right) { + return ONumber.create(getType(), Ops.ADD, this, right); } /** * @param right * @return this + right */ - public > ENumber add(Expr right) { - return ONumber.create(getType(), Ops.ADD, this, right); + public ENumber add(Number right) { + return ONumber.create(getType(), Ops.ADD, this, ENumber.create(right)); } - + /** * @return avg(this) */ @@ -223,18 +225,19 @@ public abstract class ENumber> extends ECompara * @param right * @return this / right */ - public > ENumber div(N right) { - return ONumber.create(Double.class, Ops.DIV, this, ENumber.create(right)); + public > ENumber div(Expr right) { + return ONumber.create(Double.class, Ops.DIV, this, right); } /** * @param right * @return this / right */ - public > ENumber div(Expr right) { - return ONumber.create(Double.class, Ops.DIV, this, right); + public > ENumber div(N right) { + return ONumber.create(Double.class, Ops.DIV, this, ENumber.create(right)); } + /** * Get the double expression of this numeric expression * @@ -245,7 +248,6 @@ public abstract class ENumber> extends ECompara return castToNum(Double.class); } - /** * Get the float expression of this numeric expression * @@ -402,6 +404,14 @@ public abstract class ENumber> extends ECompara } return min; } + + /** + * @param right + * @return this * right + */ + public > ENumber mult(Expr right) { + return ONumber.create(getType(), Ops.MULT, this, right); + } /** * @param right @@ -412,13 +422,17 @@ public abstract class ENumber> extends ECompara } /** - * @param right - * @return this * right + * Get the negation of this expression + * + * @return this * -1 */ - public > ENumber mult(Expr right) { - return ONumber.create(getType(), Ops.MULT, this, right); + public ENumber negate(){ + if (negation == null){ + negation = mult(-1); + } + return negation; } - + /** * @return round(this) * @see java.lang.Math#round(double) @@ -451,6 +465,14 @@ public abstract class ENumber> extends ECompara } return sqrt; } + + /** + * @param right + * @return this - right + */ + public > ENumber sub(Expr right) { + return ONumber.create(getType(), Ops.SUB, this, right); + } /** * @param right @@ -460,14 +482,6 @@ public abstract class ENumber> extends ECompara return ONumber.create(getType(), Ops.SUB, this, ENumber.create(right)); } - /** - * @param right - * @return this - right - */ - public > ENumber sub(Expr right) { - return ONumber.create(getType(), Ops.SUB, this, right); - } - /** * @return sum(this) */