KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > internal > Grouper


1 /*
2  * Copyright 2004-2005 Gary Bentley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15 package org.josql.internal;
16
17 import java.util.List JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.HashMap JavaDoc;
21
22 import org.josql.Query;
23 import org.josql.QueryExecutionException;
24
25 import org.josql.expressions.Expression;
26
27 public class Grouper
28 {
29
30     private List JavaDoc cols = new ArrayList JavaDoc ();
31     private Query q = null;
32     private int cs = -1;
33
34     public Grouper (Query q)
35     {
36
37     this.q = q;
38
39     }
40
41     public List JavaDoc getExpressions ()
42     {
43
44     return this.cols;
45
46     }
47
48     public void addExpression (Expression e)
49     {
50
51     this.cols.add (e);
52     this.cs = cols.size ();
53
54     }
55
56     public Map JavaDoc group (List JavaDoc objs)
57                   throws QueryExecutionException
58     {
59
60     Map JavaDoc retVals = new HashMap JavaDoc ();
61
62     int s = objs.size ();
63
64     List JavaDoc l = null;
65
66     for (int j = 0; j < s; j++)
67     {
68
69         Object JavaDoc o = objs.get (j);
70
71         this.q.setCurrentObject (o);
72
73         l = new ArrayList JavaDoc ();
74
75         // Get the values...
76
for (int i = 0; i < this.cs; i++)
77         {
78
79         Expression exp = (Expression) this.cols.get (i);
80
81         try
82         {
83
84             l.add (exp.getValue (o,
85                      this.q));
86
87         } catch (Exception JavaDoc e) {
88
89             throw new QueryExecutionException ("Unable to get group by value for expression: " +
90                                exp,
91                                e);
92
93         }
94
95         }
96
97         List JavaDoc v = (List JavaDoc) retVals.get (l);
98
99         if (v == null)
100         {
101
102         v = new ArrayList JavaDoc ();
103
104         retVals.put (l,
105                  v);
106
107         }
108
109         v.add (o);
110
111     }
112
113     return retVals;
114
115     }
116
117 }
118
Popular Tags