01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.objectserver.impl;
05:
06: import com.tc.objectserver.api.ObjectInstanceMonitor;
07: import com.tc.objectserver.api.ObjectInstanceMonitorMBean;
08:
09: import gnu.trove.TObjectIntHashMap;
10: import gnu.trove.TObjectIntProcedure;
11:
12: import java.util.HashMap;
13: import java.util.Map;
14:
15: public class ObjectInstanceMonitorImpl implements
16: ObjectInstanceMonitor, ObjectInstanceMonitorMBean {
17:
18: private final TObjectIntHashMap instanceCounts = new TObjectIntHashMap();
19:
20: public ObjectInstanceMonitorImpl() {
21: //
22: }
23:
24: public synchronized void instanceCreated(String type) {
25: if (type == null) {
26: throw new IllegalArgumentException();
27: }
28:
29: if (!instanceCounts.increment(type)) {
30: instanceCounts.put(type, 1);
31: }
32: }
33:
34: public synchronized void instanceDestroyed(String type) {
35: if (type == null) {
36: throw new IllegalArgumentException();
37: }
38:
39: if (!instanceCounts.adjustValue(type, -1)) {
40: throw new IllegalStateException(
41: "No count available for type " + type);
42: }
43:
44: if (instanceCounts.get(type) <= 0) {
45: instanceCounts.remove(type);
46: }
47: }
48:
49: public synchronized Map getInstanceCounts() {
50: final Map rv = new HashMap();
51:
52: instanceCounts.forEachEntry(new TObjectIntProcedure() {
53: public boolean execute(Object key, int value) {
54: rv.put(key, new Integer(value));
55: return true;
56: }
57: });
58:
59: return rv;
60: }
61: }
|