KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)StateImpl.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.IValidate;
34 import net.sourceforge.groboutils.mbtf.v1.ITransition;
35
36
37 /**
38  * Immutable IState implementation.
39  *
40  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
41  * @version $Date: 2003/02/10 22:52:26 $
42  * @since June 12, 2002
43  */

44 public class StateImpl implements IState
45 {
46     private String JavaDoc name;
47     private ITransition[] trans;
48     private IValidate[] validate;
49     
50     
51     public StateImpl( String JavaDoc name, ITransition[] t, IValidate[] v )
52     {
53         if (name == null)
54         {
55             throw new IllegalArgumentException JavaDoc("no null name");
56         }
57         
58         if (t == null)
59         {
60             t = new ITransition[0];
61         }
62         else
63         {
64             int len = t.length;
65             ITransition[] tt = new ITransition[ len ];
66             for (int i = len; --i >= 0;)
67             {
68                 if (t[i] == null)
69                 {
70                     throw new IllegalArgumentException JavaDoc(
71                         "no nulls allowed in ITransition array (index "+i+")");
72                 }
73                 // else
74
tt[i] = t[i];
75             }
76             t = tt;
77         }
78         
79         if (v == null)
80         {
81             v = new IValidate[0];
82         }
83         else
84         {
85             int len = v.length;
86             IValidate[] vv = new IValidate[ len ];
87             for (int i = len; --i >= 0;)
88             {
89                 if (v[i] == null)
90                 {
91                     throw new IllegalArgumentException JavaDoc(
92                         "no nulls allowed in IValidate array (index "+i+")");
93                 }
94                 // else
95
vv[i] = v[i];
96             }
97             v = vv;
98         }
99         
100         this.name = name;
101         this.trans = t;
102         this.validate = v;
103     }
104
105
106     /**
107      * Retrieves the name for the state. This should be unique for
108      * state-machine assembly purposes, but it does not have to be.
109      *
110      * @return a non-null name for this state.
111      */

112     public String JavaDoc getName()
113     {
114         return this.name;
115     }
116     
117     
118     /**
119      * Retrieves the list of transitions leading from this state.
120      *
121      * @return a non-null list of all transitions from this state. If the
122      * length of the list is 0, then this is a terminal state.
123      */

124     public ITransition[] getTransitions()
125     {
126         int len = this.trans.length;
127         ITransition t[] = new ITransition[ len ];
128         System.arraycopy( this.trans, 0, t, 0, len );
129         return t;
130     }
131     
132     
133     /**
134      * Retrieves the list of all validation instances used to assert that
135      * the current state is valid.
136      *
137      * @return a non-null list of all validation instances for this state.
138      * An empty list will cause a warning on all but the first
139      * (initial) state. This list should not include the included
140      * Transitions' validates.
141      */

142     public IValidate[] getValidates()
143     {
144         int len = this.validate.length;
145         IValidate v[] = new IValidate[ len ];
146         System.arraycopy( this.validate, 0, v, 0, len );
147         return v;
148     }
149     
150     
151     public String JavaDoc toString()
152     {
153         return "[State "+getName()+"]";
154     }
155 }
156
157
Popular Tags