01: /**************************************************************************************
02: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
03: * http://aspectwerkz.codehaus.org *
04: * ---------------------------------------------------------------------------------- *
05: * The software in this package is published under the terms of the LGPL license *
06: * a copy of which has been included with this distribution in the license.txt file. *
07: **************************************************************************************/package examples.caching;
08:
09: import java.util.HashMap;
10: import java.util.Collections;
11: import java.util.Map;
12:
13: /**
14: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
15: */
16: public class CacheStatistics {
17:
18: private static Map s_methodInvocations = Collections
19: .synchronizedMap(new HashMap());
20:
21: private static Map s_cacheInvocations = Collections
22: .synchronizedMap(new HashMap());
23:
24: public static void addMethodInvocation(final String methodName,
25: final Class[] parameterTypes) {
26: Long hash = calculateHash(methodName, parameterTypes);
27: if (!s_methodInvocations.containsKey(hash)) {
28: s_methodInvocations.put(hash, new Integer(0));
29: }
30: int counter = ((Integer) s_methodInvocations.get(hash))
31: .intValue();
32: counter++;
33: s_methodInvocations.put(hash, new Integer(counter));
34: }
35:
36: public static void addCacheInvocation(final String methodName,
37: final Class[] parameterTypes) {
38: Long hash = calculateHash(methodName, parameterTypes);
39: if (!s_cacheInvocations.containsKey(hash)) {
40: s_cacheInvocations.put(hash, new Integer(0));
41: }
42: int counter = ((Integer) s_cacheInvocations.get(hash))
43: .intValue();
44: counter++;
45: s_cacheInvocations.put(hash, new Integer(counter));
46: }
47:
48: public static int getNrOfMethodInvocationsFor(
49: final String methodName, final Class[] parameterTypes) {
50: Integer number = (Integer) s_methodInvocations
51: .get(calculateHash(methodName, parameterTypes));
52: if (number != null) {
53: return number.intValue();
54: } else {
55: return 0;
56: }
57: }
58:
59: public static int getNrOfCacheInvocationsFor(
60: final String methodName, final Class[] parameterTypes) {
61: Integer number = (Integer) s_cacheInvocations
62: .get(calculateHash(methodName, parameterTypes));
63: if (number != null) {
64: return number.intValue();
65: } else {
66: return 0;
67: }
68: }
69:
70: private static Long calculateHash(final String methodName,
71: final Class[] parameterTypes) {
72: int result = 17;
73: result = 37 * result + methodName.hashCode();
74: for (int i = 0, j = parameterTypes.length; i < j; i++) {
75: result = 37 * result + parameterTypes[i].hashCode();
76: }
77: return new Long(result);
78: }
79: }
|