1 4 package com.tc.objectserver.managedobject; 5 6 import com.tc.object.ObjectID; 7 8 import java.util.HashMap ; 9 import java.util.HashSet ; 10 import java.util.Iterator ; 11 import java.util.Map ; 12 import java.util.Set ; 13 14 public class BackReferences { 16 17 private final Map nodes; 18 19 private final Set parents; 20 21 public BackReferences() { 22 parents = new HashSet (); 23 nodes = new HashMap (); 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 getAllParents() { 44 return new HashSet (parents); 45 } 46 47 public Set addReferencedChildrenTo(Set objectIDs, Set interestedParents) { 48 for (Iterator 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 children; 60 61 public Node(ObjectID id) { 62 this.id = id; 63 this.children = new HashSet (); 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 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 addAllReferencedChildrenTo(Set objectIDs) { 87 for (Iterator 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 toString() { 98 return "Node(" + id + ") : children = " + children.size(); 100 } 101 } 102 103 } 104 | Popular Tags |