1 package org.bsf.smartValueObject.container; 2 3 import org.bsf.smartValueObject.Versionable; 4 import org.bsf.smartValueObject.VersionableFilters; 5 6 import java.util.Iterator ; 7 8 14 public abstract class AbstractSmartContainer implements SmartContainer, Versionable { 15 16 private Versionable version; 17 18 private int created = 0; 19 20 private int deleted = 0; 21 22 26 public AbstractSmartContainer(Versionable v) { 27 this.version = v; 28 } 29 30 35 protected abstract boolean addToContainer(Object o); 36 37 44 protected abstract Object addToContainer(Object key, Object o); 45 46 52 protected abstract Object getFromContainer(Object key); 53 54 59 protected abstract boolean removeFromContainer(Object o); 60 61 67 protected abstract Object removeKeyFromContainer(Object key); 68 69 74 protected abstract boolean containerContains(Object o); 75 76 81 protected abstract boolean containerContainsKey(Object key); 82 83 87 protected abstract int containerSize(); 88 89 93 protected abstract Iterator containerIterator(); 94 95 98 protected abstract void containerClear(); 99 100 105 protected abstract Object [] toObjectArray(); 106 107 112 protected boolean removeObject(Object o) { 113 if (o instanceof Versionable) { 114 Versionable v = (Versionable) o; 115 if (v.isCreated()) { 116 created--; 117 return removeFromContainer(o); 118 } 119 v.delete(); 120 deleted++; 121 touch(); 122 return true; 123 } else { 124 return removeFromContainer(o); 125 } 126 } 127 128 133 protected Object removeObjectByKey(Object key) { 134 Object o = getFromContainer(key); 135 if (o == null) { 136 } else if (o instanceof Versionable) { 138 Versionable v = (Versionable) o; 139 if (v.isCreated()) { 140 created--; 141 return removeKeyFromContainer(key); 142 } 143 v.delete(); 144 deleted++; 145 touch(); 146 } else { 147 return removeKeyFromContainer(key); 148 } 149 return o; 150 } 151 152 157 protected boolean addObject(Object o) { 158 if (o instanceof Versionable) { 159 Versionable v = (Versionable) o; 160 161 if (v.isCreated()) 163 created++; 164 165 touch(); 166 } 167 return addToContainer(o); 168 } 169 170 176 protected Object addObject(Object key, Object o) { 177 if (o instanceof Versionable) { 178 Versionable v = (Versionable) o; 179 if (v.isCreated()) 181 created++; 182 183 touch(); 184 } 185 return addToContainer(key, o); 186 } 187 188 194 protected boolean containsObject(Object o) { 195 if (containerContains(o)) { 196 if (o instanceof Versionable) { 197 return !((Versionable) o).isDeleted(); 198 } else { 199 return true; 200 } 201 } else { 202 return false; 203 } 204 } 205 206 212 public boolean add(Object o) { 213 return addObject(o); 214 } 215 216 222 public boolean contains(Object o) { 223 return containsObject(o); 224 } 225 226 232 public boolean containsValue(Object o) { 233 return containsObject(o); 234 } 235 236 242 public boolean containsKey(Object key) { 243 if (!containerContainsKey(key)) { 244 return false; 245 } 246 247 Object o = getFromContainer(key); 248 if (o instanceof Versionable) { 249 return !((Versionable) o).isDeleted(); 250 } else { 251 return true; 252 } 253 } 254 255 260 public Iterator iterator() { 261 return new SmartIterator(containerIterator(), 262 VersionableFilters.EXISTING); 263 } 264 265 271 public void clear() { 272 Iterator it = containerIterator(); 273 while (it.hasNext()) { 274 removeObject(it.next()); 275 } 276 } 277 278 284 public boolean isEmpty() { 285 return (containerSize()-getDeleted() == 0); 286 } 287 288 295 public Object put (Object key, Object value) { 296 return addObject(key, value); 297 } 298 299 305 public Object get(Object key) { 306 Object o = getFromContainer(key); 307 if (o == null) 308 return null; 309 310 if (o instanceof Versionable) { 311 return ((Versionable) o).isDeleted() ? null : o; 312 } else { 313 return o; 314 } 315 } 316 317 322 public Object [] toArray() { 323 Object [] a = toObjectArray(); 324 Object [] b = new Object [size()]; 325 326 for (int i = 0,j = 0; i < a.length; i++) { 327 Object o = a[i]; 328 if (o instanceof Versionable) { 329 if (((Versionable) o).isDeleted()) 330 continue; 331 } 332 b[j++] = a[i]; 333 } 334 return b; 335 } 336 337 342 public Object [] toArray(Object a[]) { 343 int size = size(); 344 if (a.length < size) { 345 a = (Object []) java.lang.reflect.Array.newInstance( 346 a.getClass().getComponentType(), size); 347 } 348 349 System.arraycopy(toArray(), 0, a, 0, size); 350 if (a.length > size) { 351 a[size] = null; 352 } 353 return a; 354 } 355 356 357 public int getCreated() { 359 return created; 360 } 361 362 public int getDeleted() { 363 return deleted; 364 } 365 366 public Iterator getIterator() { 367 return containerIterator(); 368 } 369 370 public int size() { 371 return containerSize()-getDeleted(); 372 } 373 374 public abstract Object getContainer(); 375 376 public void touch() { 378 version.touch(); 379 } 380 381 public void touch(String s) { 382 version.touch(s); 383 } 384 385 public boolean isDirty() { 386 return version.isDirty(); 387 } 388 389 public boolean isCreated() { 390 return version.isCreated(); 391 } 392 393 public boolean isDeleted() { 394 return version.isDeleted(); 395 } 396 397 public void markClean() { 398 created = deleted = 0; 399 version.markClean(); 400 } 401 402 public long getVersionId() { 403 return version.getVersionId(); 404 } 405 406 public void setVersionId(long id) { 407 version.setVersionId(id); 408 } 409 410 public void delete() {} 411 public void create() {} 412 } 413 | Popular Tags |