KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > smartValueObject > SmartAccess


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 JavaDoc;
9
10 /**
11  * Class to encapsulate implementation details and scary casts for
12  * <tt>SmartContainer</tt> and <tt>Versionable</tt> objects.
13  * It also allows to obtain iterators for various version states, as well
14  * as methods for graph traversals.
15  *
16  * @see org.bsf.smartValueObject.container.SmartContainer
17  * @see org.bsf.smartValueObject.Versionable
18  */

19 public class SmartAccess {
20     /**
21      * Gets an iterator for newly created objects.
22      * @param o an object of type <tt>SmartContainer</tt>.
23      * @return the iterator.
24      * @throws IllegalArgumentException when passing an object not
25      * implementing <tt>SmartContainer</tt>.
26      */

27     public static Iterator createdIterator(Object JavaDoc o) {
28         checkSmartContainer(o);
29         SmartContainer sc = (SmartContainer) o;
30         return new SmartIterator(sc.getIterator(), VersionableFilters.CREATED);
31     }
32
33     /**
34      * Gets an iterator for modified objects.
35      * @param o an object of type <tt>SmartContainer</tt>.
36      * @return the iterator.
37      * @throws IllegalArgumentException when passing an object not
38      * implementing <tt>SmartContainer</tt>.
39      */

40     public static Iterator modifiedIterator(Object JavaDoc o) {
41         checkSmartContainer(o);
42         SmartContainer sc = (SmartContainer) o;
43         return new SmartIterator(sc.getIterator(), VersionableFilters.DIRTY);
44     }
45
46     /**
47      * Gets an iterator for deleted objects.
48      * @param o an object of type <tt>SmartContainer</tt>.
49      * @return the iterator.
50      * @throws IllegalArgumentException when passing an object not
51      * implementing <tt>SmartContainer</tt>.
52      */

53     public static Iterator deletedIterator(Object JavaDoc o) {
54         checkSmartContainer(o);
55         SmartContainer sc = (SmartContainer) o;
56         return new SmartIterator(sc.getIterator(), VersionableFilters.DELETED);
57     }
58
59     /**
60      * Gets an iterator for all versionable objects.
61      * @param o
62      * @return
63      */

64     public static Iterator iterator(Object JavaDoc 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 JavaDoc o) {
71         checkSmartContainer(o);
72         SmartContainer sc = (SmartContainer) o;
73         return sc.getCreated();
74     }
75
76     public static int deletedSize(Object JavaDoc o) {
77         checkSmartContainer(o);
78         SmartContainer sc = (SmartContainer) o;
79         return sc.getDeleted();
80     }
81
82     /**
83      * Gets all smartcontainers in object o.
84      * @param o a versionable object.
85      * @return iterator with all smartcontainers.
86      * @see SmartContainer
87      */

88     public static Iterator getSmartContainers(Object JavaDoc o) {
89         checkVersionable(o);
90         return new SmartIterator(getVersionables(o), VersionableFilters.SMARTCONTAINER);
91     }
92
93     /**
94      * Gets all versionable objects contained in o (itself a versionable).
95      *
96      * @param o
97      * @return
98      */

99     public static Iterator getVersionables(Object JavaDoc o) {
100         checkVersionable(o);
101         Collection c = new ArrayList();
102         Field JavaDoc[] fields = o.getClass().getFields();
103         for (int i = 0; i < fields.length; i++) {
104             Field JavaDoc field = fields[i];
105             if (field.getName().equals(Instrumentor.VERSIONFIELD) ||
106                 field.getType().isPrimitive())
107                 continue;
108             Object JavaDoc fieldObject;
109             try {
110                 fieldObject = field.get(o);
111             } catch (Exception JavaDoc e) {
112                 continue;
113             }
114             if (isVersionable(fieldObject)) {
115                 c.add(fieldObject);
116             }
117         }
118         return c.iterator();
119     }
120
121     /**
122      * Checks if the object is a smart container.
123      * @param o
124      * @return
125      */

126     public static boolean isSmartContainer(Object JavaDoc o) {
127         return (o instanceof SmartContainer);
128     }
129
130     /**
131      * Checks if the object is versionable.
132      */

133     public static boolean isVersionable(Object JavaDoc o) {
134         return (o instanceof Versionable);
135     }
136
137     /**
138      * Checks if object is modified.
139      * @param o versionable object.
140      * @return
141      * @throws java.lang.IllegalArgumentException if object is not versionable.
142      */

143     public static boolean isDirty(Object JavaDoc o) {
144         checkVersionable(o);
145         return ((Versionable) o).isDirty();
146     }
147
148     /**
149      * Checks if objects in graph o have been modified.
150      *
151      * @param o versionable object with versionable child objects.
152      * @return
153      */

154     public static boolean isGraphDirty(Object JavaDoc o) {
155         DirtyVisitor dv = new DirtyVisitor();
156         traverseGraph(o, dv);
157
158         return dv.isDirty();
159     }
160
161     /**
162      * Traverses the graph using a visitor.
163      * @param o
164      * @param visitor
165      */

166      private static void traverseGraph(Object JavaDoc 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 JavaDoc();
173         }
174      }
175
176      /**
177       * Traverses the given smart container.
178       * @param o
179       * @param visitor
180       */

