1 15 package org.josql.internal; 16 17 import java.util.List ; 18 import java.util.ArrayList ; 19 import java.util.Comparator ; 20 import java.util.Map ; 21 import java.util.HashMap ; 22 23 import org.josql.expressions.Expression; 24 25 import org.josql.Query; 26 import org.josql.QueryResults; 27 28 public class GroupByExpressionComparator implements Comparator 29 { 30 31 private List items = new ArrayList (); 32 private Query q = null; 33 private int size = 0; 34 private int count = 0; 35 private Exception exp = null; 36 private Object nullObj = new Object (); 37 38 private Map cache = new HashMap (); 39 private boolean caching = false; 40 41 public GroupByExpressionComparator (Query q, 42 boolean caching) 43 { 44 45 this.q = q; 46 this.caching = caching; 47 48 } 49 50 public int getCount () 51 { 52 53 return this.count; 54 55 } 56 57 public boolean equals (Object o) 58 { 59 60 throw new UnsupportedOperationException ("Not supported for instances of: " + 61 this.getClass ().getName ()); 62 63 } 64 65 public boolean isCaching () 66 { 67 68 return this.caching; 69 70 } 71 72 public void setCaching (boolean b) 73 { 74 75 this.caching = b; 76 77 } 78 79 public void clearCache () 80 { 81 82 this.cache.clear (); 83 84 } 85 86 public int ci (Object o1, 87 Object o2) 88 throws Exception 89 { 90 91 List lo1 = (List ) o1; 92 List lo2 = (List ) o2; 93 94 QueryResults qr = this.q.getQueryResults (); 95 96 for (int i = 0; i < this.size; i++) 97 { 98 99 Item it = (Item) this.items.get (i); 100 101 this.q.setAllObjects ((List ) qr.getGroupByResults ().get (lo1)); 104 105 this.q.setCurrentGroupByObjects (this.q.getAllObjects ()); 106 107 this.q.setSaveValues ((Map ) qr.getGroupBySaveValues (lo1)); 109 110 this.q.setCurrentObject (o1); 111 112 Object eo1 = null; 113 114 if (it.exp != null) 115 { 116 117 eo1 = it.exp.getValue (o1, 118 this.q); 119 120 } else { 121 122 eo1 = lo1.get (it.ind); 123 124 } 125 126 this.q.setAllObjects ((List ) qr.getGroupByResults ().get (lo2)); 127 128 this.q.setSaveValues ((Map ) qr.getGroupBySaveValues (lo2)); 130 131 this.q.setCurrentObject (o2); 132 133 Object eo2 = null; 134 135 if (it.exp != null) 136 { 137 138 eo2 = it.exp.getValue (o2, 139 this.q); 140 141 } else { 142 143 eo2 = lo2.get (it.ind); 144 145 } 146 147 int c = Utilities.compare (eo1, 149 eo2); 150 151 if (c == 0) 152 { 153 154 continue; 156 157 } 158 159 if (it.dir == 1) 161 { 162 163 c = -1 * c; 164 165 } 166 167 return c; 168 169 } 170 171 return 0; 172 173 } 174 175 public int cic (Object o1, 176 Object o2) 177 throws Exception 178 { 179 180 this.count++; 181 182 Map co = null; 183 boolean get = true; 184 Item it = null; 185 Object eo1 = null; 186 Object eo2 = null; 187 188 QueryResults qr = this.q.getQueryResults (); 189 List lo1 = (List ) o1; 190 List lo2 = (List ) o2; 191 192 for (int i = 0; i < this.size; i++) 193 { 194 195 it = (Item) this.items.get (i); 196 197 eo1 = null; 198 199 get = true; 200 201 co = (Map ) cache.get (o1); 202 203 if (co == null) 204 { 205 206 co = new HashMap (this.size); 207 208 cache.put (o1, 209 co); 210 211 get = false; 212 213 } 214 215 if (get) 216 { 217 218 eo1 = co.get (it); 219 220 if (eo1 == this.nullObj) 221 { 222 223 eo1 = null; 224 225 } 226 227 } 228 229 if (eo1 == null) 230 { 231 232 this.q.setAllObjects ((List ) qr.getGroupByResults ().get (lo1)); 233 234 this.q.setSaveValues ((Map ) qr.getGroupBySaveValues (lo1)); 236 237 this.q.setCurrentObject (o1); 238 239 if (it.exp != null) 240 { 241 242 eo1 = it.exp.getValue (o1, 243 this.q); 244 245 } else { 246 247 eo1 = lo1.get (it.ind - 1); 248 249 } 250 251 co.put (it, 252 eo1); 253 254 } 255 256 get = true; 257 258 eo2 = null; 259 260 co = (Map ) cache.get (o2); 261 262 if (co == null) 263 { 264 265 co = new HashMap (this.size); 266 267 cache.put (o2, 268 co); 269 270 get = false; 271 272 } 273 274 if (get) 275 { 276 277 eo2 = co.get (it); 278 279 } 280 281 if (eo2 == null) 282 { 283 284 this.q.setAllObjects ((List ) qr.getGroupByResults ().get (lo2)); 285 286 this.q.setSaveValues ((Map ) qr.getGroupBySaveValues (lo2)); 288 289 this.q.setCurrentObject (o2); 290 291 if (it.exp != null) 292 { 293 294 eo2 = it.exp.getValue (o2, 295 this.q); 296 297 } else { 298 299 eo2 = lo2.get (it.ind - 1); 300 301 } 302 303 co.put (it, 304 eo2); 305 306 } 307 308 int c = Utilities.compare (eo1, 310 eo2); 311 312 if (c == 0) 313 { 314 315 continue; 317 318 } 319 320 if (it.dir == 1) 322 { 323 324 c = -1 * c; 325 326 } 327 328 return c; 329 330 } 331 332 return 0; 333 334 } 335 336 public int compare (Object o1, 337 Object o2) 338 { 339 340 try 341 { 342 343 if (this.caching) 344 { 345 346 return this.cic (o1, 347 o2); 348 349 } else { 350 351 return this.ci (o1, 352 o2); 353 354 } 355 356 } catch (Exception e) { 357 358 this.exp = e; 359 360 return 0; 361 362 } 363 364 } 365 366 public Exception getException () 367 { 368 369 return this.exp; 370 371 } 372 373 public void addSortItem (Expression exp, 374 int ind, 375 int dir) 376 { 377 378 Item it = new Item (); 379 it.dir = dir; 380 it.ind = ind; 381 it.exp = exp; 382 383 this.items.add (it); 384 385 this.size = this.items.size (); 386 387 } 388 389 private class Item 390 { 391 392 public int dir = 0; 393 public int ind = 0; 394 public Expression exp = null; 395 396 } 397 398 } 399 | Popular Tags |