com.mysema.query.group.GroupImpl 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.*;
import com.mysema.query.types.Expression;
import com.mysema.query.types.Operation;
import com.mysema.query.types.Ops;
/**
* Default implementation of the Group interface
*
* @author sasa
* @author tiwe
*
*/
class GroupImpl implements Group {
private final Map, GroupCollector,?>> groupCollectorMap = new LinkedHashMap, GroupCollector,?>>();
private final List> groupExpressions;
private final List> groupCollectors = new ArrayList>();
private final List> maps;
public GroupImpl(List> columnDefinitions, List> maps) {
this.groupExpressions = columnDefinitions;
this.maps = maps;
for (int i=0; i < columnDefinitions.size(); i++) {
GroupExpression, ?> coldef = columnDefinitions.get(i);
GroupCollector,?> collector = groupCollectorMap.get(coldef.getExpression());
if (collector == null) {
collector = coldef.createGroupCollector();
Expression> coldefExpr = coldef.getExpression();
groupCollectorMap.put(coldefExpr, collector);
if (coldefExpr instanceof Operation && ((Operation)coldefExpr).getOperator() == Ops.ALIAS) {
groupCollectorMap.put(((Operation)coldefExpr).getArg(1), collector);
}
}
groupCollectors.add(collector);
}
}
@SuppressWarnings("unchecked")
void add(Object[] row) {
int i=0;
for (GroupCollector groupCollector : groupCollectors) {
groupCollector.add(row[i]);
i++;
}
}
@SuppressWarnings("unchecked")
private R get(Expression expr) {
GroupCollector col = (GroupCollector) groupCollectorMap.get(expr);
if (col != null) {
return col.get();
}
throw new NoSuchElementException(expr.toString());
}
@Override
@SuppressWarnings("unchecked")
public R getGroup(GroupExpression definition) {
for (GroupExpression, ?> def : groupExpressions) {
if (def.equals(definition)) {
return (R) groupCollectorMap.get(def.getExpression()).get();
}
}
throw new NoSuchElementException(definition.toString());
}
@Override
public List getList(Expression expr) {
return this.>get(expr);
}
@Override
@SuppressWarnings("unchecked")
public Map getMap(Expression key, Expression value) {
for (QPair, ?> pair : maps) {
if (pair.equals(key, value)) {
return (Map) groupCollectorMap.get(pair).get();
}
}
throw new NoSuchElementException("GMap(" + key + ", " + value + ")");
}
@Override
public SortedMap getSortedMap(Expression key, Expression value) {
for (QPair, ?> pair : maps) {
if (pair.equals(key, value)) {
return (SortedMap) groupCollectorMap.get(pair).get();
}
}
throw new NoSuchElementException("GMap(" + key + ", " + value + ")");
}
@Override
public T getOne(Expression expr) {
return this.get(expr);
}
@Override
public Set getSet(Expression expr) {
return this.>get(expr);
}
@Override
public SortedSet getSortedSet(Expression expr) {
return this.>get(expr);
}
@Override
public Object[] toArray() {
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy