1 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 ; 35 36 39 class GroupItem { 40 private static final Logger 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 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 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 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 99 public void init(int size, boolean []isGroupByFields) 100 { 101 setSize(size); 102 setGroupByFields(isGroupByFields); 103 clear(); 104 } 105 106 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 122 private void setGroupByFields(boolean []isGroupByFields) 123 { 124 _isGroupByFields = isGroupByFields; 125 } 126 127 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 141 public boolean isNull(int index) 142 { 143 return _data[index].isNull(); 144 } 145 146 149 public void setDouble(int index, double value) 150 { 151 _data[index].setDouble(value); 152 } 153 154 157 public double getDouble(int index) 158 { 159 return _data[index].getDouble(); 160 } 161 162 165 public void setLong(int index, long value) 166 { 167 _data[index].setLong(value); 168 } 169 170 173 public long getLong(int index) 174 { 175 return _data[index].getLong(); 176 } 177 178 181 public void setString(int index, String value) 182 { 183 _data[index].setString(value); 184 } 185 186 189 public String getString(int index) 190 { 191 return _data[index].getString(); 192 } 193 194 197 public Data getData(int index) 198 { 199 return _data[index]; 200 } 201 202 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 228 public boolean equals(Object 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 |