1 24 25 package com.mckoi.util; 26 27 import java.util.*; 28 import java.io.PrintStream ; 29 30 37 38 public final class Stats { 39 40 43 private HashMap properties; 44 45 48 public Stats() { 49 properties = new HashMap(250, 0.50f); 52 } 53 54 59 public synchronized void resetSession() { 60 Set key_set = properties.keySet(); 61 String [] keys = new String [key_set.size()]; 62 int index = 0; 63 Iterator it = key_set.iterator(); 64 while (it.hasNext()) { 65 keys[index] = (String ) it.next(); 66 ++index; 67 } 68 69 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 81 public synchronized void add(int value, String 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 97 public synchronized void increment(String 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 112 public synchronized void decrement(String 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 128 public synchronized Object get(String stat_name) { 129 IntegerStat stat = (IntegerStat) properties.get(stat_name); 130 if (stat != null) { 131 return new Long (stat.value); 132 } 133 return null; 134 } 135 136 139 public synchronized void set(int value, String 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 155 public synchronized String [] keyList() { 156 Set key_set = properties.keySet(); 157 158 String [] keys = new String [key_set.size()]; 159 int index = 0; 160 Iterator it = key_set.iterator(); 161 while (it.hasNext()) { 162 keys[index] = (String ) it.next(); 163 ++index; 164 } 165 166 Arrays.sort(keys, STRING_COMPARATOR); 168 169 return keys; 170 } 171 172 176 final static Comparator STRING_COMPARATOR = new Comparator() { 177 public int compare(Object ob1, Object ob2) { 178 return ((String ) ob1).compareTo((String ) ob2); 179 } 180 }; 181 182 183 184 187 public synchronized String statString(String key) { 188 IntegerStat stat = (IntegerStat) properties.get(key); 189 return Long.toString(stat.value); 190 } 191 192 195 public synchronized String toString() { 196 197 String [] keys = keyList(); 198 199 StringBuffer buf = new StringBuffer (); 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 (buf); 209 } 210 211 214 public synchronized void printTo(PrintStream out) { 215 216 String [] 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 231 private static class IntegerStat { 232 long value; 233 } 234 235 } 236 | Popular Tags |