181      private static void traverseSmartContainer(Object JavaDoc 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      /**
190       * Traverses the given versionable object.
191       * @param o
192       * @param visitor
193       */

194      private static void traverseVersionable(Object JavaDoc 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     /**
203      * Checks if object was created.
204      * @param o versionable object.
205      * @return
206      * @throws java.lang.IllegalArgumentException if object is not versionable.
207      */

208     public static boolean isCreated(Object JavaDoc o) {
209         checkVersionable(o);
210         return ((Versionable) o).isCreated();
211     }
212
213     /**
214      * Checks if object was deleted.
215      * @param o versionable object.
216      * @return
217      * @throws java.lang.IllegalArgumentException if object is not versionable.
218      */

219     public static boolean isDeleted(Object JavaDoc o) {
220         checkVersionable(o);
221         return ((Versionable) o).isDeleted();
222     }
223
224     /**
225      * Resets objects version state.
226      * @param o versionable object.
227      * @throws java.lang.IllegalArgumentException if object is not versionable.
228      */

229     public static void reset(Object JavaDoc o) {
230         checkVersionable(o);
231         ((Versionable) o).markClean();
232     }
233
234     /**
235      * Resets all objects on the graph,including the root object.
236      * @param o
237      */

238     public static void resetGraph(Object JavaDoc o) {
239         checkVersionable(o);
240         traverseGraph(o, new ResetVisitor());
241     }
242
243     /**
244      * Checks if object o1, o2 have the same version
245      * number.
246      *
247      * @param o1 versionable object.
248      * @param o2 versionable object.
249      * @return
250      */

251     public static boolean sameVersion(Object JavaDoc o1, Object JavaDoc o2) {
252         checkVersionable(o1);
253         checkVersionable(o2);
254         return ((Versionable) o1).getVersionId() ==
255                ((Versionable) o2).getVersionId();
256     }
257
258     /**
259      * Gets version id from object.
260      * @param o
261      * @return
262      */

263     public static long getVersionId(Object JavaDoc o) {
264         checkVersionable(o);
265         return ((Versionable) o).getVersionId();
266     }
267
268     /**
269      * Sets version id.
270      * @param o
271      * @param id
272      */

273     public static void setVersionId(Object JavaDoc o, long id) {
274         checkVersionable(o);
275         ((Versionable) o).setVersionId(id);
276     }
277
278     /**
279      * Interface for visiting graphs.
280      *
281      * @see #traverseGraph
282      */

283     private static interface SmartVisitor {
284         /** Visits a versionable object */
285         boolean visitVersionable(Object JavaDoc o);
286          /** Visits a smart container */
287         boolean visitSmartContainer(Object JavaDoc o);
288         /** To obtain a return value after traversal. */
289         Object JavaDoc visitorResponse();
290     }
291
292     /**
293      * A visitor which searches the graph for dirty objects.
294      */

295     private static class DirtyVisitor implements SmartVisitor {
296         private Map map = new HashMap();
297         private boolean dirty = false;
298
299         public boolean visitVersionable(Object JavaDoc o) {
300             if (!continueVisiting(o))
301                 return false;
302
303             //log("visitVersionable" + o);
304
markVisited(o);
305             dirty = SmartAccess.isDirty(o);
306             return dirty == false;
307         }
308
309         public boolean visitSmartContainer(Object JavaDoc o) {
310             if (!continueVisiting(o))
311                 return false;
312
313             //log("visitSmartContainer " + o);
314
markVisited(o);
315             return dirty == false;
316         }
317
318         public Object JavaDoc visitorResponse() {
319             return new Boolean JavaDoc(dirty);
320         }
321
322         public boolean isDirty() {
323             return dirty;
324         }
325
326         private boolean continueVisiting(Object JavaDoc o) {
327             if (dirty)
328                 return false;
329             else
330                 return !map.containsKey(o);
331         }
332
333         private void markVisited(Object JavaDoc o) {
334             map.put(o, o);
335         }
336     }
337
338     /**
339      * A visitor which resets all versionable objects.
340      */

341     private static class ResetVisitor implements SmartVisitor {
342         private Map map = new HashMap();
343
344         public boolean visitVersionable(Object JavaDoc 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 JavaDoc o) {
354             if (!continueVisiting(o))
355                 return false;
356
357             markVisited(o);
358             reset(o);
359             return true;
360         }
361
362         public Object JavaDoc visitorResponse() {
363             return null;
364         }
365
366         private boolean continueVisiting(Object JavaDoc o) {
367             return !map.containsKey(o);
368         }
369
370         private void markVisited(Object JavaDoc o) {
371             map.put(o, o);
372         }
373     }
374
375     private static void log(String JavaDoc s) {
376         System.out.println(s);
377     }
378
379     /**
380      * Check if object is versionable, else throw IllegalArgumentException
381      * @param o
382      */

383     private static void checkVersionable(Object JavaDoc o) {
384         if (!isVersionable(o))
385             throw new IllegalArgumentException JavaDoc("passed non-versionable object");
386     }
387
388     /**
389      * Check if object is smartcontainer, else throw IllegalArgumentException
390      * @param o
391      */

392     private static void checkSmartContainer(Object JavaDoc o) {
393         if (!isSmartContainer(o)) {
394             throw new IllegalArgumentException JavaDoc("passed non-smartcontainer object");
395         }
396     }
397 }
398
Popular Tags