KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > mbtf > v1 > assembler > AsmblTransition


1 /*
2  * @(#)AsmblTransition.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Part of the GroboUtils package at:
9  * http://groboutils.sourceforge.net
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */

29 package net.sourceforge.groboutils.mbtf.v1.assembler;
30
31
32 import net.sourceforge.groboutils.mbtf.v1.IState;
33 import net.sourceforge.groboutils.mbtf.v1.ITransition;
34 import net.sourceforge.groboutils.mbtf.v1.IValidate;
35 import net.sourceforge.groboutils.mbtf.v1.IAction;
36
37 import net.sourceforge.groboutils.mbtf.v1.engine.TransitionImpl;
38
39 import java.util.Vector JavaDoc;
40
41
42 /**
43  *
44  *
45  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
46  * @version $Date: 2003/02/10 22:52:25 $
47  * @since June 12, 2002
48  */

49 public class AsmblTransition
50 {
51     private String JavaDoc name;
52     private Vector JavaDoc validates = new Vector JavaDoc();
53     private IAction action;
54     private String JavaDoc nextState;
55     
56     private TransitionImpl iTrans;
57     
58     
59     /**
60      *
61      */

62     public AsmblTransition()
63     {
64         // do nothing
65
}
66     
67     
68     public void setName( String JavaDoc name )
69     {
70         if (name == null)
71         {
72             throw new IllegalArgumentException JavaDoc("no null args");
73         }
74         
75         // invalidates our transition
76
resetTransition( true );
77         
78         this.name = name;
79     }
80     
81     
82     public String JavaDoc getName()
83     {
84         return this.name;
85     }
86     
87     
88     public void setNextStateName( String JavaDoc name )
89     {
90         if (name == null)
91         {
92             throw new IllegalArgumentException JavaDoc("no null args");
93         }
94         
95         // invalidates our transition, and its state (if any)
96
resetTransition( false );
97         
98         this.nextState = name;
99     }
100     
101     
102     public String JavaDoc getNextStateName()
103     {
104         return this.nextState;
105     }
106     
107     
108     public void addValidate( IValidate v )
109     {
110         if (v != null)
111         {
112             // invalidates our transition
113
resetTransition( true );
114
115             this.validates.addElement( v );
116         }
117     }
118     
119     
120     public void setAction( IAction a )
121     {
122         if (a == null)
123         {
124             throw new IllegalArgumentException JavaDoc("no null args");
125         }
126         
127         // invalidates our transition
128
resetTransition( true );
129         
130         this.action = a;
131     }
132     
133     
134     public IValidate[] getValidates()
135     {
136         IValidate v[] = new IValidate[ this.validates.size() ];
137         this.validates.copyInto( v );
138         return v;
139     }
140     
141     
142     public IAction getAction()
143     {
144         return this.action;
145     }
146     
147     
148     public ITransition getTransition()
149     {
150         if (this.iTrans == null)
151         {
152             this.iTrans = new TransitionImpl( getName(), null, getAction(),
153                 getValidates() );
154         }
155         return this.iTrans;
156     }
157     
158     
159     public void setDestinationState( IState state )
160     {
161         // ensure the transition is created...
162
getTransition();
163         
164         // make sure the state is correct
165
if (state == null)
166         {
167             throw new IllegalArgumentException JavaDoc("no null args");
168         }
169         if (!state.getName().equals( getNextStateName() ))
170         {
171             throw new IllegalArgumentException JavaDoc("Invalid state name");
172         }
173         
174         // only set the destination if we can.
175
if (!this.iTrans.isDestinationStateSet())
176         {
177             this.iTrans.setDestinationState( state );
178         }
179     }
180     
181     
182     protected void resetTransition( boolean saveState )
183     {
184         if (saveState && this.iTrans != null &&
185             this.iTrans.isDestinationStateSet())
186         {
187             throw new IllegalStateException JavaDoc("Cannot reset transition");
188         }
189         else
190         {
191             this.iTrans = null;
192         }
193     }
194 }
195
196
Popular Tags