KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > management > opentypes > adapters > ClassCreationCount


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