KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > util > Stats


1 /**
2  * com.mckoi.util.Stats 04 Jul 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.util;
26
27 import java.util.*;
28 import java.io.PrintStream JavaDoc;
29
30 /**
31  * An object that is used to store and update various stats.
32  * <p>
33  * NOTE: This object is thread safe.
34  *
35  * @author Tobias Downer
36  */

37
38 public final class Stats {
39
40   /**
41    * Where the stat properties are held.
42    */

43   private HashMap properties;
44
45   /**
46    * Constructs the object.
47    */

48   public Stats() {
49     // We need lookup on this hash to be really quick, so load factor is
50
// low and initial capacity is high.
51
properties = new HashMap(250, 0.50f);
52   }
53
54   /**
55    * Resets all stats that start with "{session}" to 0. This should be
56    * called when we are collecting stats over a given session and a session
57    * has finished.
58    */

59   public synchronized void resetSession() {
60     Set key_set = properties.keySet();
61     String JavaDoc[] keys = new String JavaDoc[key_set.size()];
62     int index = 0;
63     Iterator it = key_set.iterator();
64     while (it.hasNext()) {
65       keys[index] = (String JavaDoc) it.next();
66       ++index;
67     }
68
69     // If key starts with a "{session}" then reset it to 0.
70
for (int i = 0; i < keys.length; ++i) {
71       if (keys[i].startsWith("{session}")) {
72         IntegerStat stat = (IntegerStat) properties.get(keys[i]);
73         stat.value = 0;
74       }
75     }
76   }
77
78   /**
79    * Adds the given value to a stat property.
80    */

81   public synchronized void add(int value, String JavaDoc stat_name) {
82     IntegerStat stat = (IntegerStat) properties.get(stat_name);
83     if (stat != null) {
84       stat.value += value;
85     }
86     else {
87       stat = new IntegerStat();
88       stat.value = value;
89       properties.put(stat_name, stat);
90     }
91   }
92
93   /**
94    * Increments a stat property. eg.
95    * stats.increment("File Hits");
96    */

97   public synchronized void increment(String JavaDoc stat_name) {
98     IntegerStat stat = (IntegerStat) properties.get(stat_name);
99     if (stat != null) {
100       ++stat.value;
101     }
102     else {
103       stat = new IntegerStat();
104       stat.value = 1;
105       properties.put(stat_name, stat);
106     }
107   }
108
109   /**
110    * Decrements a stat property.
111    */

112   public synchronized void decrement(String JavaDoc stat_name) {
113     IntegerStat stat = (IntegerStat) properties.get(stat_name);
114     if (stat != null) {
115       --stat.value;
116     }
117     else {
118       stat = new IntegerStat();
119       stat.value = -1;
120       properties.put(stat_name, stat);
121     }
122   }
123
124   /**
125    * Retrieves the current Object value of a stat property. Returns null if
126    * the stat wasn't found.
127    */

128   public synchronized Object JavaDoc get(String JavaDoc stat_name) {
129     IntegerStat stat = (IntegerStat) properties.get(stat_name);
130     if (stat != null) {
131       return new Long JavaDoc(stat.value);
132     }
133     return null;
134   }
135
136   /**
137    * Sets the given stat name with the given value.
138    */

139   public synchronized void set(int value, String JavaDoc stat_name) {
140     IntegerStat stat = (IntegerStat) properties.get(stat_name);
141     if (stat != null) {
142       stat.value = value;
143     }
144     else {
145       stat = new IntegerStat();
146       stat.value = value;
147       properties.put(stat_name, stat);
148     }
149   }
150
151   /**
152    * Return a String array of all stat keys sorted in order from lowest to
153    * highest.
154    */

155   public synchronized String JavaDoc[] keyList() {
156     Set key_set = properties.keySet();
157
158     String JavaDoc[] keys = new String JavaDoc[key_set.size()];
159     int index = 0;
160     Iterator it = key_set.iterator();
161     while (it.hasNext()) {
162       keys[index] = (String JavaDoc) it.next();
163       ++index;
164     }
165
166     // Sort the keys
167
Arrays.sort(keys, STRING_COMPARATOR);
168
169     return keys;
170   }
171
172   /**
173    * Comparator for sorting the list of keys (for 1.1 implementation without
174    * Comparable String objects).
175    */

176   final static Comparator STRING_COMPARATOR = new Comparator() {
177     public int compare(Object JavaDoc ob1, Object JavaDoc ob2) {
178       return ((String JavaDoc) ob1).compareTo((String JavaDoc) ob2);
179     }
180   };
181
182
183
184   /**
185    * Returns a String representation of the stat with the given key name.
186    */

187   public synchronized String JavaDoc statString(String JavaDoc key) {
188     IntegerStat stat = (IntegerStat) properties.get(key);
189     return Long.toString(stat.value);
190   }
191
192   /**
193    * Returns a String that can be use to print out the values of all the stats.
194    */

195   public synchronized String JavaDoc toString() {
196
197     String JavaDoc[] keys = keyList();
198
199     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
200     for (int i = 0; i < keys.length; ++i) {
201       IntegerStat stat = (IntegerStat) properties.get(keys[i]);
202       buf.append(keys[i]);
203       buf.append(": ");
204       buf.append(stat.value);
205       buf.append('\n');
206     }
207
208     return new String JavaDoc(buf);
209   }
210
211   /**
212    * Outputs the stats to a print stream.
213    */

214   public synchronized void printTo(PrintStream JavaDoc out) {
215
216     String JavaDoc[] keys = keyList();
217
218     for (int i = 0; i < keys.length; ++i) {
219       IntegerStat stat = (IntegerStat) properties.get(keys[i]);
220       out.print(keys[i]);
221       out.print(": ");
222       out.println(stat.value);
223     }
224   }
225
226
227
228
229   // ---------- Inner class ----------
230

231   private static class IntegerStat {
232     long value;
233   }
234
235 }
236
Popular Tags