View Javadoc

1   package org.sourceforge.jemm.client.events;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import org.sourceforge.jemm.client.Descriptor;
7   
8   /**
9    * A context that represents a JEMMObject within the system.
10   * 
11   * This class is thread safe.
12   * 
13   * @author Paul
14   *
15   */
16  public class EntityContext extends SimpleContext{
17  	Object obj;
18  	Map<Descriptor, MethodContext> methods;
19  	
20  	public EntityContext(Object obj) {
21  		this.obj = obj;
22  		methods = new HashMap<Descriptor,MethodContext>();
23  	}
24  	
25  	/**
26  	 * Adds a stack trace for both this entity and for the MethodContext.
27  	 * 
28  	 * @param method The method to add to
29  	 * @param thread The thread to add
30  	 */
31  	public synchronized void add(Descriptor method, Thread thread) {
32  		if(!methods.containsKey(method)) {
33  			methods.put(method, new MethodContext(method));
34  		}
35  		
36  		MethodContext mContext = methods.get(method);
37  		mContext.add(thread);
38  		add(thread);
39  	}
40  	
41  	public void add(Descriptor method) {
42  		add(method,Thread.currentThread());
43  	}
44  	
45  	public synchronized boolean has(Descriptor method, Thread thread) {
46  		if(methods.containsKey(method)) {
47  			MethodContext mContext = methods.get(method);
48  			return mContext.has(thread);
49  		}
50  		return false;
51  	}
52  	
53  	/**
54  	 * If the method has any threads within its context then returns true,
55  	 * otherwise returns false.
56  	 * 
57  	 * @param method The method to check
58  	 * @return
59  	 */
60  	public boolean has(Descriptor method) {
61  		return has(method,Thread.currentThread());
62  	}
63  	
64  	public synchronized void remove(Descriptor method, Thread thread) {
65  		if(methods.containsKey(method)) {
66  			MethodContext mContext = methods.get(method);
67  			mContext.remove(thread);
68  			
69  			if(!mContext.hasAny())
70  				methods.remove(method);
71  			
72  		}
73  		remove(thread);
74  	}
75  	
76  	public void remove(Descriptor method) {
77  		remove(method,Thread.currentThread());
78  	}
79  }