/** * Create a typed array projection for the given type and expressions * * @param <T> * @param type type of the projection * @param exprs arguments for the projection * @return */ public static <T> ArrayConstructorExpression<T> array(Class<T[]> type, Expression<T>... exprs) { return new ArrayConstructorExpression<T>(type, exprs); }
@Override public boolean equals(Object obj) { if (obj == this) { return true; } else if (obj instanceof FactoryExpression<?>) { FactoryExpression<?> c = (FactoryExpression<?>)obj; return args.equals(c.getArgs()) && getType().equals(c.getType()); } else { return false; } }
/** * @param projection * @return */ public static FactoryExpression<?> wrap(List<? extends Expression<?>> projection) { boolean usesFactoryExpressions = false; for (Expression<?> e : projection) { usesFactoryExpressions |= e instanceof FactoryExpression; } if (usesFactoryExpressions) { return wrap(new ArrayConstructorExpression( projection.toArray(new Expression[projection.size()]))); } else { return null; } }
private void order(QueryMetadata metadata, List<Expression<?>> sources, List<?> list) { // create a projection for the order List<OrderSpecifier<?>> orderBy = metadata.getOrderBy(); Expression<Object>[] orderByExpr = new Expression[orderBy.size()]; boolean[] directions = new boolean[orderBy.size()]; for (int i = 0; i < orderBy.size(); i++) { orderByExpr[i] = (Expression) orderBy.get(i).getTarget(); directions[i] = orderBy.get(i).getOrder() == Order.ASC; } Expression<?> expr = new ArrayConstructorExpression<Object>(Object[].class, orderByExpr); Evaluator orderEvaluator = evaluatorFactory.create(metadata, sources, expr); Collections.sort(list, new MultiComparator(orderEvaluator, directions)); }
@SuppressWarnings("unchecked") @Test public void ArrayProjection() { List<String[]> results = query().from(cat) .list(new ArrayConstructorExpression<String>(String[].class, cat.name)); assertFalse(results.isEmpty()); for (String[] result : results) { assertNotNull(result[0]); } }