View Javadoc

1   package cz.cuni.amis.pogamut.base.agent.navigation.impl;
2   
3   import cz.cuni.amis.pogamut.base.agent.navigation.IPathExecutorState;
4   import cz.cuni.amis.pogamut.base.agent.navigation.PathExecutorState;
5   import cz.cuni.amis.utils.flag.Flag;
6   
7   /**
8    * Represents simple implementation of the {@link IPathExecutorState} containing just the state.
9    * <p><p>
10   * Do not instantiated your own objects - use preinstantiated objects (constants), e.g., {@link BasePathExecutor#INSTANTIATED}, etc. 
11   * (The only exception is the {@link PathExecutorState#SWITCHED_TO_ANOTHER_PATH_ELEMENT} state where you always have to instantiate
12   * this class to reraise the same event again.)
13   * <p><p>
14   * Use {@link BasePathExecutorState#getState(PathExecutorState)} to translate {@link PathExecutorState} into {@link BasePathExecutorState}.
15   * @author Jimmy
16   */
17  public class BasePathExecutorState implements IPathExecutorState {
18  
19  	/**
20  	 * Corresponds to the {@link PathExecutorState#INSTANTIATED} state.
21  	 */
22  	public static final BasePathExecutorState INSTANTIATED = new BasePathExecutorState(PathExecutorState.INSTANTIATED);
23  	
24  	/**
25  	 * Corresponds to the {@link PathExecutorState#FOLLOW_PATH_CALLED} state.
26  	 */
27  	public static final BasePathExecutorState FOLLOW_PATH_CALLED = new BasePathExecutorState(PathExecutorState.FOLLOW_PATH_CALLED);
28  	
29  	/**
30  	 * Corresponds to the {@link PathExecutorState#PATH_COMPUTED} state.
31  	 */
32  	public static final BasePathExecutorState PATH_COMPUTED = new BasePathExecutorState(PathExecutorState.PATH_COMPUTED);
33  	
34  	/**
35  	 * Corresponds to the {@link PathExecutorState#PATH_COMPUTATION_FAILED} state.
36  	 */
37  	public static final BasePathExecutorState PATH_COMPUTATION_FAILED = new BasePathExecutorState(PathExecutorState.PATH_COMPUTATION_FAILED);
38  	
39  	/**
40  	 * Corresponds to the {@link PathExecutorState#SWITCHED_TO_ANOTHER_PATH_ELEMENT} state.
41  	 */
42  	public static final BasePathExecutorState SWITCHED_TO_ANOTHER_PATH_ELEMENT = new BasePathExecutorState(PathExecutorState.SWITCHED_TO_ANOTHER_PATH_ELEMENT);
43  	
44  	/**
45  	 * Corresponds to the {@link PathExecutorState#TARGET_REACHED} state.
46  	 */
47  	public static final BasePathExecutorState TARGET_REACHED = new BasePathExecutorState(PathExecutorState.TARGET_REACHED);
48  	
49  	/**
50  	 * Corresponds to the {@link PathExecutorState#STUCK} state.
51  	 */
52  	public static final BasePathExecutorState STUCK = new BasePathExecutorState(PathExecutorState.STUCK);
53  	
54  	/**
55  	 * Corresponds to the {@link PathExecutorState#STOPPED} state.
56  	 */
57  	public static final BasePathExecutorState STOPPED = new BasePathExecutorState(PathExecutorState.STOPPED);
58  	
59  	public static BasePathExecutorState getState(PathExecutorState state) {
60  		switch(state) {
61  		case INSTANTIATED:                     return BasePathExecutorState.INSTANTIATED;
62  		case FOLLOW_PATH_CALLED:               return BasePathExecutorState.FOLLOW_PATH_CALLED;
63  		case PATH_COMPUTED:                    return BasePathExecutorState.PATH_COMPUTED;
64  		case PATH_COMPUTATION_FAILED:          return BasePathExecutorState.PATH_COMPUTATION_FAILED;
65  		case SWITCHED_TO_ANOTHER_PATH_ELEMENT: return BasePathExecutorState.SWITCHED_TO_ANOTHER_PATH_ELEMENT;
66  		case TARGET_REACHED:                   return BasePathExecutorState.TARGET_REACHED;
67  		case STUCK:                            return BasePathExecutorState.STUCK;
68  		case STOPPED:                          return BasePathExecutorState.STOPPED;
69  		default:
70  			throw new RuntimeException("Unhandled state '" + state + "', can't create corresponding BasePathExecutorState.");
71  		}
72  	}
73  	
74  	/**
75  	 * State of the path executor.
76  	 */
77  	private PathExecutorState state;
78  	
79  	/**
80  	 * Instantiated the path executor with the given state.
81  	 * <p><p>
82  	 * This constructor is meant to be used only by {@link BasePathExecutor} descendants! If you want to use it,
83  	 * it is advised to use predefined constants. (The only exception is the {@link PathExecutorState#SWITCHED_TO_ANOTHER_PATH_ELEMENT} state.)
84  	 * @param state
85  	 */
86  	public BasePathExecutorState(PathExecutorState state) {
87  		this.state = state;
88  	}
89  	
90  	@Override
91  	public PathExecutorState getState() {
92  		return state;
93  	}
94  
95  }