View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2010 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.tests.aspects.util;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.springframework.util.Assert;
26  
27  /**
28   * Invocation monitor to count per class and see the invocation order.
29   *
30   * @svnLink $Revision: 4253 $;$Date: 2010-12-21 11:08:04 +0100 (Di, 21. Dez 2010) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/tests/aspects/src/main/java/ch/elca/el4j/tests/aspects/util/InvocationMonitor.java $
31   *
32   * @author Martin Zeltner (MZE)
33   */
34  public final class InvocationMonitor {
35  	/**
36  	 * Private thread-save class-specific counter variables.
37  	 */
38  	private static Map<Class<?>, Integer> s_classCounterMap;
39  	
40  	/**
41  	 * Private thread-save list of count invocations.
42  	 */
43  	private static List<Class<?>> s_invocationList;
44  	
45  	/**
46  	 * Static constructor clears the invocation monitor.
47  	 */
48  	static {
49  		clear();
50  	}
51  	
52  	/**
53  	 * Hide default constructor.
54  	 */
55  	private InvocationMonitor() { }
56  	
57  	/**
58  	 * Clears the invocation monitor.
59  	 */
60  	public static synchronized void clear() {
61  		s_classCounterMap = Collections.synchronizedMap(new HashMap<Class<?>, Integer>());
62  		s_invocationList = Collections.synchronizedList(new ArrayList<Class<?>>());
63  	}
64  	
65  	/**
66  	 * Initializes the static counter to zero for the given clazz.
67  	 * 
68  	 * @param clazz Is the counter class.
69  	 */
70  	public static synchronized void initCounter(Class<?> clazz) {
71  		Assert.notNull(clazz);
72  		s_classCounterMap.remove(clazz);
73  		s_classCounterMap.put(clazz, 0);
74  	}
75  
76  	/**
77  	 * @return Returns the current counter value of the given class.
78  	 * 
79  	 * @param clazz Is the counter class.
80  	 */
81  	public static synchronized int getCounter(Class<?> clazz) {
82  		Assert.isTrue(s_classCounterMap.containsKey(clazz));
83  		return s_classCounterMap.get(clazz);
84  	}
85  	
86  	/**
87  	 * Increments the counter variable of the given class.
88  	 * 
89  	 * @param clazz Is the counter class.
90  	 */
91  	public static synchronized void incrementCounter(Class<?> clazz) {
92  		Assert.isTrue(s_classCounterMap.containsKey(clazz));
93  		int i = s_classCounterMap.get(clazz);
94  		s_classCounterMap.put(clazz, i + 1);
95  		s_invocationList.add(clazz);
96  	}
97  	
98  	/**
99  	 * @return Returns a copy of the invocation list.
100 	 */
101 	public static synchronized List<Class<?>> getInvocationList() {
102 		return new ArrayList<Class<?>>(s_invocationList);
103 	}
104 }