1 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 ; 13 import java.util.Map ; 14 15 public class ObjectInstanceMonitorImpl implements ObjectInstanceMonitor, ObjectInstanceMonitorMBean { 16 17 private final TObjectIntHashMap instanceCounts = new TObjectIntHashMap(); 18 19 public ObjectInstanceMonitorImpl() { 20 } 22 23 public synchronized void instanceCreated(String type) { 24 if (type == null) { throw new IllegalArgumentException (); } 25 26 if (!instanceCounts.increment(type)) { 27 instanceCounts.put(type, 1); 28 } 29 } 30 31 public synchronized void instanceDestroyed(String type) { 32 if (type == null) { throw new IllegalArgumentException (); } 33 34 if (!instanceCounts.adjustValue(type, -1)) { throw new IllegalStateException ("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 key, int value) { 46 rv.put(key, new Integer (value)); 47 return true; 48 } 49 }); 50 51 return rv; 52 } 53 } 54 | Popular Tags |