1 15 package org.josql.incubator; 16 17 import java.util.List ; 18 import java.util.ArrayList ; 19 import java.util.Comparator ; 20 import java.util.Collections ; 21 22 import com.gentlyweb.utils.Getter; 23 24 32 public class ObjectIndex implements Comparator 33 { 34 35 private List objs = new ArrayList (); 36 private List indices = new ArrayList (); 37 private Class c = null; 38 private int size = -1; 39 private boolean dirty = false; 40 private boolean syncOnAdd = false; 41 42 public ObjectIndex (Class c) 43 { 44 45 this.c = c; 46 47 } 48 49 public boolean isSyncOnAdd () 50 { 51 52 return this.syncOnAdd; 53 54 } 55 56 public void setSyncOnAdd (boolean v) 57 { 58 59 this.syncOnAdd = v; 60 61 } 62 63 public List getObjects (List keys) 64 { 65 66 if (this.dirty) 67 { 68 69 Collections.sort (this.objs, 70 this); 71 72 this.dirty = false; 73 74 } 75 76 List res = new ArrayList (); 77 78 int low = 0; 79 int os1 = this.objs.size () - 1; 80 81 int high = os1; 82 83 while (low <= high) 84 { 85 86 int mid = (low + high) / 2; 87 88 Object o = this.objs.get (mid); 89 90 int r = this.compare (o, 91 keys); 92 93 if (r != 0) 94 { 95 96 if (r < 0) 98 { 99 100 low = mid + 1; 101 102 } else { 103 104 high = mid - 1; 105 106 } 107 108 } else { 109 110 res.add (o); 112 113 int oi = mid; 114 115 while (mid < os1) 118 { 119 120 mid++; 121 122 o = this.objs.get (mid); 123 124 r = this.compare (o, 125 keys); 126 127 if (r == 0) 128 { 129 130 res.add (o); 131 132 } else { 133 134 break; 136 137 } 138 139 } 140 141 mid = oi; 142 143 while (mid > -1) 144 { 145 146 mid--; 147 148 o = this.objs.get (mid); 149 150 r = this.compare (o, 151 keys); 152 153 if (r == 0) 154 { 155 156 res.add (o); 157 158 } else { 159 160 break; 162 163 } 164 165 } 166 167 break; 169 170 } 171 172 } 173 174 return res; 175 176 } 177 178 public int size () 179 { 180 181 return this.size; 182 183 } 184 185 public void sort () 186 { 187 188 Collections.sort (this.objs, 189 this); 190 191 this.dirty = false; 192 193 } 194 195 public void add (String name) 196 { 197 198 if (this.syncOnAdd) 199 { 200 201 Collections.sort (this.objs, 202 this); 203 204 this.dirty = false; 205 206 } else { 207 208 this.dirty = true; 209 210 } 211 212 this.indices.add (new Getter (name, 213 this.c)); 214 215 } 216 217 public void removeObject (Object o) 218 { 219 220 this.objs.remove (o); 221 222 } 223 224 public void addObject (Object o) 225 { 226 227 if (this.objs.contains (o)) 228 { 229 230 return; 231 232 } 233 234 if (this.syncOnAdd) 235 { 236 237 Collections.sort (this.objs, 238 this); 239 240 this.dirty = false; 241 242 } else { 243 244 this.dirty = true; 245 246 } 247 248 this.objs.add (o); 249 250 this.size = this.objs.size (); 251 252 } 253 254 public int compare (Object o, 255 List keys) 256 { 257 258 try 259 { 260 261 int ks = keys.size (); 262 263 for (int i = 0; i < ks; i++) 264 { 265 266 Getter g = (Getter) this.indices.get (i); 267 268 Object eo1 = g.getValue (o); 269 270 Object kso = keys.get (i); 271 272 if ((eo1 == null) 274 || 275 (kso == null) 276 ) 277 { 278 279 return 0; 280 281 } 282 283 int v = 0; 284 285 if (eo1 instanceof Comparable ) 286 { 287 288 Comparable comp = (Comparable ) eo1; 290 291 v = comp.compareTo (kso); 292 293 } else { 294 295 v = eo1.toString ().compareTo (kso.toString ()); 296 297 } 298 299 if (v != 0) 300 { 301 302 return v; 303 304 } 305 306 continue; 308 309 } 310 311 } catch (Exception e) { 312 313 } 314 315 return 0; 317 318 } 319 320 public int compare (Object o1, 321 Object o2) 322 { 323 324 try 325 { 326 327 for (int i = 0; i < this.size; i++) 328 { 329 330 Getter g = (Getter) this.indices.get (i); 331 332 Object eo1 = g.getValue (o1); 333 334 Object eo2 = g.getValue (o2); 335 336 if ((eo1 == null) 338 || 339 (eo2 == null) 340 ) 341 { 342 343 return 0; 344 345 } 346 347 int v = 0; 348 349 if (eo1 instanceof Comparable ) 350 { 351 352 Comparable comp = (Comparable ) eo1; 354 355 v = comp.compareTo (eo2); 356 357 } else { 358 359 v = eo1.toString ().compareTo (eo2.toString ()); 360 361 } 362 363 if (v != 0) 364 { 365 366 return v; 367 368 } 369 370 continue; 372 373 } 374 375 } catch (Exception e) { 376 377 } 378 379 return 0; 381 382 } 383 384 } 385 | Popular Tags |