1 16 package net.sf.dozer.util.mapping.stats; 17 18 import java.util.HashMap ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import net.sf.dozer.util.mapping.config.GlobalSettings; 25 import net.sf.dozer.util.mapping.exception.NotFoundException; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 33 public final class StatisticsManager implements StatisticsManagerIF { 34 private static final Log log = LogFactory.getLog(StatisticsManager.class); 35 36 private final Map statisticsMap = new HashMap (); 37 private boolean isStatisticsEnabled = GlobalSettings.getInstance().getSettings().isStatisticsEnabled(); 38 39 public StatisticsManager() { 40 } 41 42 public void clearAll() { 43 statisticsMap.clear(); 44 } 45 46 public Set getStatisticEntries(String statisticType) { 47 return getStatistic(statisticType).getEntries(); 48 } 49 50 public Set getStatistics() { 51 return new HashSet (statisticsMap.values()); 52 } 53 54 public boolean isStatisticsEnabled() { 55 return isStatisticsEnabled; 56 } 57 58 public void setStatisticsEnabled(boolean statisticsEnabled) { 59 this.isStatisticsEnabled = statisticsEnabled; 60 GlobalSettings.getInstance().getSettings().setStatisticsEnabled(statisticsEnabled); 61 } 62 63 public Statistic getStatistic(String statisticType) { 64 Statistic result = (Statistic) statisticsMap.get(statisticType); 65 if (result == null) { 66 throw new NotFoundException("Unable to find statistic for type: " + statisticType); 67 } 68 return result; 69 } 70 71 public Set getStatisticTypes() { 72 Set results = new HashSet (); 73 Iterator iter = statisticsMap.entrySet().iterator(); 74 while (iter.hasNext()) { 75 Map.Entry entry = (Map.Entry )iter.next(); 76 results.add((String )entry.getKey()); 77 } 78 return results; 79 } 80 81 86 public void increment(String statisticType) { 87 increment(statisticType, 1); 88 } 89 90 public void increment(String statisticType, long value) { 91 increment(statisticType, statisticType, value); 92 } 93 94 public void increment(String statisticType, Object statisticEntryKey) { 95 increment(statisticType, statisticEntryKey, 1); 96 } 97 98 public void increment(String statisticType, Object statisticEntryKey, long value) { 99 if (!isStatisticsEnabled()) { 101 return; 102 } 103 104 if (statisticType == null) { 105 throw new IllegalArgumentException ("statistic type must be specified"); 106 } 107 108 if (statisticEntryKey == null) { 109 throw new IllegalArgumentException ("statistic entry key must be specified"); 110 } 111 112 Statistic statistic = (Statistic) statisticsMap.get(statisticType); 114 if (statistic == null) { 115 statistic = new Statistic(statisticType); 116 addStatistic(statistic); 117 } 118 119 StatisticEntry statisticEntry = null; 122 statisticEntry = statistic.getEntry(statisticEntryKey); 123 if(statisticEntry == null) { 124 statisticEntry = new StatisticEntry(statisticEntryKey); 125 statistic.addEntry(statisticEntry); 126 } 127 128 statisticEntry.increment(value); 130 } 131 132 137 public long getStatisticValue(String statisticType) { 138 Set entries = getStatistic(statisticType).getEntries(); 139 if (entries.size() > 1) { 140 throw new IllegalArgumentException ("More than one value entry found for stat type: " + statisticType); 141 } 142 return ((StatisticEntry)entries.iterator().next()).getValue(); 143 } 144 145 public void addStatistic(Statistic statistic) { 146 if (statisticExists(statistic.getType())) { 147 throw new IllegalArgumentException ("Statistic already exists for type: " + statistic.getType()); 148 } 149 statisticsMap.put(statistic.getType(), statistic); 150 } 151 152 public boolean statisticExists(String statisticType) { 153 return statisticsMap.containsKey(statisticType); 154 } 155 156 public void logStatistics() { 157 log.info(getStatistics()); 158 } 159 } 160 | Popular Tags |