1 22 package org.jboss.varia.stats; 23 24 import java.io.Serializable ; 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 29 33 public class TxReport 34 implements Serializable 35 { 36 private static final String DEFAULT_NAME = "UNKNOWN"; 37 38 private String name = DEFAULT_NAME; 39 private final Map stats = new HashMap (); 40 private int count = 1; 41 42 public String getName() 43 { 44 return name; 45 } 46 47 public int getCount() 48 { 49 return count; 50 } 51 52 public Map getStats() 53 { 54 return stats; 55 } 56 57 public boolean addItem(StatisticalItem item) 58 { 59 if(name == DEFAULT_NAME) 60 { 61 name = item.getValue(); 62 } 63 64 boolean addedNew = false; 65 Map itemMap = (Map ) stats.get(item.getName()); 66 if(itemMap == null) 67 { 68 itemMap = new HashMap (); 69 stats.put(item.getName(), itemMap); 70 addedNew = true; 71 } 72 73 StatisticalItem curItem = (StatisticalItem) itemMap.get(item.getValue()); 74 if(curItem == null) 75 { 76 itemMap.put(item.getValue(), item); 77 } 78 else 79 { 80 curItem.add(item); 81 } 82 return addedNew; 83 } 84 85 88 public void merge(TxReport txReport) 89 { 90 for(Iterator iter = txReport.stats.entrySet().iterator(); iter.hasNext();) 91 { 92 Map.Entry entry = (Map.Entry ) iter.next(); 93 String itemName = (String ) entry.getKey(); 94 95 Map myMap = (Map ) stats.get(itemName); 96 Map itemMap = (Map ) entry.getValue(); 97 98 if(myMap == null) 99 { 100 stats.put(itemName, itemMap); 101 } 102 else 103 { 104 for(Iterator myItems = myMap.values().iterator(); myItems.hasNext();) 106 { 107 StatisticalItem myItem = (StatisticalItem) myItems.next(); 108 StatisticalItem newItem = (StatisticalItem) itemMap.remove(myItem.getValue()); 109 110 if(newItem == null) 111 { 112 myItem.mergeNull(); 113 } 114 else 115 { 116 myItem.merge(newItem); 117 } 118 } 119 120 if(!itemMap.isEmpty()) 122 { 123 for(Iterator newItems = itemMap.values().iterator(); newItems.hasNext();) 124 { 125 StatisticalItem newItem = (StatisticalItem) newItems.next(); 126 myMap.put(newItem.getValue(), newItem); 127 } 128 } 129 } 130 } 131 132 count += txReport.count; 133 } 134 135 137 public static class MethodStats extends AbstractStatisticalItem 138 { 139 public static final String NAME = "Method Statistics Per Transaction"; 140 141 public MethodStats(String method) 142 { 143 super(NAME); 144 value = method; 145 } 146 } 147 148 public static class SqlStats extends AbstractStatisticalItem 149 { 150 public static final String NAME = "SQL Statistics Per Transaction"; 151 152 public SqlStats(String sql) 153 { 154 super(NAME); 155 value = sql; 156 } 157 } 158 } 159 | Popular Tags |