KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)PathImpl.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.IPath;
33 import net.sourceforge.groboutils.mbtf.v1.IState;
34 import net.sourceforge.groboutils.mbtf.v1.ITransition;
35 import net.sourceforge.groboutils.mbtf.v1.IPathIterator;
36
37 import org.apache.log4j.Logger;
38
39
40 /**
41  * An immutable implementation of IPath
42  *
43  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
44  * @version $Date: 2003/02/10 22:52:26 $
45  * @since June 12, 2002
46  */

47 public class PathImpl implements IPath
48 {
49     private static final Logger LOG = Logger.getLogger( PathImpl.class );
50     
51     private ITransition[] trans;
52     private IState start;
53     
54     
55     public PathImpl( IState startState, ITransition[] trans )
56     {
57         if (startState == null)
58         {
59             throw new IllegalArgumentException JavaDoc("no null args");
60         }
61         
62         LOG.debug("Path starting at "+startState);
63         if (trans == null)
64         {
65             LOG.debug("-- Path has no transitions");
66             this.trans = new ITransition[0];
67         }
68         else
69         {
70             int len = trans.length;
71             ITransition[] t = new ITransition[ len ];
72             for (int i = 0; i < len; ++i)
73             {
74                 if (trans[i] == null)
75                 {
76                     throw new IllegalArgumentException JavaDoc(
77                         "no nulls allowed in ITransition array");
78                 }
79                 // else
80
LOG.debug("-- transition "+i+"="+trans[i]);
81                 t[i] = trans[i];
82             }
83             this.trans = t;
84         }
85         
86         this.start = startState;
87     }
88     
89     
90     /**
91      * Generate an iterator for this path. The iterator will only return
92      * transition elements.
93      *
94      * @return a new iterator for the path.
95      */

96     public IPathIterator iterator()
97     {
98         return new PathIteratorImpl( this.trans );
99     }
100     
101     
102     /**
103      * Retrieve the starting state for this path.
104      *
105      * @return the start state for the path, which can never be <tt>null</tt>.
106      */

107     public IState getStartState()
108     {
109         return this.start;
110     }
111     
112     
113     /**
114      * Returns the number of transitions in the path. This is named "size" to
115      * correspond to the <tt>java.util</tt>
116      * container classes terminology.
117      *
118      * @return the path transition count.
119      */

120     public int size()
121     {
122         return this.trans.length;
123     }
124     
125     
126     /**
127      * Returns the number of states visited in the path, which should
128      * always equal <tt>size() + 2</tt>, due to the start and final states,
129      * unless there are no transitions, in which case the depth is 1
130      * (there must always be a start state).
131      *
132      * @return the depth of the path.
133      */

134     public int getDepth()
135     {
136         int size = size();
137         int depth;
138         if (size == 0)
139         {
140             depth = 1;
141         }
142         else
143         {
144             depth = size + 2;
145         }
146         return depth;
147     }
148 }
149
150
Popular Tags