KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > state > Result


1 package org.sapia.soto.state;
2
3
4 /**
5  * An instance of this class results from the execution of a given state.
6  *
7  * @see org.sapia.soto.state.StateMachine#execute(String, Context)
8  *
9  * @author Yanick Duchesne
10  * <dl>
11  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
12  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
13  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
14  * </dl>
15  */

16 public class Result {
17   private String JavaDoc _nextState;
18   private String JavaDoc _currentState;
19   private boolean _errorHandled;
20   private boolean _aborted;
21   private Err _err;
22   private Context _ctx;
23   private StateMachine _stm;
24
25   public Result(StateMachine engine, Context ctx) {
26     _ctx = ctx;
27     _stm = engine;
28   }
29
30   /**
31    * @return the identifier of the next state to execute, or <code>null</code>
32    * if there is no other state to execute.
33    */

34   public String JavaDoc getNextStateId() {
35     return _nextState;
36   }
37
38   /**
39    * @param id sets the identifier of the next state to execute.
40    * @see #getNextStateId()
41    */

42   public void setNextStateId(String JavaDoc id) {
43     _nextState = id;
44   }
45
46   /**
47    * Indicates that state execution should be stopped. This method
48    * should be called when no error has been generated, but state
49    * execution must still be stopped.
50    */

51   public void abort() {
52     _aborted = true;
53   }
54
55   /**
56    * @return <code>true</code> if the abort() method has been called
57    * on this instance.
58    *
59    * @see #abort()
60    */

61   public boolean isAborted() {
62     return _aborted;
63   }
64
65   /**
66    * @return <code>true</code> if this instance holds an <code>Err</code> instance.
67    */

68   public boolean isError() {
69     return _err != null;
70   }
71
72   /**
73    * @return the <code>Context</code> in which this instance has been created.
74    */

75   public Context getContext() {
76     return _ctx;
77   }
78
79   /**
80    * Signals that an error has occured.
81    * @param msg an error message.
82    */

83   public void error(String JavaDoc msg) {
84     _err = new Err(msg);
85   }
86
87   /**
88    * Signals that an error has occured.
89    * @param err an <code>Err</code> instance.
90    */

91   public void error(Err err) {
92     _err = err;
93   }
94
95   /**
96    * @param id sets the ID of the current state. Applications should
97    * not deal with this method.
98    */

99   public void setCurrentStateId(String JavaDoc id) {
100     _currentState = id;
101   }
102
103   /**
104    * @return the identifier of the currently executing state.
105    */

106   public String JavaDoc getCurrentStateId() {
107     return _currentState;
108   }
109
110   /**
111    * Signals that an error has occurred.
112    * @param msg an error message.
113    * @param err a <code>Throwable</code> instance.
114    */

115   public void error(String JavaDoc msg, Throwable JavaDoc err) {
116     _err = new Err(msg, err);
117   }
118
119   /**
120    * @param err a <code>Throwable</code>.
121    */

122   public void error(Throwable JavaDoc err) {
123     _err = new Err(err);
124   }
125
126   /**
127    * @return the <code>Err</code> object that contains information about the
128    * error that occurred.
129    *
130    * @throws IllegalStateException if no error occurred.
131    *
132    * @see #isError()
133    */

134   public Err handleError() throws IllegalStateException JavaDoc {
135     _errorHandled = true;
136
137     return _err;
138   }
139
140   /**
141    * @return <code>true</code> if the error that this instance contains (if any)
142    * was handled.
143    *
144    * @see #isError()
145    * @see #handleError()
146    */

147   public boolean isErrorHandled() {
148     return _errorHandled;
149   }
150
151   /**
152    * Executes the state whose ID is given.
153    *
154    * @param id the identifier of a state.
155    * @throws UnknownStateException if no state exists for the given identifier.
156    */

157   public void exec(String JavaDoc stateId, String JavaDoc module)
158     throws UnknownStateException, StateExecException {
159     _stm.execute(stateId, module, this);
160   }
161
162   /**
163    * Sets the next state identifier to null.
164    */

165   void reset() {
166     _nextState = null;
167   }
168
169   /**
170    * @return an <code>Err</code> instance, or <code>null</code> if this state
171    * holds to <code>Err</code> instance.
172    */

173   Err getError() {
174     return _err;
175   }
176 }
177
Popular Tags