com.mysema.query.group.AbstractGroupByTransformer Maven / Gradle / Ivy
/*
* 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.List;
import com.mysema.query.ResultTransformer;
import com.mysema.query.Tuple;
import com.mysema.query.types.Expression;
import com.mysema.query.types.ExpressionBase;
import com.mysema.query.types.FactoryExpression;
import com.mysema.query.types.Operation;
import com.mysema.query.types.Ops;
import com.mysema.query.types.Visitor;
/**
* Base class for GroupBy result transformers
*
* @author tiwe
*
* @param
* @param
*/
abstract class AbstractGroupByTransformer implements ResultTransformer {
private static final class FactoryExpressionAdapter extends ExpressionBase implements FactoryExpression {
private final FactoryExpression expr;
private final List> args;
private FactoryExpressionAdapter(FactoryExpression expr, List> args) {
super(expr.getType());
this.expr = expr;
this.args = args;
}
@Override
public R accept(Visitor v, C context) {
return expr.accept(v, context);
}
@Override
public List> getArgs() {
return args;
}
@Override
public T newInstance(Object... args) {
return expr.newInstance(args);
}
}
protected final List> groupExpressions = new ArrayList>();
protected final List> maps = new ArrayList>();
protected final Expression>[] expressions;
AbstractGroupByTransformer(Expression key, Expression>... expressions) {
List> projection = new ArrayList>(expressions.length);
groupExpressions.add(new GOne(key));
projection.add(key);
for (Expression> expr : expressions) {
if (expr instanceof GroupExpression,?>) {
GroupExpression,?> groupExpr = (GroupExpression,?>)expr;
groupExpressions.add(groupExpr);
Expression> colExpression = groupExpr.getExpression();
if (colExpression instanceof Operation && ((Operation)colExpression).getOperator() == Ops.ALIAS) {
projection.add(((Operation)colExpression).getArg(0));
} else {
projection.add(colExpression);
}
if (groupExpr instanceof GMap) {
maps.add((QPair, ?>) colExpression);
}
} else {
groupExpressions.add(new GOne(expr));
projection.add(expr);
}
}
this.expressions = projection.toArray(new Expression[projection.size()]);
}
protected static FactoryExpression withoutGroupExpressions(final FactoryExpression expr) {
List> args = new ArrayList>(expr.getArgs().size());
for (Expression> arg : expr.getArgs()) {
if (arg instanceof GroupExpression) {
args.add(((GroupExpression)arg).getExpression());
} else {
args.add(arg);
}
}
return new FactoryExpressionAdapter(expr, args);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy