1 package org.bsf.smartValueObject; 2 3 import org.bsf.smartValueObject.container.SmartContainer; 4 import org.bsf.smartValueObject.container.SmartIterator; 5 import org.bsf.smartValueObject.tools.Instrumentor; 6 7 import java.util.*; 8 import java.lang.reflect.Field ; 9 10 19 public class SmartAccess { 20 27 public static Iterator createdIterator(Object o) { 28 checkSmartContainer(o); 29 SmartContainer sc = (SmartContainer) o; 30 return new SmartIterator(sc.getIterator(), VersionableFilters.CREATED); 31 } 32 33 40 public static Iterator modifiedIterator(Object o) { 41 checkSmartContainer(o); 42 SmartContainer sc = (SmartContainer) o; 43 return new SmartIterator(sc.getIterator(), VersionableFilters.DIRTY); 44 } 45 46 53 public static Iterator deletedIterator(Object o) { 54 checkSmartContainer(o); 55 SmartContainer sc = (SmartContainer) o; 56 return new SmartIterator(sc.getIterator(), VersionableFilters.DELETED); 57 } 58 59 64 public static Iterator iterator(Object o) { 65 checkSmartContainer(o); 66 SmartContainer sc = (SmartContainer) o; 67 return new SmartIterator(sc.getIterator(), VersionableFilters.ALL); 68 } 69 70 public static int createdSize(Object o) { 71 checkSmartContainer(o); 72 SmartContainer sc = (SmartContainer) o; 73 return sc.getCreated(); 74 } 75 76 public static int deletedSize(Object o) { 77 checkSmartContainer(o); 78 SmartContainer sc = (SmartContainer) o; 79 return sc.getDeleted(); 80 } 81 82 88 public static Iterator getSmartContainers(Object o) { 89 checkVersionable(o); 90 return new SmartIterator(getVersionables(o), VersionableFilters.SMARTCONTAINER); 91 } 92 93 99 public static Iterator getVersionables(Object o) { 100 checkVersionable(o); 101 Collection c = new ArrayList(); 102 Field [] fields = o.getClass().getFields(); 103 for (int i = 0; i < fields.length; i++) { 104 Field field = fields[i]; 105 if (field.getName().equals(Instrumentor.VERSIONFIELD) || 106 field.getType().isPrimitive()) 107 continue; 108 Object fieldObject; 109 try { 110 fieldObject = field.get(o); 111 } catch (Exception e) { 112 continue; 113 } 114 if (isVersionable(fieldObject)) { 115 c.add(fieldObject); 116 } 117 } 118 return c.iterator(); 119 } 120 121 126 public static boolean isSmartContainer(Object o) { 127 return (o instanceof SmartContainer); 128 } 129 130 133 public static boolean isVersionable(Object o) { 134 return (o instanceof Versionable); 135 } 136 137 143 public static boolean isDirty(Object o) { 144 checkVersionable(o); 145 return ((Versionable) o).isDirty(); 146 } 147 148 154 public static boolean isGraphDirty(Object o) { 155 DirtyVisitor dv = new DirtyVisitor(); 156 traverseGraph(o, dv); 157 158 return dv.isDirty(); 159 } 160 161 166 private static void traverseGraph(Object o, SmartVisitor visitor) { 167 if (SmartAccess.isSmartContainer(o)) { 168 traverseSmartContainer(o, visitor); 169 } else if (SmartAccess.isVersionable(o)) { 170 traverseVersionable(o, visitor); 171 } else { 172 throw new IllegalArgumentException (); 173 } 174 } 175 176 181 private static void traverseSmartContainer(Object o, SmartVisitor visitor) { 182 checkSmartContainer(o); 183 if (!visitor.visitSmartContainer(o)) return; 184 for (Iterator it = iterator(o); it.hasNext();) { 185 traverseGraph(it.next(), visitor); 186 } 187 } 188 189 194 private static void traverseVersionable(Object o, SmartVisitor visitor) { 195 checkVersionable(o); 196 if (!visitor.visitVersionable(o)) return; 197 for (Iterator it = getVersionables(o); it.hasNext(); ) { 198 traverseGraph(it.next(), visitor); 199 } 200 } 201 202 208 public static boolean isCreated(Object o) { 209 checkVersionable(o); 210 return ((Versionable) o).isCreated(); 211 } 212 213 219 public static boolean isDeleted(Object o) { 220 checkVersionable(o); 221 return ((Versionable) o).isDeleted(); 222 } 223 224 229 public static void reset(Object o) { 230 checkVersionable(o); 231 ((Versionable) o).markClean(); 232 } 233 234 238 public static void resetGraph(Object o) { 239 checkVersionable(o); 240 traverseGraph(o, new ResetVisitor()); 241 } 242 243 251 public static boolean sameVersion(Object o1, Object o2) { 252 checkVersionable(o1); 253 checkVersionable(o2); 254 return ((Versionable) o1).getVersionId() == 255 ((Versionable) o2).getVersionId(); 256 } 257 258 263 public static long getVersionId(Object o) { 264 checkVersionable(o); 265 return ((Versionable) o).getVersionId(); 266 } 267 268 273 public static void setVersionId(Object o, long id) { 274 checkVersionable(o); 275 ((Versionable) o).setVersionId(id); 276 } 277 278 283 private static interface SmartVisitor { 284 285 boolean visitVersionable(Object o); 286 287 boolean visitSmartContainer(Object o); 288 289 Object visitorResponse(); 290 } 291 292 295 private static class DirtyVisitor implements SmartVisitor { 296 private Map map = new HashMap(); 297 private boolean dirty = false; 298 299 public boolean visitVersionable(Object o) { 300 if (!continueVisiting(o)) 301 return false; 302 303 markVisited(o); 305 dirty = SmartAccess.isDirty(o); 306 return dirty == false; 307 } 308 309 public boolean visitSmartContainer(Object o) { 310 if (!continueVisiting(o)) 311 return false; 312 313 markVisited(o); 315 return dirty == false; 316 } 317 318 public Object visitorResponse() { 319 return new Boolean (dirty); 320 } 321 322 public boolean isDirty() { 323 return dirty; 324 } 325 326 private boolean continueVisiting(Object o) { 327 if (dirty) 328 return false; 329 else 330 return !map.containsKey(o); 331 } 332 333 private void markVisited(Object o) { 334 map.put(o, o); 335 } 336 } 337 338 341 private static class ResetVisitor implements SmartVisitor { 342 private Map map = new HashMap(); 343 344 public boolean visitVersionable(Object o) { 345 if (!continueVisiting(o)) 346 return false; 347 348 markVisited(o); 349 reset(o); 350 return true; 351 } 352 353 public boolean visitSmartContainer(Object o) { 354 if (!continueVisiting(o)) 355 return false; 356 357 markVisited(o); 358 reset(o); 359 return true; 360 } 361 362 public Object visitorResponse() { 363 return null; 364 } 365 366 private boolean continueVisiting(Object o) { 367 return !map.containsKey(o); 368 } 369 370 private void markVisited(Object o) { 371 map.put(o, o); 372 } 373 } 374 375 private static void log(String s) { 376 System.out.println(s); 377 } 378 379 383 private static void checkVersionable(Object o) { 384 if (!isVersionable(o)) 385 throw new IllegalArgumentException ("passed non-versionable object"); 386 } 387 388 392 private static void checkSmartContainer(Object o) { 393 if (!isSmartContainer(o)) { 394 throw new IllegalArgumentException ("passed non-smartcontainer object"); 395 } 396 } 397 } 398 | Popular Tags |