KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > Utils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.languages;
21
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import org.openide.util.RequestProcessor;
28
29
30 /**
31  *
32  * @author Jan Jancura
33  */

34 public class Utils {
35
36     private static Map JavaDoc<String JavaDoc,WeakReference JavaDoc> collections;
37     
38     
39     public static void startTest (String JavaDoc name, Collection JavaDoc c) {
40         if (collections == null) {
41             // init
42
collections = new HashMap JavaDoc<String JavaDoc,WeakReference JavaDoc> ();
43             start ();
44         }
45         collections.put (name, new WeakReference JavaDoc<Collection JavaDoc> (c));
46     }
47     
48     public static void startTest (String JavaDoc name, Map JavaDoc m) {
49         if (collections == null) {
50             // init
51
collections = new HashMap JavaDoc<String JavaDoc,WeakReference JavaDoc> ();
52             start ();
53         }
54         collections.put (name, new WeakReference JavaDoc<Map JavaDoc> (m));
55     }
56     
57     private static void start () {
58         RequestProcessor.getDefault ().post (new Runnable JavaDoc () {
59             public void run () {
60                 Map JavaDoc<String JavaDoc,WeakReference JavaDoc> cs = new HashMap JavaDoc<String JavaDoc,WeakReference JavaDoc> (collections);
61                 Iterator JavaDoc<String JavaDoc> it = cs.keySet ().iterator ();
62                 while (it.hasNext ()) {
63                     String JavaDoc name = it.next ();
64                     Object JavaDoc 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 JavaDoc o) {
76         if (o instanceof Collection JavaDoc) {
77             Collection JavaDoc c = (Collection JavaDoc) o;
78             int s = c.size ();
79             Iterator JavaDoc it = c.iterator ();
80             while (it.hasNext ()) {
81                 Object JavaDoc item = it.next ();
82                 if (item instanceof Collection JavaDoc ||
83                     item instanceof Map JavaDoc
84                 )
85                     s += size (item);
86             }
87             return s;
88         }
89         Map JavaDoc m = (Map JavaDoc) o;
90         int s = m.size ();
91         Iterator JavaDoc it = m.keySet ().iterator ();
92         while (it.hasNext ()) {
93             Object JavaDoc key = it.next ();
94             if (key instanceof Collection JavaDoc ||
95                 key instanceof Map JavaDoc
96             )
97                 s += size (key);
98             Object JavaDoc value = m.get (key);
99             if (value instanceof Collection JavaDoc ||
100                 value instanceof Map JavaDoc
101             )
102                 s += size (value);
103         }
104         return s;
105     }
106 }
107
Popular Tags