public static Number aggregate(Collection<Number> source, Expression<?> expr, Operator aggregator) {
        @SuppressWarnings("unchecked") // This is a number expression
        Class<Number> numberType = (Class<Number>) expr.getType();
        if (aggregator == Ops.AggOps.AVG_AGG) {
            Number sum = reduce(source, SUM);
            return sum.doubleValue() / source.size();
        } else if (aggregator == Ops.AggOps.COUNT_AGG) {
            return (long) source.size();
        } else if (aggregator == Ops.AggOps.COUNT_DISTINCT_AGG) {
            if (!Set.class.isInstance(source)) {
                source = new HashSet<>(source);
            }
            return (long) source.size();
        } else if (aggregator == Ops.AggOps.MAX_AGG) {
            return MathUtils.cast(reduce(source, MAX), numberType);
        } else if (aggregator == Ops.AggOps.MIN_AGG) {
            return MathUtils.cast(reduce(source, MIN), numberType);
        } else if (aggregator == Ops.AggOps.SUM_AGG) {
            return MathUtils.cast(reduce(source, SUM), numberType);
        } else {
            throw new IllegalArgumentException("Unknown operator " + aggregator);
        }
    }
{@code CollQueryFunctions} defines function implementation for use in ColQueryTemplates

@author tiwe
querydsl-collections/src/main/java/com/querydsl/collections/CollQueryFunctions.java