]> source.dussan.org Git - aspectj.git/commitdiff
incorporate tim peierls suggestion from mailing list
authoracolyer <acolyer>
Fri, 17 Dec 2004 13:25:52 +0000 (13:25 +0000)
committeracolyer <acolyer>
Fri, 17 Dec 2004 13:25:52 +0000 (13:25 +0000)
docs/adk15ProgGuideDB/pertypewithin.xml

index e3f5ec0ae3f44dadeaf5550825f2e075097742d3..ec099c1fdf41c020b5e74712d4099ee338324426 100644 (file)
 
        <programlisting><![CDATA[
         public aspect InstanceTracking pertypewithin(org.xyz..*) {
-        
-          private Set<WeakReference<Object>> instances = new HashSet<WeakReference<Object>>();
+
+          // use WeakHashMap for auto-garbage collection of keys        
+          private Map<?,Boolean> instances = new WeakHashMap<?,Boolean>();
           
           after(Object o) returning : execution(new(..)) {
-            instances.add(new WeakReference<Object>(o);
+            instances.put(o,true);
           }
         
-          public Set<Object> getInstances() {
-             Set<Object> result = new HashSet<Object>();
-             for(WeakReference<Object> ref : instances) {
-                 if (ref.get() != null) {
-                  result.add(ref.get());
-                 }
-             }
-             return result;
+          public Set<?> getInstances() {
+             return instances.keySet();
           }
         }
        ]]></programlisting>
        <programlisting><![CDATA[
         public aspect InstanceTracking<T> pertypewithin(org.xyz..*) {
         
-          private Set<WeakReference<T>> instances = new HashSet<WeakReference<T>>();
+          // use WeakHashMap for auto-garbage collection of keys        
+          private Map<T, Boolean> instances = new WeakHashMap<T, Boolean>();
           
           after(T t) returning : execution(new(..)) {
-            instances.add(new WeakReference<T>(t);
+            instances.put(t, true);
           }
         
           public Set<T> getInstances() {
-             Set<T> result = new HashSet<T>();
-             for(WeakReference<T> ref : instances) {
-                 if (ref.get() != null) {
-                  result.add(ref.get());
-                 }
-             }
-             return result;
+                 return instances.keySet();
           }
+          
         }
        ]]></programlisting>