KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > mbtf > v1 > engine > TransitionImpl


1 /*
2  * @(#)TransitionImpl.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.engine;
30
31
32 import net.sourceforge.groboutils.mbtf.v1.IState;
33 import net.sourceforge.groboutils.mbtf.v1.IAction;
34 import net.sourceforge.groboutils.mbtf.v1.IValidate;
35 import net.sourceforge.groboutils.mbtf.v1.ITransition;
36
37
38 /**
39  * Nearly-Immutable implementation of ITransition. As an immutable, transitions
40  * cannot be created if there are cycles in the state machine. Therefore,
41  * there needs to be a way to post-creation correctly populate the Transition's
42  * destination state. Thus, the destination state may be set once.
43  *
44  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
45  * @version $Date: 2003/02/10 22:52:26 $
46  * @since June 12, 2002
47  */

48 public class TransitionImpl implements ITransition
49 {
50     private String JavaDoc name;
51     private IState destination;
52     private IAction action;
53     private IValidate[] validate;
54     
55     
56     public TransitionImpl( String JavaDoc name, IState dest, IAction a, IValidate[] v )
57     {
58         // allow destination to be null
59

60         
61         if (name == null || a == null)
62         {
63             throw new IllegalArgumentException JavaDoc("no null args");
64         }
65         
66         if (v == null)
67         {
68             v = new IValidate[0];
69         }
70         else
71         {
72             int len = v.length;
73             IValidate[] vv = new IValidate[ len ];
74             for (int i = len; --i >= 0;)
75             {
76                 if (v[i] == null)
77                 {
78                     throw new IllegalArgumentException JavaDoc(
79                         "no nulls allowed in IValidate array");
80                 }
81                 // else
82
vv[i] = v[i];
83             }
84             v = vv;
85         }
86         
87         this.name = name;
88         setDestinationState( dest );
89         this.action = a;
90         this.validate = v;
91     }
92     
93     
94     /**
95      * Retrieves the name for the transition. This should be unique for
96      * state-machine assembly purposes, but it does not have to be.
97      *
98      * @return a non-null name for this transition.
99      */

100     public String JavaDoc getName()
101     {
102         return this.name;
103     }
104     
105     
106     /**
107      * Returns the next state if the corresponding action is executed.
108      *
109      * @return destination state, which can never be <tt>null</tt>.
110      */

111     public IState getDestinationState()
112     {
113         if (isDestinationStateSet())
114         {
115             return this.destination;
116         }
117         // else
118
throw new IllegalStateException JavaDoc("Destination was never set.");
119     }
120     
121     
122     /**
123      * Returns the action used to transition to the destination state.
124      *
125      * @return transition's action, which can never be <tt>null</tt>.
126      */

127     public IAction getAction()
128     {
129         return this.action;
130     }
131     
132     
133     /**
134      * Returns all validation methods used to assert that the system can
135      * perform this transition.
136      *
137      * @return a list of associated validation instances.
138      */

139     public IValidate[] getValidates()
140     {
141         int len = this.validate.length;
142         IValidate v[] = new IValidate[ len ];
143         System.arraycopy( this.validate, 0, v, 0, len );
144         return v;
145     }
146     
147     //-------------------------------------------------------------------------
148

149     /**
150      * Allows for post-creation setting of the destination. The destination
151      * may only be set to a non-<tt>null</tt> value once. If there is an
152      * attempt to pull the destination through <tt>getDestinationState()</tt>
153      * and it has not been set yet, then an IllegalStateException will
154      * be thrown.
155      */

156     public void setDestinationState( IState dest )
157     {
158         if (isDestinationStateSet())
159         {
160             throw new IllegalStateException JavaDoc(
161                 "Destination has already been set.");
162         }
163         else
164         {
165             this.destination = dest;
166         }
167     }
168     
169     
170     /**
171      * Allows for a builder system to detect if the destination state has
172      * been set or not, without causing the IllegalStateException through
173      * the <tt>getDestinationState()</tt>.
174      */

175     public boolean isDestinationStateSet()
176     {
177         return (this.destination != null);
178     }
179     
180     
181     public String JavaDoc toString()
182     {
183         return "[Transition "+getName()+"]";
184     }
185 }
186
187
Popular Tags