Search in sources :

Example 1 with InMemoryExpressionEvaluator

use of org.datanucleus.query.inmemory.InMemoryExpressionEvaluator in project datanucleus-core by datanucleus.

the class QueryUtils method orderCandidates.

/**
 * Convenience method to order the input List of objects to the ordering defined by the compilation.
 * @param candidates Candidates
 * @param ordering Ordering expression(s)
 * @param state Map of state information (see JavaQueryEvaluator)
 * @param candidateAlias Candidate alias
 * @param ec ExecutionContext
 * @param clr ClassLoader resolver
 * @param parameterValues Any parameter values (maybe used by the ordering clause)
 * @param imports Imports for the query
 * @param queryLanguage The language of this query (JDOQL, JPQL etc)
 * @return The ordered List of candidates
 */
public static List orderCandidates(List candidates, final Expression[] ordering, final Map state, final String candidateAlias, final ExecutionContext ec, final ClassLoaderResolver clr, final Map parameterValues, final Imports imports, final String queryLanguage) {
    if (ordering == null) {
        // Nothing to do
        return candidates;
    }
    Object[] o = candidates.toArray();
    Arrays.sort(o, new Comparator() {

        public int compare(Object obj1, Object obj2) {
            for (int i = 0; i < ordering.length; i++) {
                state.put(candidateAlias, obj1);
                Object a = ordering[i].evaluate(new InMemoryExpressionEvaluator(ec, parameterValues, state, imports, clr, candidateAlias, queryLanguage));
                state.put(candidateAlias, obj2);
                Object b = ordering[i].evaluate(new InMemoryExpressionEvaluator(ec, parameterValues, state, imports, clr, candidateAlias, queryLanguage));
                if (a instanceof InMemoryFailure || b instanceof InMemoryFailure) {
                    throw new NucleusException("Error encountered in in-memory evaluation of ordering. Consult the log for details");
                }
                OrderExpression orderExpr = (OrderExpression) ordering[i];
                // Put any null values at the end
                if (a == null && b == null) {
                    return 0;
                } else if (a == null) {
                    if (orderExpr.getNullOrder() != null) {
                        // Use specified null handling
                        return orderExpr.getNullOrder() == NullOrderingType.NULLS_FIRST ? 1 : -1;
                    }
                    // Default to putting nulls at the end
                    return -1;
                } else if (b == null) {
                    if (orderExpr.getNullOrder() != null) {
                        // Use specified null handling
                        return orderExpr.getNullOrder() == NullOrderingType.NULLS_FIRST ? -1 : 1;
                    }
                    // Default to putting nulls at the end
                    return 1;
                } else {
                    int result = ((Comparable) a).compareTo(b);
                    if (result != 0) {
                        if (orderExpr.getSortOrder() == null || orderExpr.getSortOrder().equals("ascending")) {
                            // Ascending
                            return result;
                        }
                        // Descending
                        return -1 * result;
                    }
                }
            }
            return 0;
        }
    });
    return Arrays.asList(o);
}
Also used : InMemoryExpressionEvaluator(org.datanucleus.query.inmemory.InMemoryExpressionEvaluator) InMemoryFailure(org.datanucleus.query.inmemory.InMemoryFailure) OrderExpression(org.datanucleus.query.expression.OrderExpression) NucleusException(org.datanucleus.exceptions.NucleusException) Comparator(java.util.Comparator)

Aggregations

Comparator (java.util.Comparator)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 OrderExpression (org.datanucleus.query.expression.OrderExpression)1 InMemoryExpressionEvaluator (org.datanucleus.query.inmemory.InMemoryExpressionEvaluator)1 InMemoryFailure (org.datanucleus.query.inmemory.InMemoryFailure)1