KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > sql > GroupItem


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.db.sql;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.FreeList;
33
34 import java.util.logging.Logger JavaDoc;
35
36 /**
37  * Represents a row of a group item.
38  */

39 class GroupItem {
40   private static final Logger JavaDoc log = Log.open(GroupItem.class);
41
42   private static FreeList<GroupItem> _freeList = new FreeList<GroupItem>(256);
43   
44   private Data []_data;
45   
46   private boolean []_isGroupByFields;
47
48   /**
49    * Creates a group item of a given size.
50    */

51   private GroupItem(int size)
52   {
53     _data = new Data[size];
54     for (int i = 0; i < size; i++)
55       _data[i] = new Data();
56   }
57
58   /**
59    * Creates a group item of a given size.
60    */

61   static GroupItem allocate(boolean []isGroupByFields)
62   {
63     GroupItem item = _freeList.allocate();
64
65     if (item == null)
66       item = new GroupItem(isGroupByFields.length);
67
68     item.setSize(isGroupByFields.length);
69     item.setGroupByFields(isGroupByFields);
70
71     return item;
72   }
73
74   /**
75    * Creates a group item of a given size.
76    */

77   GroupItem allocateCopy()
78   {
79     GroupItem item = _freeList.allocate();
80
81     if (item == null)
82       item = new GroupItem(_data.length);
83
84     item.setSize(_isGroupByFields.length);
85     item.setGroupByFields(_isGroupByFields);
86
87     for (int i = 0; i < _isGroupByFields.length; i++) {
88       if (_isGroupByFields[i]) {
89     getData(i).copyTo(item.getData(i));
90       }
91     }
92
93     return item;
94   }
95
96   /**
97    * Sets the size.
98    */

99   public void init(int size, boolean []isGroupByFields)
100   {
101     setSize(size);
102     setGroupByFields(isGroupByFields);
103     clear();
104   }
105
106   /**
107    * Sets the group item size.
108    */

109   private void setSize(int size)
110   {
111     if (_data.length < size) {
112       _data = new Data[size];
113
114       for (int i = 0; i < size; i++)
115     _data[i] = new Data();
116     }
117   }
118
119   /**
120    * Sets the group-by fields
121    */

122   private void setGroupByFields(boolean []isGroupByFields)
123   {
124     _isGroupByFields = isGroupByFields;
125   }
126
127   /**
128    * Clears the data.
129    */

130   public void clear()
131   {
132     int length = _data.length;
133     
134     for (int i = 0; i < length; i++)
135       _data[i].clear();
136   }
137
138   /**
139    * Return true for null
140    */

141   public boolean isNull(int index)
142   {
143     return _data[index].isNull();
144   }
145
146   /**
147    * Sets the data as a double.
148    */

149   public void setDouble(int index, double value)
150   {
151     _data[index].setDouble(value);
152   }
153
154   /**
155    * Gets the data as a double.
156    */

157   public double getDouble(int index)
158   {
159     return _data[index].getDouble();
160   }
161
162   /**
163    * Sets the data as a long.
164    */

165   public void setLong(int index, long value)
166   {
167     _data[index].setLong(value);
168   }
169
170   /**
171    * Gets the data as a long.
172    */

173   public long getLong(int index)
174   {
175     return _data[index].getLong();
176   }
177
178   /**
179    * Sets the data as a string.
180    */

181   public void setString(int index, String JavaDoc value)
182   {
183     _data[index].setString(value);
184   }
185
186   /**
187    * Gets the data as a string.
188    */

189   public String JavaDoc getString(int index)
190   {
191     return _data[index].getString();
192   }
193
194   /**
195    * Returns the data object a string.
196    */

197   public Data getData(int index)
198   {
199     return _data[index];
200   }
201
202   /**
203    * Returns the hashCode.
204    */

205   public int hashCode()
206   {
207     int hash = 37;
208
209     boolean []isGroupByFields = _isGroupByFields;
210     int length = isGroupByFields.length;
211     if (length == 0)
212       return hash;
213
214     Data []data = _data;
215     
216     for (int i = 0; i < length; i++) {
217       if (isGroupByFields[i]) {
218     hash = hash * 65521 + data[i].hashCode();
219       }
220     }
221
222     return hash;
223   }
224
225   /**
226    * Returns equality based on group by
227    */

228   public boolean equals(Object JavaDoc o)
229   {
230     if (this == o)
231       return true;
232     
233     else if (! o.getClass().equals(getClass()))
234       return false;
235
236     GroupItem item = (GroupItem) o;
237
238     boolean []isGroupByFields = _isGroupByFields;
239     if (isGroupByFields != item._isGroupByFields)
240       return false;
241
242     for (int i = 0; i < isGroupByFields.length; i++) {
243       if (isGroupByFields[i] && ! _data[i].equals(item._data[i]))
244     return false;
245     }
246
247     return true;
248   }
249 }
250
Popular Tags