KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > managedobject > BackReferences


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

4 package com.tc.objectserver.managedobject;
5
6 import com.tc.object.ObjectID;
7
8 import java.util.HashMap JavaDoc;
9 import java.util.HashSet JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Set JavaDoc;
13
14 //TODO::This class doesnt maintain backreferences anymore. Should be renamed.
15
public class BackReferences {
16
17   private final Map JavaDoc nodes;
18
19   private final Set JavaDoc parents;
20
21   public BackReferences() {
22     parents = new HashSet JavaDoc();
23     nodes = new HashMap JavaDoc();
24   }
25
26   public void addBackReference(ObjectID child, ObjectID parent) {
27     if (child.isNull()) return;
28     Node c = getOrCreateNode(child);
29     Node p = getOrCreateNode(parent);
30     p.addChild(c);
31     parents.add(parent);
32   }
33
34   private Node getOrCreateNode(ObjectID id) {
35     Node n = (Node) nodes.get(id);
36     if (n == null) {
37       n = new Node(id);
38       nodes.put(id, n);
39     }
40     return n;
41   }
42
43   public Set JavaDoc getAllParents() {
44     return new HashSet JavaDoc(parents);
45   }
46
47   public Set JavaDoc addReferencedChildrenTo(Set JavaDoc objectIDs, Set JavaDoc interestedParents) {
48     for (Iterator JavaDoc i = interestedParents.iterator(); i.hasNext();) {
49       ObjectID pid = (ObjectID) i.next();
50       Node p = getOrCreateNode(pid);
51       p.addAllReferencedChildrenTo(objectIDs);
52     }
53     return objectIDs;
54   }
55
56   private static class Node {
57
58     private final ObjectID id;
59     private final Set JavaDoc children;
60
61     public Node(ObjectID id) {
62       this.id = id;
63       this.children = new HashSet JavaDoc();
64     }
65
66     public int hashCode() {
67       return id.hashCode();
68     }
69     
70     public ObjectID getID() {
71       return id;
72     }
73
74     public boolean equals(Object JavaDoc o) {
75       if (o instanceof Node) {
76         Node other = (Node) o;
77         return this.id.equals(other.id);
78       }
79       return false;
80     }
81
82     public void addChild(Node c) {
83       children.add(c);
84     }
85     
86     public Set JavaDoc addAllReferencedChildrenTo(Set JavaDoc objectIDs) {
87       for (Iterator JavaDoc i = children.iterator(); i.hasNext();) {
88         Node child = (Node) i.next();
89         if(!objectIDs.contains(child.getID())) {
90           objectIDs.add(child.getID());
91           child.addAllReferencedChildrenTo(objectIDs);
92         }
93       }
94       return objectIDs;
95     }
96
97     public String JavaDoc toString() {
98       // XXX:: dont just print the contents of children. That might cause a recursive loop
99
return "Node(" + id + ") : children = " + children.size();
100     }
101   }
102
103 }
104
Popular Tags