KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > MethodCounter


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.aop.framework;
18
19 import java.io.Serializable JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.HashMap JavaDoc;
22
23 /**
24  * Useful abstract superclass for counting advices etc.
25  *
26  * @author Rod Johnson
27  */

28 public class MethodCounter implements Serializable JavaDoc {
29     
30     /** Method name --> count, does not understand overloading */
31     private HashMap JavaDoc map = new HashMap JavaDoc();
32     
33     private int allCount;
34     
35     protected void count(Method JavaDoc m) {
36         count(m.getName());
37     }
38     
39     protected void count(String JavaDoc methodName) {
40         Integer JavaDoc I = (Integer JavaDoc) map.get(methodName);
41         I = (I != null) ? new Integer JavaDoc(I.intValue() + 1) : new Integer JavaDoc(1);
42         map.put(methodName, I);
43         ++allCount;
44     }
45     
46     public int getCalls(String JavaDoc methodName) {
47         Integer JavaDoc I = (Integer JavaDoc) map.get(methodName);
48         return (I != null) ? I.intValue() : 0;
49     }
50     
51     public int getCalls() {
52         return allCount;
53     }
54     
55     /**
56      * A bit simplistic: just wants the same class.
57      * Doesn't worry about counts.
58      * @see java.lang.Object#equals(java.lang.Object)
59      */

60     public boolean equals(Object JavaDoc other) {
61         if (other == null || other.getClass() != this.getClass()) {
62             return false;
63         }
64         MethodCounter mc2 = (MethodCounter) other;
65         return true;
66     }
67
68 }
69
Popular Tags