View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.rt.swingmonitor;
22  
23  import java.io.Serializable;
24  
25  import java.util.*;
26  
27  
28  /***
29   * @author Misha TODO To change the template for this generated type comment go
30   *         to Window - Preferences - Java - Code Style - Code Templates
31   */
32  public class InstanceCounter implements Serializable {
33      /***   DOCUMENT ME!   */
34      private HashMap hash = new HashMap();
35  
36      /***
37       * DOCUMENT ME!
38       *
39       * @param clazz DOCUMENT ME!
40       */
41      public void constructed(String clazz) {
42          getCounter(clazz).increment();
43      }
44  
45      public Counter getCounter(String clazz) {
46          Counter counter;
47          if (!hash.containsKey(clazz)) {
48              counter = new Counter();
49              hash.put(clazz, counter);
50          } else {
51              counter = (Counter) hash.get(clazz);
52          }
53          return counter;
54      }
55      
56      /* 
57       * TODO
58       */
59      public synchronized void reset() {
60          
61      }
62      
63      /***
64       * DOCUMENT ME!
65       *
66       * @param clazz DOCUMENT ME!
67       */
68      public void destroyed(String clazz) {
69          getCounter(clazz).decrement();
70      }
71  
72      /***
73       * DOCUMENT ME!
74       *
75       * @return DOCUMENT ME!
76       */
77      public synchronized Object[][] getReport() {
78          Object[][] retVal = new Object[hash.keySet().size()][3];
79          Iterator iter = hash.keySet().iterator();
80          int counter = 0;
81          while (iter.hasNext()) {
82              String clazz = (String) iter.next();
83              retVal[counter][0] = clazz;
84              retVal[counter][1] = new Integer(((Counter) hash.get(clazz)).getCounter());
85              retVal[counter][2] = new Integer(((Counter) hash.get(clazz)).getDeltaCounter());
86              counter++;
87          }
88  
89          return retVal;
90      }
91  
92      public void setMarker() {
93          Iterator iter = hash.entrySet().iterator();
94          while (iter.hasNext()) {
95              Counter element = (Counter)((Map.Entry) iter.next()).getValue();
96              element.setMarker();
97          }
98      }
99      
100     /***
101      * DOCUMENT ME!
102      *
103      * @author $author$
104      * @version $Revision: 1.4 $
105      */
106     class Counter {
107         /***   DOCUMENT ME!   */
108         private int counter = 0;
109         private int offset = 0;
110 
111         /***
112          * DOCUMENT ME!
113          *
114          * @return DOCUMENT ME!
115          */
116         protected int increment() {
117             return ++counter;
118         }
119 
120         /***
121          * DOCUMENT ME!
122          *
123          * @return DOCUMENT ME!
124          */
125         protected int decrement() {
126             return --counter;
127         }
128 
129         /***
130          * DOCUMENT ME!
131          *
132          * @return DOCUMENT ME!
133          */
134         protected int getCounter() {
135             return counter;
136         }
137         
138         protected void setMarker() {
139             offset = counter;
140         }
141         
142         protected int getDeltaCounter() {
143             return counter - offset;
144         }
145     }
146 }