001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.db.sql;
030:
031: import com.caucho.log.Log;
032: import com.caucho.util.FreeList;
033:
034: import java.util.logging.Logger;
035:
036: /**
037: * Represents a row of a group item.
038: */
039: class GroupItem {
040: private static final Logger log = Log.open(GroupItem.class);
041:
042: private static FreeList<GroupItem> _freeList = new FreeList<GroupItem>(
043: 256);
044:
045: private Data[] _data;
046:
047: private boolean[] _isGroupByFields;
048:
049: /**
050: * Creates a group item of a given size.
051: */
052: private GroupItem(int size) {
053: _data = new Data[size];
054: for (int i = 0; i < size; i++)
055: _data[i] = new Data();
056: }
057:
058: /**
059: * Creates a group item of a given size.
060: */
061: static GroupItem allocate(boolean[] isGroupByFields) {
062: GroupItem item = _freeList.allocate();
063:
064: if (item == null)
065: item = new GroupItem(isGroupByFields.length);
066:
067: item.setSize(isGroupByFields.length);
068: item.setGroupByFields(isGroupByFields);
069:
070: return item;
071: }
072:
073: /**
074: * Creates a group item of a given size.
075: */
076: GroupItem allocateCopy() {
077: GroupItem item = _freeList.allocate();
078:
079: if (item == null)
080: item = new GroupItem(_data.length);
081:
082: item.setSize(_isGroupByFields.length);
083: item.setGroupByFields(_isGroupByFields);
084:
085: for (int i = 0; i < _isGroupByFields.length; i++) {
086: if (_isGroupByFields[i]) {
087: getData(i).copyTo(item.getData(i));
088: }
089: }
090:
091: return item;
092: }
093:
094: /**
095: * Sets the size.
096: */
097: public void init(int size, boolean[] isGroupByFields) {
098: setSize(size);
099: setGroupByFields(isGroupByFields);
100: clear();
101: }
102:
103: /**
104: * Sets the group item size.
105: */
106: private void setSize(int size) {
107: if (_data.length < size) {
108: _data = new Data[size];
109:
110: for (int i = 0; i < size; i++)
111: _data[i] = new Data();
112: }
113: }
114:
115: /**
116: * Sets the group-by fields
117: */
118: private void setGroupByFields(boolean[] isGroupByFields) {
119: _isGroupByFields = isGroupByFields;
120: }
121:
122: /**
123: * Clears the data.
124: */
125: public void clear() {
126: int length = _data.length;
127:
128: for (int i = 0; i < length; i++)
129: _data[i].clear();
130: }
131:
132: /**
133: * Return true for null
134: */
135: public boolean isNull(int index) {
136: return _data[index].isNull();
137: }
138:
139: /**
140: * Sets the data as a double.
141: */
142: public void setDouble(int index, double value) {
143: _data[index].setDouble(value);
144: }
145:
146: /**
147: * Gets the data as a double.
148: */
149: public double getDouble(int index) {
150: return _data[index].getDouble();
151: }
152:
153: /**
154: * Sets the data as a long.
155: */
156: public void setLong(int index, long value) {
157: _data[index].setLong(value);
158: }
159:
160: /**
161: * Gets the data as a long.
162: */
163: public long getLong(int index) {
164: return _data[index].getLong();
165: }
166:
167: /**
168: * Sets the data as a string.
169: */
170: public void setString(int index, String value) {
171: _data[index].setString(value);
172: }
173:
174: /**
175: * Gets the data as a string.
176: */
177: public String getString(int index) {
178: return _data[index].getString();
179: }
180:
181: /**
182: * Returns the data object a string.
183: */
184: public Data getData(int index) {
185: return _data[index];
186: }
187:
188: /**
189: * Returns the hashCode.
190: */
191: public int hashCode() {
192: int hash = 37;
193:
194: boolean[] isGroupByFields = _isGroupByFields;
195: int length = isGroupByFields.length;
196: if (length == 0)
197: return hash;
198:
199: Data[] data = _data;
200:
201: for (int i = 0; i < length; i++) {
202: if (isGroupByFields[i]) {
203: hash = hash * 65521 + data[i].hashCode();
204: }
205: }
206:
207: return hash;
208: }
209:
210: /**
211: * Returns equality based on group by
212: */
213: public boolean equals(Object o) {
214: if (this == o)
215: return true;
216:
217: else if (!o.getClass().equals(getClass()))
218: return false;
219:
220: GroupItem item = (GroupItem) o;
221:
222: boolean[] isGroupByFields = _isGroupByFields;
223: if (isGroupByFields != item._isGroupByFields)
224: return false;
225:
226: for (int i = 0; i < isGroupByFields.length; i++) {
227: if (isGroupByFields[i] && !_data[i].equals(item._data[i]))
228: return false;
229: }
230:
231: return true;
232: }
233: }
|