View Javadoc

1   package cz.cuni.amis.pogamut.base.agent.state.impl;
2   
3   import java.io.Serializable;
4   
5   import cz.cuni.amis.pogamut.base.agent.state.level0.IAgentState;
6   import cz.cuni.amis.utils.SafeEquals;
7   
8   /**
9    * Wrapper for the AgentState adding additional String information that provides
10   * more details about the agent state.
11   * <p><p>
12   * WARNING: equals() defined according to it'description as well only!
13   * <p><p>
14   * Note that we can not assume which state the agent may find itself in, so we will use a Java object hierarchy to represent
15   * it. We provide a set of interfaces that may extend each other to provide the information about the state, e.g.
16   * the state may be only {@IAgentUp} but we may create also interface {@IAgentRunning} that extends {@IAgentUp} thus
17   * allowing other objects to know, that {@IAgentRunning} means that agent is healthy because that is what {@IAgentUp} state
18   * declares.  
19   * 
20   * @author Jimmy
21   */
22  public abstract class AgentState implements IAgentState, Serializable {
23  	
24  	private String description;
25  
26  	public AgentState(String description) {
27  		this.description = description;
28  	}
29  	
30  	@Override
31  	public boolean isState(Class... states) {
32  		for (Class state : states) {
33  			if (state.isAssignableFrom(this.getClass())) return true;
34  		}
35  		return false;
36  	}
37  	
38  	@Override
39  	public boolean isNotState(Class... states) {
40  		for (Class state : states) {
41  			if (state.isAssignableFrom(this.getClass())) return false;
42  		}
43  		return true;
44  	}
45  	
46  	@Override
47  	public boolean equals(Object o) {
48  		if (!(o instanceof AgentState)) return false; 
49  		AgentState state = (AgentState)o;
50  		return SafeEquals.equals(description, state.description);
51  	}
52  
53  	public String getDescription() {
54  		return description;
55  	}
56  	
57  	@Override
58  	public String toString() {
59  		if (this == null) return "AgentState-instantiating";
60  		return getClass().getSimpleName()+"[" + description + "]";
61  	}
62  	
63  }