001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package threaddemo.model;
043:
044: import java.io.IOException;
045: import java.io.InputStream;
046: import java.io.OutputStream;
047: import java.lang.ref.Reference;
048: import java.lang.ref.WeakReference;
049: import java.util.AbstractList;
050: import java.util.ArrayList;
051: import java.util.List;
052: import java.util.Map;
053: import java.util.WeakHashMap;
054: import threaddemo.locking.RWLock;
055:
056: /**
057: * Wraps a plain Phadhail and buffers its list of children.
058: * @author Jesse Glick
059: */
060: final class BufferedPhadhail implements Phadhail, PhadhailListener {
061:
062: private static final Map<Phadhail, Reference<BufferedPhadhail>> instances = new WeakHashMap<Phadhail, Reference<BufferedPhadhail>>();
063:
064: public static Phadhail forPhadhail(Phadhail ph) {
065: if (ph.hasChildren() && !(ph instanceof BufferedPhadhail)) {
066: Reference<BufferedPhadhail> r = instances.get(ph);
067: BufferedPhadhail bph = (r != null) ? r.get() : null;
068: if (bph == null) {
069: bph = new BufferedPhadhail(ph);
070: instances.put(ph, new WeakReference<BufferedPhadhail>(
071: bph));
072: }
073: return bph;
074: } else {
075: return ph;
076: }
077: }
078:
079: private final Phadhail ph;
080: private Reference<List<Phadhail>> kids;
081: private List<PhadhailListener> listeners = null;
082:
083: private BufferedPhadhail(Phadhail ph) {
084: this .ph = ph;
085: }
086:
087: public List<Phadhail> getChildren() {
088: List<Phadhail> phs = null;
089: if (kids != null) {
090: phs = kids.get();
091: }
092: if (phs == null) {
093: // Need to (re)calculate the children.
094: phs = new BufferedChildrenList(ph.getChildren());
095: kids = new WeakReference<List<Phadhail>>(phs);
096: }
097: return phs;
098: }
099:
100: private static final class BufferedChildrenList extends
101: AbstractList<Phadhail> {
102: private final List<Phadhail> orig;
103: private final Phadhail[] kids;
104:
105: public BufferedChildrenList(List<Phadhail> orig) {
106: this .orig = orig;
107: kids = new Phadhail[orig.size()];
108: }
109:
110: public Phadhail get(int i) {
111: if (kids[i] == null) {
112: kids[i] = forPhadhail(orig.get(i));
113: }
114: return kids[i];
115: }
116:
117: public int size() {
118: return kids.length;
119: }
120: }
121:
122: public String getName() {
123: return ph.getName();
124: }
125:
126: public String getPath() {
127: return ph.getPath();
128: }
129:
130: public boolean hasChildren() {
131: return ph.hasChildren();
132: }
133:
134: public void addPhadhailListener(PhadhailListener l) {
135: if (listeners == null) {
136: ph.addPhadhailListener(this );
137: listeners = new ArrayList<PhadhailListener>();
138: }
139: listeners.add(l);
140: }
141:
142: public void removePhadhailListener(PhadhailListener l) {
143: if (listeners != null) {
144: listeners.remove(l);
145: if (listeners.isEmpty()) {
146: listeners = null;
147: ph.removePhadhailListener(this );
148: }
149: }
150: }
151:
152: public void rename(String nue) throws IOException {
153: ph.rename(nue);
154: }
155:
156: public Phadhail createContainerPhadhail(String name)
157: throws IOException {
158: return ph.createContainerPhadhail(name);
159: }
160:
161: public Phadhail createLeafPhadhail(String name) throws IOException {
162: return ph.createLeafPhadhail(name);
163: }
164:
165: public void delete() throws IOException {
166: ph.delete();
167: }
168:
169: public InputStream getInputStream() throws IOException {
170: return ph.getInputStream();
171: }
172:
173: public OutputStream getOutputStream() throws IOException {
174: return ph.getOutputStream();
175: }
176:
177: public void childrenChanged(PhadhailEvent ev) {
178: // clear cache
179: kids = null;
180: if (listeners != null) {
181: PhadhailEvent ev2 = PhadhailEvent.create(this );
182: for (PhadhailListener l : listeners) {
183: l.childrenChanged(ev2);
184: }
185: }
186: }
187:
188: public void nameChanged(PhadhailNameEvent ev) {
189: if (listeners != null) {
190: PhadhailNameEvent ev2 = PhadhailNameEvent.create(this , ev
191: .getOldName(), ev.getNewName());
192: for (PhadhailListener l : listeners) {
193: l.nameChanged(ev2);
194: }
195: }
196: }
197:
198: public String toString() {
199: return "BufferedPhadhail<" + ph + ">@"
200: + Integer.toHexString(System.identityHashCode(this ));
201: }
202:
203: public RWLock lock() {
204: return ph.lock();
205: }
206:
207: }
|