/** * Create a new Operation expression * * @param type type of expression * @param operator operator * @param args operation arguments * @return operation expression */ public static <T extends Number & Comparable<?>> NumberOperation<T> numberOperation(Class<? extends T> type, Operator operator, Expression<?>... args) { return new NumberOperation<T>(type, operator, args); }
/** * Create a new NumberExpression * * @param expr Expression of type Number * @return new NumberExpression */ public static <T extends Number & Comparable<?>> NumberExpression<T> asNumber(Expression<T> expr) { Expression<T> underlyingMixin = ExpressionUtils.extract(expr); if (underlyingMixin instanceof PathImpl) { return new NumberPath<T>((PathImpl<T>) underlyingMixin); } else if (underlyingMixin instanceof OperationImpl) { return new NumberOperation<T>((OperationImpl<T>) underlyingMixin); } else if (underlyingMixin instanceof TemplateExpressionImpl) { return new NumberTemplate<T>((TemplateExpressionImpl<T>) underlyingMixin); } else { return new NumberExpression<T>(underlyingMixin) { private static final long serialVersionUID = -8712299418891960222L; @Override public <R, C> R accept(Visitor<R, C> v, C context) { return this.mixin.accept(v, context); } }; } }
@SuppressWarnings("unchecked") @Test public void various() { Expression[] args = new Expression[]{new StringPath("x"), new StringPath("y")}; List<Operation<?>> operations = new ArrayList<Operation<?>>(); // paths.add(new ArrayOperation(String[].class, "p")); operations.add(new BooleanOperation(Ops.EQ, args)); operations.add(new ComparableOperation(String.class, Ops.SUBSTR_1ARG, args)); operations.add(new DateOperation(Date.class, Ops.DateTimeOps.CURRENT_DATE, args)); operations.add(new DateTimeOperation(Date.class,Ops.DateTimeOps.CURRENT_TIMESTAMP, args)); operations.add(new EnumOperation(ExampleEnum.class,Ops.ALIAS, args)); operations.add(new NumberOperation(Integer.class,Ops.ADD, args)); operations.add(new SimpleOperation(String.class,Ops.TRIM, args)); operations.add(new StringOperation(Ops.CONCAT, args)); operations.add(new TimeOperation(Time.class,Ops.DateTimeOps.CURRENT_TIME, args)); for (Operation<?> operation : operations) { Operation<?> other = ExpressionUtils.operation(operation.getType(), operation.getOperator(), ImmutableList.copyOf(operation.getArgs())); assertEquals(operation.toString(), operation.accept(ToStringVisitor.DEFAULT, Templates.DEFAULT)); assertEquals(operation.hashCode(), other.hashCode()); assertEquals(operation, other); assertNotNull(operation.getOperator()); assertNotNull(operation.getArgs()); assertNotNull(operation.getType()); } }
/** * Create a new Operation expression * * @param type type of expression * @param operator operator * @param args operation arguments * @return operation expression */ public static <T extends Number & Comparable<?>> NumberOperation<T> numberOperation(Class<? extends T> type, Operator operator, Expression<?>... args) { return new NumberOperation<T>(type, operator, args); }
/** * Create a new NumberExpression * * @param expr Expression of type Number * @return new NumberExpression */ public static <T extends Number & Comparable<?>> NumberExpression<T> asNumber(Expression<T> expr) { Expression<T> underlyingMixin = ExpressionUtils.extract(expr); if (underlyingMixin instanceof PathImpl) { return new NumberPath<T>((PathImpl<T>) underlyingMixin); } else if (underlyingMixin instanceof OperationImpl) { return new NumberOperation<T>((OperationImpl<T>) underlyingMixin); } else if (underlyingMixin instanceof TemplateExpressionImpl) { return new NumberTemplate<T>((TemplateExpressionImpl<T>) underlyingMixin); } else { return new NumberExpression<T>(underlyingMixin) { private static final long serialVersionUID = -8712299418891960222L; @Override public <R, C> R accept(Visitor<R, C> v, C context) { return this.mixin.accept(v, context); } }; } }