1 4 package com.tc.management.opentypes.adapters; 5 6 import java.util.ArrayList ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 10 import javax.management.openmbean.CompositeData ; 11 import javax.management.openmbean.CompositeDataSupport ; 12 import javax.management.openmbean.CompositeType ; 13 import javax.management.openmbean.OpenDataException ; 14 import javax.management.openmbean.OpenType ; 15 import javax.management.openmbean.SimpleType ; 16 import javax.management.openmbean.TabularData ; 17 import javax.management.openmbean.TabularDataSupport ; 18 import javax.management.openmbean.TabularType ; 19 20 public class ClassCreationCount implements Comparable { 21 22 private static final String COMPOSITE_TYPE_NAME = "ObjectCreationCountPerClass"; 23 private static final String COMPOSITE_TYPE_DESCRIPTION = "Number of objects created per class"; 24 private static final String [] ITEM_NAMES = new String [] { "className", "objectCreationCount" }; 25 private static final String [] ITEM_DESCRIPTIONS = new String [] { "className", "objectCreationCount" }; 26 private static final OpenType [] ITEM_TYPES = new OpenType [] { SimpleType.STRING, 27 SimpleType.INTEGER }; 28 private static final CompositeType COMPOSITE_TYPE; 29 private static final String TABULAR_TYPE_NAME = "ObjectCreationCountByClass"; 30 private static final String TABULAR_TYPE_DESCRIPTION = "Object creation count by class"; 31 private static final String [] INDEX_NAMES = new String [] { "className" }; 32 private static final TabularType TABULAR_TYPE; 33 34 static { 35 try { 36 COMPOSITE_TYPE = new CompositeType (COMPOSITE_TYPE_NAME, COMPOSITE_TYPE_DESCRIPTION, ITEM_NAMES, 37 ITEM_DESCRIPTIONS, ITEM_TYPES); 38 TABULAR_TYPE = new TabularType (TABULAR_TYPE_NAME, TABULAR_TYPE_DESCRIPTION, COMPOSITE_TYPE, INDEX_NAMES); 39 } catch (OpenDataException e) { 40 throw new RuntimeException (e); 41 } 42 } 43 44 private final String className; 45 private final Integer count; 46 47 public ClassCreationCount(final String className, final Integer count) { 48 this.className = className; 49 this.count = count; 50 } 51 52 public String getClassName() { 53 return className; 54 } 55 56 public Integer getCount() { 57 return count; 58 } 59 60 public ClassCreationCount(final CompositeData cData) { 61 className = (String ) cData.get(ITEM_NAMES[0]); 62 count = (Integer ) cData.get(ITEM_NAMES[1]); 63 } 64 65 public CompositeData toCompositeData() { 66 try { 67 return new CompositeDataSupport (COMPOSITE_TYPE, ITEM_NAMES, new Object [] { className, count }); 68 } catch (OpenDataException e) { 69 throw new RuntimeException (e); 70 } 71 } 72 73 public static TabularData newTabularDataInstance() { 74 return new TabularDataSupport (TABULAR_TYPE); 75 } 76 77 public static ClassCreationCount[] fromTabularData(final TabularData tabularData) { 78 final List countList = new ArrayList (tabularData.size()); 79 for (final Iterator pos = tabularData.values().iterator(); pos.hasNext();) { 80 countList.add(new ClassCreationCount((CompositeData ) pos.next())); 81 } 82 final ClassCreationCount[] counts = new ClassCreationCount[countList.size()]; 83 countList.toArray(counts); 84 return counts; 85 } 86 87 public int compareTo(final Object theOtherGuy) { 88 ClassCreationCount rhs = (ClassCreationCount) theOtherGuy; 89 int result = count.compareTo(rhs.count); 90 return result != 0 ? result : className.compareTo(rhs.className); 91 } 92 } 93 | Popular Tags |