001: /*
002: * Copyright 2004-2007 Gary Bentley
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may
005: * not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package org.josql.internal;
016:
017: import java.util.List;
018: import java.util.ArrayList;
019: import java.util.Map;
020: import java.util.HashMap;
021:
022: import org.josql.Query;
023: import org.josql.QueryExecutionException;
024:
025: import org.josql.expressions.Expression;
026:
027: public class Grouper {
028:
029: private List cols = new ArrayList();
030: private Query q = null;
031: private int cs = -1;
032:
033: public Grouper(Query q) {
034:
035: this .q = q;
036:
037: }
038:
039: public List getExpressions() {
040:
041: return this .cols;
042:
043: }
044:
045: public void addExpression(Expression e) {
046:
047: this .cols.add(e);
048: this .cs = cols.size();
049:
050: }
051:
052: public Map group(List objs) throws QueryExecutionException {
053:
054: Map retVals = new HashMap();
055:
056: int s = objs.size();
057:
058: List l = null;
059:
060: for (int j = 0; j < s; j++) {
061:
062: Object o = objs.get(j);
063:
064: this .q.setCurrentObject(o);
065:
066: l = new ArrayList();
067:
068: // Get the values...
069: for (int i = 0; i < this .cs; i++) {
070:
071: Expression exp = (Expression) this .cols.get(i);
072:
073: try {
074:
075: l.add(exp.getValue(o, this .q));
076:
077: } catch (Exception e) {
078:
079: throw new QueryExecutionException(
080: "Unable to get group by value for expression: "
081: + exp, e);
082:
083: }
084:
085: }
086:
087: List v = (List) retVals.get(l);
088:
089: if (v == null) {
090:
091: v = new ArrayList();
092:
093: retVals.put(l, v);
094:
095: }
096:
097: v.add(o);
098:
099: }
100:
101: return retVals;
102:
103: }
104:
105: }
|