01: package org.continuent.sequoia.console.text.commands.dbadmin;
02:
03: import java.util.ArrayList;
04: import java.util.Collection;
05: import java.util.Collections;
06: import java.util.Comparator;
07: import java.util.Iterator;
08: import java.util.List;
09:
10: import javax.management.openmbean.CompositeData;
11: import javax.management.openmbean.TabularData;
12:
13: public class JMXUtils {
14:
15: /**
16: * Convert a Collection<CompositeData> to a matrix of String.
17: *
18: * @param collection a Collection<CompositeData>
19: * @param headers the headers corresponding to the items to retrieve for each
20: * CompositeDate
21: * @return a matrix of String
22: */
23: static String[][] from(Collection/*<CompositeData>*/collection,
24: String[] headers) {
25: String[][] entries = new String[collection.size()][];
26: Iterator iter = collection.iterator();
27: int i = 0;
28: while (iter.hasNext()) {
29: CompositeData logEntry = (CompositeData) iter.next();
30: Object[] logEntryItems = logEntry.getAll(headers);
31: String[] entry = new String[logEntryItems.length];
32: for (int j = 0; j < entry.length; j++) {
33: Object logEntryItem = logEntryItems[j];
34: entry[j] = "" + logEntryItem; //$NON-NLS-1$
35: }
36: entries[i] = entry;
37: i++;
38: }
39: return entries;
40: }
41:
42: /**
43: * Sort tabular data based on one given key.
44: * The entries will be sorted only if the value corresponding to the given
45: * key is Comparable.
46: *
47: * @param tabularData a TabularData
48: * @param key the key to use to sort the tabular data.
49: *
50: * @return a List<CompositeData> where the composite data are sorted by
51: * the specified <code>key</code>
52: */
53: static List/*<CompositeData>*/sortEntriesByKey(
54: TabularData tabularData, final String key) {
55: List entries = new ArrayList(tabularData.values());
56: Collections.sort(entries, new Comparator() {
57:
58: public int compare(Object o1, Object o2) {
59: CompositeData entry1 = (CompositeData) o1;
60: CompositeData entry2 = (CompositeData) o2;
61:
62: Object value1 = entry1.get(key);
63: Object value2 = entry2.get(key);
64:
65: if (value1 != null && value1 instanceof Comparable
66: && value2 instanceof Comparable) {
67: Comparable comp1 = (Comparable) value1;
68: Comparable comp2 = (Comparable) value2;
69: return comp1.compareTo(comp2);
70: }
71: return 0;
72: }
73: });
74: return entries;
75: }
76: }
|