1 8 package examples.caching; 9 10 import java.util.HashMap ; 11 import java.util.Collections ; 12 import java.util.Map ; 13 14 17 public class CacheStatistics { 18 19 private static Map s_methodInvocations = Collections.synchronizedMap(new HashMap ()); 20 21 private static Map s_cacheInvocations = Collections.synchronizedMap(new HashMap ()); 22 23 public static void addMethodInvocation(final String methodName, final Class [] parameterTypes) { 24 Long hash = calculateHash(methodName, parameterTypes); 25 if (!s_methodInvocations.containsKey(hash)) { 26 s_methodInvocations.put(hash, new Integer (0)); 27 } 28 int counter = ((Integer ) s_methodInvocations.get(hash)).intValue(); 29 counter++; 30 s_methodInvocations.put(hash, new Integer (counter)); 31 } 32 33 public static void addCacheInvocation(final String methodName, final Class [] parameterTypes) { 34 Long hash = calculateHash(methodName, parameterTypes); 35 if (!s_cacheInvocations.containsKey(hash)) { 36 s_cacheInvocations.put(hash, new Integer (0)); 37 } 38 int counter = ((Integer ) s_cacheInvocations.get(hash)).intValue(); 39 counter++; 40 s_cacheInvocations.put(hash, new Integer (counter)); 41 } 42 43 public static int getNrOfMethodInvocationsFor(final String methodName, 44 final Class [] parameterTypes) { 45 Integer number = (Integer ) s_methodInvocations 46 .get(calculateHash(methodName, parameterTypes)); 47 if (number != null) { 48 return number.intValue(); 49 } else { 50 return 0; 51 } 52 } 53 54 public static int getNrOfCacheInvocationsFor(final String methodName, 55 final Class [] parameterTypes) { 56 Integer number = (Integer ) s_cacheInvocations 57 .get(calculateHash(methodName, parameterTypes)); 58 if (number != null) { 59 return number.intValue(); 60 } else { 61 return 0; 62 } 63 } 64 65 private static Long calculateHash(final String methodName, final Class [] parameterTypes) { 66 int result = 17; 67 result = 37 * result + methodName.hashCode(); 68 for (int i = 0, j = parameterTypes.length; i < j; i++) { 69 result = 37 * result + parameterTypes[i].hashCode(); 70 } 71 return new Long (result); 72 } 73 } | Popular Tags |