#73263: Using WeakResult the same way SimpleProxyLookup does. Fixes Nejedlak's memory test. release55_auc_root v2_2_5
authorjtulach@netbeans.org
Tue, 07 Mar 2006 06:12:43 +0000
changeset 129fccb092d4adb
parent 128 430775912617
child 130 99bf3b6ecd3f
#73263: Using WeakResult the same way SimpleProxyLookup does. Fixes Nejedlak's memory test.
openide.util/src/org/openide/util/lookup/ProxyLookup.java
     1.1 --- a/openide.util/src/org/openide/util/lookup/ProxyLookup.java	Sun Mar 05 21:08:57 2006 +0000
     1.2 +++ b/openide.util/src/org/openide/util/lookup/ProxyLookup.java	Tue Mar 07 06:12:43 2006 +0000
     1.3 @@ -12,6 +12,7 @@
     1.4   */
     1.5  package org.openide.util.lookup;
     1.6  
     1.7 +import java.lang.ref.WeakReference;
     1.8  import org.openide.util.Lookup;
     1.9  import org.openide.util.LookupEvent;
    1.10  import org.openide.util.LookupListener;
    1.11 @@ -245,23 +246,24 @@
    1.12       * that was found (not too useful) and also to all objects found
    1.13       * (more useful).
    1.14       */
    1.15 -    private final class R extends WaitableResult implements LookupListener {
    1.16 +    private final class R extends WaitableResult {
    1.17          /** list of listeners added */
    1.18          private javax.swing.event.EventListenerList listeners;
    1.19  
    1.20          /** template for this result */
    1.21          private Lookup.Template template;
    1.22  
    1.23 -        /** all results */
    1.24 -        private Lookup.Result[] results;
    1.25 -
    1.26          /** collection of Objects */
    1.27          private Collection[] cache;
    1.28  
    1.29 +        /** weak listener & result */
    1.30 +        private WeakResult weakL;
    1.31 +
    1.32          /** Constructor.
    1.33           */
    1.34          public R(Lookup.Template t) {
    1.35              template = t;
    1.36 +            weakL = new WeakResult(this);
    1.37          }
    1.38  
    1.39          /** When garbage collected, remove the template from the has map.
    1.40 @@ -274,8 +276,8 @@
    1.41           */
    1.42          private Result[] initResults() {
    1.43              synchronized (this) {
    1.44 -                if (results != null) {
    1.45 -                    return results;
    1.46 +                if (weakL.results != null) {
    1.47 +                    return weakL.results;
    1.48                  }
    1.49              }
    1.50  
    1.51 @@ -289,15 +291,15 @@
    1.52              synchronized (this) {
    1.53                  // some other thread might compute the result mean while. 
    1.54                  // if not finish the computation yourself
    1.55 -                if (results != null) {
    1.56 -                    return results;
    1.57 +                if (weakL.results != null) {
    1.58 +                    return weakL.results;
    1.59                  }
    1.60  
    1.61                  for (int i = 0; i < arr.length; i++) {
    1.62 -                    arr[i].addLookupListener(this);
    1.63 +                    arr[i].addLookupListener(weakL);
    1.64                  }
    1.65  
    1.66 -                results = arr;
    1.67 +                weakL.results = arr;
    1.68  
    1.69                  return arr;
    1.70              }
    1.71 @@ -310,7 +312,7 @@
    1.72           */
    1.73          protected void lookupChange(Set added, Set removed, Lookup[] old, Lookup[] current) {
    1.74              synchronized (this) {
    1.75 -                if (results == null) {
    1.76 +                if (weakL.results == null) {
    1.77                      // not computed yet, do not need to do anything
    1.78                      return;
    1.79                  }
    1.80 @@ -321,10 +323,10 @@
    1.81                  for (int i = 0; i < old.length; i++) {
    1.82                      if (removed.contains(old[i])) {
    1.83                          // removed lookup
    1.84 -                        results[i].removeLookupListener(this);
    1.85 +                        weakL.results[i].removeLookupListener(weakL);
    1.86                      } else {
    1.87                          // remember the association
    1.88 -                        map.put(old[i], results[i]);
    1.89 +                        map.put(old[i], weakL.results[i]);
    1.90                      }
    1.91                  }
    1.92  
    1.93 @@ -334,7 +336,7 @@
    1.94                      if (added.contains(current[i])) {
    1.95                          // new lookup
    1.96                          arr[i] = current[i].lookup(template);
    1.97 -                        arr[i].addLookupListener(this);
    1.98 +                        arr[i].addLookupListener(weakL);
    1.99                      } else {
   1.100                          // old lookup
   1.101                          arr[i] = (Lookup.Result) map.get(current[i]);
   1.102 @@ -347,7 +349,7 @@
   1.103                  }
   1.104  
   1.105                  // remember the new results
   1.106 -                results = arr;
   1.107 +                weakL.results = arr;
   1.108              }
   1.109          }
   1.110  
   1.111 @@ -451,7 +453,7 @@
   1.112                      cache = new Collection[3];
   1.113                  }
   1.114                  
   1.115 -                if (arr == results) {
   1.116 +                if (arr == weakL.results) {
   1.117                      // updates the results, if the results have not been
   1.118                      // changed during the computation of allInstances
   1.119                      cache[indexToCache] = ret;
   1.120 @@ -550,4 +552,75 @@
   1.121              }
   1.122          }
   1.123      }
   1.124 +    private static final class WeakResult extends WaitableResult implements LookupListener {
   1.125 +        /** all results */
   1.126 +        private Lookup.Result[] results;
   1.127 +
   1.128 +        private Reference result;
   1.129 +        
   1.130 +        public WeakResult(R r) {
   1.131 +            this.result = new WeakReference(r);
   1.132 +        }
   1.133 +        
   1.134 +        protected void beforeLookup(Lookup.Template t) {
   1.135 +            R r = (R)result.get();
   1.136 +            if (r != null) {
   1.137 +                r.beforeLookup(t);
   1.138 +            } else {
   1.139 +                removeListeners();
   1.140 +            }
   1.141 +        }
   1.142 +
   1.143 +        private void removeListeners() {
   1.144 +            Lookup.Result[] arr = this.results;
   1.145 +            if (arr == null) {
   1.146 +                return;
   1.147 +            }
   1.148 +
   1.149 +            for(int i = 0; i < arr.length; i++) {
   1.150 +                arr[i].removeLookupListener(this);
   1.151 +            }
   1.152 +        }
   1.153 +
   1.154 +        protected void collectFires(Collection evAndListeners) {
   1.155 +            R r = (R)result.get();
   1.156 +            if (r != null) {
   1.157 +                r.collectFires(evAndListeners);
   1.158 +            } else {
   1.159 +                removeListeners();
   1.160 +            }
   1.161 +        }
   1.162 +
   1.163 +        public void addLookupListener(LookupListener l) {
   1.164 +            assert false;
   1.165 +        }
   1.166 +
   1.167 +        public void removeLookupListener(LookupListener l) {
   1.168 +            assert false;
   1.169 +        }
   1.170 +
   1.171 +        public Collection allInstances() {
   1.172 +            assert false;
   1.173 +            return null;
   1.174 +        }
   1.175 +
   1.176 +        public void resultChanged(LookupEvent ev) {
   1.177 +            R r = (R)result.get();
   1.178 +            if (r != null) {
   1.179 +                r.resultChanged(ev);
   1.180 +            } else {
   1.181 +                removeListeners();
   1.182 +            }
   1.183 +        }
   1.184 +
   1.185 +        public Collection allItems() {
   1.186 +            assert false;
   1.187 +            return null;
   1.188 +        }
   1.189 +
   1.190 +        public Set allClasses() {
   1.191 +            assert false;
   1.192 +            return null;
   1.193 +        }
   1.194 +    } // end of WeakResult
   1.195  }