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: package org.netbeans.modules.timers;
042:
043: import java.lang.ref.Reference;
044: import java.lang.ref.ReferenceQueue;
045: import java.lang.ref.WeakReference;
046: import java.util.ArrayList;
047: import java.util.Collection;
048: import java.util.LinkedList;
049: import java.util.List;
050: import java.util.concurrent.ExecutorService;
051: import java.util.concurrent.Executors;
052: import javax.swing.event.ChangeEvent;
053: import javax.swing.event.ChangeListener;
054:
055: /** A class for watching instances.
056: *
057: * @author Petr Hrebejk
058: */
059: public class InstanceWatcher {
060:
061: private List<Reference<Object>> references;
062: private ReferenceQueue<Object> queue;
063: private static ExecutorService executor = Executors
064: .newSingleThreadExecutor();
065:
066: private transient List<WeakReference<ChangeListener>> changeListenerList;
067:
068: /** Creates a new instance of InstanceWatcher */
069: public InstanceWatcher() {
070: references = new ArrayList<Reference<Object>>();
071: queue = new ReferenceQueue<Object>();
072: new FinalizingToken();
073: }
074:
075: public synchronized void add(Object instance) {
076: if (!contains(instance)) {
077: references.add(new WeakReference<Object>(instance, queue));
078: }
079: }
080:
081: private synchronized boolean contains(Object o) {
082: for (Reference r : references) {
083: if (r.get() == o) {
084: return true;
085: }
086: }
087: return false;
088: }
089:
090: public synchronized int size() {
091: removeNulls();
092: return references.size();
093: }
094:
095: public Collection<?> getInstances() {
096: List<Object> l = new ArrayList<Object>(references.size());
097: for (Reference wr : references) {
098: Object inst = wr.get();
099: if (inst != null)
100: l.add(inst);
101: }
102: return l;
103: }
104:
105: /*
106: public Iterator iterator() {
107:
108: }
109: */
110:
111: /**
112: * Registers ChangeListener to receive events. Notice that the listeners are
113: * held weakly. Make sure that you create hard reference to yopur listener.
114: * @param listener The listener to register.
115: */
116: public synchronized void addChangeListener(
117: javax.swing.event.ChangeListener listener) {
118: if (changeListenerList == null) {
119: changeListenerList = new ArrayList<WeakReference<ChangeListener>>();
120: }
121: changeListenerList.add(new WeakReference<ChangeListener>(
122: listener));
123: }
124:
125: /**
126: * Removes ChangeListener from the list of listeners.
127: * @param listener The listener to remove.
128: */
129: public synchronized void removeChangeListener(
130: ChangeListener listener) {
131:
132: if (listener == null) {
133: return;
134: }
135:
136: if (changeListenerList != null) {
137: for (WeakReference<ChangeListener> r : changeListenerList) {
138: if (listener.equals(r.get())) {
139: changeListenerList.remove(r);
140: }
141: }
142: }
143:
144: }
145:
146: // Private methods ---------------------------------------------------------
147:
148: private static <T> void cleanAndCopy(
149: List<? extends Reference<T>> src, List<? super T> dest) {
150: for (int i = src.size() - 1; i >= 0; i--) {
151: T o = src.get(i).get();
152: if (o == null) {
153: src.remove(i);
154: } else if (dest != null) {
155: dest.add(0, o);
156: }
157: }
158: }
159:
160: private synchronized void removeNulls() {
161: cleanAndCopy(references, null);
162: }
163:
164: private boolean cleanQueue() {
165: boolean retValue = false;
166:
167: while (queue.poll() != null) {
168: retValue = true;
169: }
170:
171: return retValue;
172: }
173:
174: private void refresh() {
175: if (cleanQueue()) {
176: removeNulls();
177: fireChangeListenerStateChanged();
178: }
179:
180: new FinalizingToken();
181: }
182:
183: private void fireChangeListenerStateChanged() {
184: List<ChangeListener> list = new LinkedList<ChangeListener>();
185: synchronized (this ) {
186: if (changeListenerList == null) {
187: return;
188: }
189: cleanAndCopy(changeListenerList, list);
190: }
191:
192: ChangeEvent e = new ChangeEvent(this );
193: for (ChangeListener ch : list) {
194: ch.stateChanged(e);
195: }
196: }
197:
198: // Private innerclasses ----------------------------------------------------
199:
200: private class FinalizingToken implements Runnable {
201:
202: public void finalize() {
203: executor.submit(this );
204: }
205:
206: public void run() {
207: refresh();
208: }
209:
210: }
211:
212: }
|