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 27 public class ListExpressionComparator implements Comparator 28 { 29 30 private List items = new ArrayList (); 31 private Query q = null; 32 private int size = 0; 33 private int count = 0; 34 private Exception exp = null; 35 private Object nullObj = new Object (); 36 37 private Map cache = new HashMap (); 38 private boolean caching = false; 39 40 public ListExpressionComparator (Query q, 41 boolean caching) 42 { 43 44 this.q = q; 45 this.caching = caching; 46 47 } 48 49 public int getCount () 50 { 51 52 return this.count; 53 54 } 55 56 public boolean equals (Object o) 57 { 58 59 throw new UnsupportedOperationException ("Not supported for instances of: " + 60 this.getClass ().getName ()); 61 62 } 63 64 public boolean isCaching () 65 { 66 67 return this.caching; 68 69 } 70 71 public void setCaching (boolean b) 72 { 73 74 this.caching = b; 75 76 } 77 78 public void clearCache () 79 { 80 81 this.cache.clear (); 82 83 } 84 85 public int ci (Object o1, 86 Object o2) 87 throws Exception 88 { 89 90 for (int i = 0; i < this.size; i++) 91 { 92 93 Item it = (Item) this.items.get (i); 94 95 this.q.setCurrentObject (o1); 96 97 Object eo1 = it.exp.getValue (o1, 98 this.q); 99 100 this.q.setCurrentObject (o2); 101 102 Object eo2 = it.exp.getValue (o2, 103 this.q); 104 105 int c = Utilities.compare (eo1, 107 eo2); 108 109 if (c == 0) 110 { 111 112 continue; 114 115 } 116 117 if (it.dir == 1) 119 { 120 121 c = -1 * c; 122 123 } 124 125 return c; 126 127 } 128 129 return 0; 130 131 } 132 133 public int cic (Object o1, 134 Object o2) 135 throws Exception 136 { 137 138 this.count++; 139 140 Map co = null; 141 boolean get = true; 142 Item it = null; 143 Object eo1 = null; 144 Object eo2 = null; 145 146 for (int i = 0; i < this.size; i++) 147 { 148 149 it = (Item) this.items.get (i); 150 151 eo1 = null; 152 153 get = true; 154 155 co = (Map ) cache.get (o1); 156 157 if (co == null) 158 { 159 160 co = new HashMap (this.size); 161 162 cache.put (o1, 163 co); 164 165 get = false; 166 167 } 168 169 if (get) 170 { 171 172 eo1 = co.get (it); 173 174 if (eo1 == this.nullObj) 175 { 176 177 eo1 = null; 178 179 } 180 181 } 182 183 if (eo1 == null) 184 { 185 186 this.q.setCurrentObject (o1); 187 188 eo1 = it.exp.getValue (o1, 189 this.q); 190 191 co.put (it, 192 eo1); 193 194 } 195 196 get = true; 197 198 eo2 = null; 199 200 co = (Map ) cache.get (o2); 201 202 if (co == null) 203 { 204 205 co = new HashMap (this.size); 206 207 cache.put (o2, 208 co); 209 210 get = false; 211 212 } 213 214 if (get) 215 { 216 217 eo2 = co.get (it); 218 219 } 220 221 if (eo2 == null) 222 { 223 224 this.q.setCurrentObject (o2); 225 226 eo2 = it.exp.getValue (o2, 227 this.q); 228 229 co.put (it, 230 eo2); 231 232 } 233 234 int c = Utilities.compare (eo1, 236 eo2); 237 238 if (c == 0) 239 { 240 241 continue; 243 244 } 245 246 if (it.dir == 1) 248 { 249 250 c = -1 * c; 251 252 } 253 254 return c; 255 256 } 257 258 return 0; 259 260 } 261 262 public int compare (Object o1, 263 Object o2) 264 { 265 266 try 267 { 268 269 if (this.caching) 270 { 271 272 return this.cic (o1, 273 o2); 274 275 } else { 276 277 return this.ci (o1, 278 o2); 279 280 } 281 282 } catch (Exception e) { 283 284 this.exp = e; 285 286 return 0; 287 288 } 289 290 } 291 292 public Exception getException () 293 { 294 295 return this.exp; 296 297 } 298 299 public List getSortItems () 300 { 301 302 return this.items; 303 304 } 305 306 public void addSortItem (Expression exp, 307 int dir) 308 { 309 310 Item it = new Item (); 311 it.dir = dir; 312 it.exp = exp; 313 314 this.items.add (it); 315 316 this.size = this.items.size (); 317 318 } 319 320 private class Item 321 { 322 323 public int dir = 0; 324 public Expression exp = null; 325 326 } 327 328 } 329 | Popular Tags |