1 19 20 package org.netbeans.modules.languages; 21 22 import java.lang.ref.WeakReference ; 23 import java.util.Collection ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import org.openide.util.RequestProcessor; 28 29 30 34 public class Utils { 35 36 private static Map <String ,WeakReference > collections; 37 38 39 public static void startTest (String name, Collection c) { 40 if (collections == null) { 41 collections = new HashMap <String ,WeakReference > (); 43 start (); 44 } 45 collections.put (name, new WeakReference <Collection > (c)); 46 } 47 48 public static void startTest (String name, Map m) { 49 if (collections == null) { 50 collections = new HashMap <String ,WeakReference > (); 52 start (); 53 } 54 collections.put (name, new WeakReference <Map > (m)); 55 } 56 57 private static void start () { 58 RequestProcessor.getDefault ().post (new Runnable () { 59 public void run () { 60 Map <String ,WeakReference > cs = new HashMap <String ,WeakReference > (collections); 61 Iterator <String > it = cs.keySet ().iterator (); 62 while (it.hasNext ()) { 63 String name = it.next (); 64 Object o = cs.get (name).get (); 65 if (o == null) 66 collections.remove (name); 67 else 68 System.out.println (":" + name + " " + size (o)); 69 } 70 start (); 71 } 72 }, 5000); 73 } 74 75 private static int size (Object o) { 76 if (o instanceof Collection ) { 77 Collection c = (Collection ) o; 78 int s = c.size (); 79 Iterator it = c.iterator (); 80 while (it.hasNext ()) { 81 Object item = it.next (); 82 if (item instanceof Collection || 83 item instanceof Map 84 ) 85 s += size (item); 86 } 87 return s; 88 } 89 Map m = (Map ) o; 90 int s = m.size (); 91 Iterator it = m.keySet ().iterator (); 92 while (it.hasNext ()) { 93 Object key = it.next (); 94 if (key instanceof Collection || 95 key instanceof Map 96 ) 97 s += size (key); 98 Object value = m.get (key); 99 if (value instanceof Collection || 100 value instanceof Map 101 ) 102 s += size (value); 103 } 104 return s; 105 } 106 } 107 | Popular Tags |