KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > stats > StatisticsManager


1 /*
2  * Copyright 2005-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */

16 package net.sf.dozer.util.mapping.stats;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
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 /**
31  * @author tierney.matt
32 */

33 public final class StatisticsManager implements StatisticsManagerIF {
34   private static final Log log = LogFactory.getLog(StatisticsManager.class);
35   
36   private final Map JavaDoc statisticsMap = new HashMap JavaDoc();
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 JavaDoc getStatisticEntries(String JavaDoc statisticType) {
47     return getStatistic(statisticType).getEntries();
48   }
49   
50   public Set JavaDoc getStatistics() {
51     return new HashSet JavaDoc(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 JavaDoc 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 JavaDoc getStatisticTypes() {
72     Set JavaDoc results = new HashSet JavaDoc();
73     Iterator JavaDoc iter = statisticsMap.entrySet().iterator();
74     while (iter.hasNext()) {
75       Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
76       results.add((String JavaDoc)entry.getKey());
77     }
78     return results;
79   }
80
81   /*
82    * Convenience method that should only be used for statistic types that
83    * will only ever have 1 statistic entry(value). For stats that only have one entry, it
84    * is assumed that the single entrie's key is the same as the stat type name
85    */

86   public void increment(String JavaDoc statisticType) {
87     increment(statisticType, 1);
88   }
89
90   public void increment(String JavaDoc statisticType, long value) {
91     increment(statisticType, statisticType, value);
92   }
93   
94   public void increment(String JavaDoc statisticType, Object JavaDoc statisticEntryKey) {
95     increment(statisticType, statisticEntryKey, 1);
96   }
97   
98   public void increment(String JavaDoc statisticType, Object JavaDoc statisticEntryKey, long value) {
99     //If statistics are not enabled, just return and do nothing.
100
if (!isStatisticsEnabled()) {
101       return;
102     }
103     
104     if (statisticType == null) {
105       throw new IllegalArgumentException JavaDoc("statistic type must be specified");
106     }
107     
108     if (statisticEntryKey == null) {
109       throw new IllegalArgumentException JavaDoc("statistic entry key must be specified");
110     }
111
112     //Get Statistic object for the specified type. If it doesnt aleady exist, create it
113
Statistic statistic = (Statistic) statisticsMap.get(statisticType);
114     if (statistic == null) {
115       statistic = new Statistic(statisticType);
116       addStatistic(statistic);
117     }
118     
119     //Get the Statistic Entry object which contains the actual value.
120
//If it doesnt aleady exist, create it so that it can be incremented
121
StatisticEntry statisticEntry = null;
122     statisticEntry = statistic.getEntry(statisticEntryKey);
123     if(statisticEntry == null) {
124       statisticEntry = new StatisticEntry(statisticEntryKey);
125       statistic.addEntry(statisticEntry);
126     }
127     
128     //Increment the actual value
129
statisticEntry.increment(value);
130   }
131   
132   /*
133    * Convenience method that should only be used for statistic types that
134    * will only ever have 1 statistic entry(value). getStatisticEntries() should
135    * be used for statistic types that have more than 1 statistic entry(value)
136    */

137   public long getStatisticValue(String JavaDoc statisticType) {
138     Set JavaDoc entries = getStatistic(statisticType).getEntries();
139     if (entries.size() > 1) {
140       throw new IllegalArgumentException JavaDoc("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 JavaDoc("Statistic already exists for type: " + statistic.getType());
148     }
149     statisticsMap.put(statistic.getType(), statistic);
150   }
151   
152   public boolean statisticExists(String JavaDoc statisticType) {
153     return statisticsMap.containsKey(statisticType);
154   }
155   
156   public void logStatistics() {
157     log.info(getStatistics());
158   }
159 }
160
Popular Tags