KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > caching > CacheStatistics


1 /**************************************************************************************
2  * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package examples.caching;
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Collections JavaDoc;
12 import java.util.Map JavaDoc;
13
14 /**
15  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
16  */

17 public class CacheStatistics {
18
19     private static Map JavaDoc s_methodInvocations = Collections.synchronizedMap(new HashMap JavaDoc());
20
21     private static Map JavaDoc s_cacheInvocations = Collections.synchronizedMap(new HashMap JavaDoc());
22
23     public static void addMethodInvocation(final String JavaDoc methodName, final Class JavaDoc[] parameterTypes) {
24         Long JavaDoc hash = calculateHash(methodName, parameterTypes);
25         if (!s_methodInvocations.containsKey(hash)) {
26             s_methodInvocations.put(hash, new Integer JavaDoc(0));
27         }
28         int counter = ((Integer JavaDoc) s_methodInvocations.get(hash)).intValue();
29         counter++;
30         s_methodInvocations.put(hash, new Integer JavaDoc(counter));
31     }
32
33     public static void addCacheInvocation(final String JavaDoc methodName, final Class JavaDoc[] parameterTypes) {
34         Long JavaDoc hash = calculateHash(methodName, parameterTypes);
35         if (!s_cacheInvocations.containsKey(hash)) {
36             s_cacheInvocations.put(hash, new Integer JavaDoc(0));
37         }
38         int counter = ((Integer JavaDoc) s_cacheInvocations.get(hash)).intValue();
39         counter++;
40         s_cacheInvocations.put(hash, new Integer JavaDoc(counter));
41     }
42
43     public static int getNrOfMethodInvocationsFor(final String JavaDoc methodName,
44                                                   final Class JavaDoc[] parameterTypes) {
45         Integer JavaDoc number = (Integer JavaDoc) 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 JavaDoc methodName,
55                                                  final Class JavaDoc[] parameterTypes) {
56         Integer JavaDoc number = (Integer JavaDoc) 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 JavaDoc calculateHash(final String JavaDoc methodName, final Class JavaDoc[] 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 JavaDoc(result);
72     }
73 }
Popular Tags