1 19 package org.openide.nodes; 20 21 import java.lang.ref.Reference ; 22 import java.util.*; 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 25 26 27 32 final class ChildrenArray extends NodeAdapter { 33 34 public Children children; 35 36 37 private Node[] nodes; 38 39 private Map<Children.Info,Collection<Node>> map; 40 41 private Reference <ChildrenArray> ref; 42 43 private static final Logger LOG_NODES_FOR = Logger.getLogger( 44 "org.openide.nodes.ChildrenArray.nodesFor"); 46 47 public ChildrenArray() { 48 } 49 50 public Children getChildren() { 51 return children; 52 } 53 54 56 protected void finalize() { 57 children.finalizedChildrenArray(ref); 58 } 59 60 61 final void pointedBy(Reference <ChildrenArray> ref) { 62 this.ref = ref; 63 } 64 65 67 public Node[] nodes() { 68 if (children == null) { 69 return null; 71 } 72 73 if (nodes == null) { 74 nodes = children.justComputeNodes(); 75 76 for (int i = 0; i < nodes.length; i++) { 77 nodes[i].reassignTo(children, this); 80 } 81 82 children.registerChildrenArray(this, nodes.length > 0); 84 } 85 86 return nodes; 87 } 88 89 91 public void clear() { 92 if (nodes != null) { 93 nodes = null; 94 95 children.registerChildrenArray(this, false); 99 } 100 } 101 102 106 public void finalizeNodes() { 107 Map m = map; 108 109 if (m != null) { 110 m.remove(null); 113 } 114 } 115 116 118 public boolean isInitialized() { 119 return nodes != null; 120 } 121 122 private String logInfo(Children.Info info) { 123 return info.toString() + '[' + Integer.toHexString(System.identityHashCode(info)) + ']'; 124 } 125 126 130 public synchronized Collection<Node> nodesFor(Children.Info info) { 131 final boolean IS_LOG = LOG_NODES_FOR.isLoggable(Level.FINE); 132 if (IS_LOG) { 133 LOG_NODES_FOR.fine("nodesFor(" +logInfo(info) + ") on " + Thread.currentThread()); } 135 if (map == null) { 136 map = new WeakHashMap<Children.Info,Collection<Node>>(7); 137 } 138 Collection<Node> nodes = map.get(info); 139 140 if (IS_LOG) { 141 LOG_NODES_FOR.fine(" map size=" + map.size() + ", nodes=" + nodes); } 143 144 if (nodes == null) { 145 nodes = info.entry.nodes(); 146 info.length = nodes.size(); 147 map.put(info, nodes); 148 if (IS_LOG) { 149 LOG_NODES_FOR.fine(" created nodes=" + nodes); } 151 } 152 153 if (IS_LOG) { 154 LOG_NODES_FOR.fine(" leaving nodesFor(" +logInfo(info) + ") on " + Thread.currentThread()); } 156 return nodes; 157 } 158 159 163 public synchronized void useNodes(Children.Info info, Collection<Node> list) { 164 if (map == null) { 165 map = new WeakHashMap<Children.Info,Collection<Node>>(7); 166 } 167 168 info.length = list.size(); 169 170 map.put(info, list); 171 } 172 173 public String toString() { 174 return super.toString() + " " + getChildren(); } 176 } 177 | Popular Tags |