KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > Traverser


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.object;
6
7 import com.tc.exception.TCRuntimeException;
8 import com.tc.object.appevent.NonPortableEventContext;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.IdentityHashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15
16 /**
17  * Generic Object traverser. Initial use for this is to add any unmanaged objects to the managed object tree
18  *
19  * @author steve
20  */

21 public class Traverser {
22   private static final TraverseTest NULL_TEST = new NullTraverseTest();
23   private final TraversalAction action;
24   private final PortableObjectProvider portableObjectProvider;
25   private NonPortableEventContext context;
26
27   public Traverser(TraversalAction action, PortableObjectProvider portableObjectProvider) {
28     this.action = action;
29     this.portableObjectProvider = portableObjectProvider;
30   }
31
32   private void addReferencedObjects(Map JavaDoc toBeVisited, Object JavaDoc start, Map JavaDoc visited, TraverseTest traverseTest) {
33     Class JavaDoc clazz = start.getClass();
34
35     while (clazz != null) {
36       TraversedReferences portableObjects = new TraversedReferencesImpl();
37       portableObjects = portableObjectProvider.getPortableObjects(clazz, start, portableObjects);
38
39       for (Iterator JavaDoc i = portableObjects.iterator(); i.hasNext();) {
40         try {
41           TraversedReference currentReference = (TraversedReference) i.next();
42           Object JavaDoc currentObject = currentReference.getValue();
43
44           if (doNotTraverse(traverseTest, visited, currentObject)) {
45             continue;
46           }
47
48           traverseTest.checkPortability(currentReference, start.getClass(), context);
49
50           toBeVisited.put(currentObject, null);
51         } catch (IllegalArgumentException JavaDoc e) {
52           throw new TCRuntimeException(e);
53         }
54       }
55       clazz = clazz.getSuperclass();
56     }
57
58   }
59
60   private boolean doNotTraverse(TraverseTest traverseTest, Map JavaDoc visited, Object JavaDoc current) {
61     if (current == null) { return true; }
62
63     if (visited.containsKey(current)) { return true; }
64
65     if (!traverseTest.shouldTraverse(current)) { return true; }
66
67     return false;
68   }
69
70   public void traverse(Object JavaDoc object) {
71     traverse(object, NULL_TEST, null);
72   }
73
74   public void traverse(Object JavaDoc object, TraverseTest traverseTest, NonPortableEventContext ctx) {
75     this.context = ctx;
76     Map JavaDoc visited = new IdentityHashMap JavaDoc();
77     List JavaDoc toAdd = new ArrayList JavaDoc();
78
79     visited.put(object, null);
80
81     IdentityHashMap JavaDoc toBeVisited = new IdentityHashMap JavaDoc();
82     addReferencedObjects(toBeVisited, object, visited, traverseTest);
83     toAdd.add(object);
84
85     while (!toBeVisited.isEmpty()) {
86       for (Iterator JavaDoc i = new IdentityHashMap JavaDoc(toBeVisited).keySet().iterator(); i.hasNext();) {
87         Object JavaDoc obj = i.next();
88         visited.put(obj, null);
89         toBeVisited.remove(obj);
90         addReferencedObjects(toBeVisited, obj, visited, traverseTest);
91         toAdd.add(obj); // action.visit() to be taken place after addReferencedObjects() so that
92
// the manager of the referenced objects will only be set after the referenced
93
// objects are obtained.
94
}
95     }
96     action.visit(toAdd);
97   }
98 }
99
Popular Tags