KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > impl > ObjectInstanceMonitorImpl


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.objectserver.impl;
5
6 import com.tc.objectserver.api.ObjectInstanceMonitor;
7 import com.tc.objectserver.api.ObjectInstanceMonitorMBean;
8
9 import gnu.trove.TObjectIntHashMap;
10 import gnu.trove.TObjectIntProcedure;
11
12 import java.util.HashMap JavaDoc;
13 import java.util.Map JavaDoc;
14
15 public class ObjectInstanceMonitorImpl implements ObjectInstanceMonitor, ObjectInstanceMonitorMBean {
16
17   private final TObjectIntHashMap instanceCounts = new TObjectIntHashMap();
18
19   public ObjectInstanceMonitorImpl() {
20     //
21
}
22
23   public synchronized void instanceCreated(String JavaDoc type) {
24     if (type == null) { throw new IllegalArgumentException JavaDoc(); }
25
26     if (!instanceCounts.increment(type)) {
27       instanceCounts.put(type, 1);
28     }
29   }
30
31   public synchronized void instanceDestroyed(String JavaDoc type) {
32     if (type == null) { throw new IllegalArgumentException JavaDoc(); }
33
34     if (!instanceCounts.adjustValue(type, -1)) { throw new IllegalStateException JavaDoc("No count available for type " + type); }
35
36     if (instanceCounts.get(type) <= 0) {
37       instanceCounts.remove(type);
38     }
39   }
40
41   public synchronized Map getInstanceCounts() {
42     final Map rv = new HashMap();
43
44     instanceCounts.forEachEntry(new TObjectIntProcedure() {
45       public boolean execute(Object JavaDoc key, int value) {
46         rv.put(key, new Integer JavaDoc(value));
47         return true;
48       }
49     });
50
51     return rv;
52   }
53 }
54
Popular Tags