View Javadoc

1   package org.sourceforge.jemm.client.events;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   /**
7    * A basic Context implementation that counts the number of
8    * thread add and removes.
9    * 
10   * This class is thread safe.
11   * 
12   * @author Paul
13   *
14   */
15  public class SimpleContext implements Context {
16  	Map<Thread,Integer> tracked;
17  	
18  	public SimpleContext() {
19  		tracked = new HashMap<Thread,Integer>();
20  	}
21  	
22  	public synchronized void add(Thread thread) {
23  		Integer i= tracked.get(thread);
24  		if(i == null) {
25  			tracked.put(thread, 1);
26  		} else {
27  			tracked.put(thread, i +1);
28  		}
29  	}
30  	
31  	public void add() {
32  		add(Thread.currentThread());
33  	}
34  
35  	public synchronized boolean has(Thread t) {
36  		return tracked.containsKey(t);
37  	}
38  	
39  	public synchronized boolean hasAny() {
40  		return tracked.size() >0;
41  	}
42  	
43  	public boolean has() {
44  		return has(Thread.currentThread());
45  	}
46  	
47  	public synchronized void remove(Thread t) {
48  		Integer i= tracked.get(t);
49  		if(i==null)
50  			return;
51  		
52  		if(i>0)
53  			--i;
54  		
55  		if(i<=0)
56  			tracked.remove(t);
57  		else
58  			tracked.put(t, i);
59  	}
60  	
61  	public void remove() {
62  		remove(Thread.currentThread());
63  	}
64  
65  }