All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mysema.query.group.GroupByBuilder Maven / Gradle / Ivy

There is a newer version: 3.7.4
Show newest version
/*
 * Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mysema.query.group;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.mysema.commons.lang.CloseableIterator;
import com.mysema.query.ResultTransformer;
import com.mysema.query.types.Expression;
import com.mysema.query.types.FactoryExpression;
import com.mysema.query.types.FactoryExpressionUtils;

/**
 * GroupByBuilder is a fluent builder for GroupBy transformer instances. This class is not to be used directly,
 * but via GroupBy.
 *
 * @author tiwe
 *
 * @param 
 */
public class GroupByBuilder {

    private final Expression key;

    /**
     * Create a new GroupByBuilder for the given key expression
     *
     * @param key
     */
    public GroupByBuilder(Expression key) {
        this.key = key;
    }

    /**
     * Get the results as a map
     *
     * @param expressions
     * @return
     */
    public ResultTransformer> as(Expression... expressions) {
        return new GroupByMap(key, expressions);
    }

    /**
     * Get the results as a closeable iterator
     *
     * @param expressions
     * @return
     */
    public ResultTransformer> iterate(Expression... expressions) {
        return new GroupByIterate(key, expressions);
    }

    /**
     * Get the results as a list
     *
     * @param expressions
     * @return
     */
    public ResultTransformer> list(Expression... expressions) {
        return new GroupByList(key, expressions);
    }

    /**
     * Get the results as a map
     *
     * @param expression
     * @return
     */
    @SuppressWarnings("unchecked")
    public  ResultTransformer> as(Expression expression) {
        final Expression lookup = getLookup(expression);
        return new GroupByMap(key, expression) {
            @Override
            protected Map transform(Map groups) {
                Map results = new LinkedHashMap((int) Math.ceil(groups.size()/0.75), 0.75f);
                for (Map.Entry entry : groups.entrySet()) {
                    results.put(entry.getKey(), entry.getValue().getOne(lookup));
                }
                return results;
            }
        };
    }

    /**
     * Get the results as a closeable iterator
     *
     * @param expression
     * @return
     */
    public  ResultTransformer> iterate(Expression expression) {
        final Expression lookup = getLookup(expression);
        return new GroupByIterate(key, expression) {
            @Override
            protected V transform(Group group) {
                return group.getOne(lookup);
            }
        };
    }

    /**
     * Get the results as a list
     *
     * @param expression
     * @return
     */
    public  ResultTransformer> list(Expression expression) {
        final Expression lookup = getLookup(expression);
        return new GroupByList(key, expression) {
            @Override
            protected V transform(Group group) {
                return group.getOne(lookup);
            }
        };
    }

    private  Expression getLookup(Expression expression) {
        if (expression instanceof GroupExpression) {
            return ((GroupExpression)expression).getExpression();
        } else {
            return expression;
        }
    }

    /**
     * Get the results as a map
     *
     * @param expression
     * @return
     */
    public  ResultTransformer> as(FactoryExpression expression) {
        final FactoryExpression transformation = FactoryExpressionUtils.wrap(expression);
        List> args = transformation.getArgs();
        return new GroupByMap(key, args.toArray(new Expression[args.size()])) {

            @Override
            protected Map transform(Map groups) {
                Map results = new LinkedHashMap((int) Math.ceil(groups.size()/0.75), 0.75f);
                for (Map.Entry entry : groups.entrySet()) {
                    results.put(entry.getKey(), transform(entry.getValue()));
                }
                return results;
            }

            @SuppressWarnings("unchecked")
            protected V transform(Group group) {
                // XXX Isn't group.toArray() suitable here?
                List args = new ArrayList(groupExpressions.size() - 1);
                for (int i = 1; i < groupExpressions.size(); i++) {
                    args.add(group.getGroup(groupExpressions.get(i)));
                }
                return (V)transformation.newInstance(args.toArray());
            }

        };
    }

    /**
     * Get the results as a closeable iterator
     *
     * @param expression
     * @return
     */
    public  ResultTransformer> iterate(FactoryExpression expression) {
        final FactoryExpression transformation = FactoryExpressionUtils.wrap(expression);
        List> args = transformation.getArgs();
        return new GroupByIterate(key, args.toArray(new Expression[args.size()])) {
            @Override
            protected V transform(Group group) {
                // XXX Isn't group.toArray() suitable here?
                List args = new ArrayList(groupExpressions.size() - 1);
                for (int i = 1; i < groupExpressions.size(); i++) {
                    args.add(group.getGroup(groupExpressions.get(i)));
                }
                return (V)transformation.newInstance(args.toArray());
            }
        };
    }

    /**
     * Get the results as a list
     *
     * @param expression
     * @return
     */
    public  ResultTransformer> list(FactoryExpression expression) {
        final FactoryExpression transformation = FactoryExpressionUtils.wrap(expression);
        List> args = transformation.getArgs();
        return new GroupByList(key, args.toArray(new Expression[args.size()])) {
            @Override
            protected V transform(Group group) {
                // XXX Isn't group.toArray() suitable here?
                List args = new ArrayList(groupExpressions.size() - 1);
                for (int i = 1; i < groupExpressions.size(); i++) {
                    args.add(group.getGroup(groupExpressions.get(i)));
                }
                return (V)transformation.newInstance(args.toArray());
            }
        };
    }


}