KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > automaton > MinimalDfaState


1 /* This file is part of SableCC ( http://sablecc.org ).
2  *
3  * Copyright 2007 Etienne M. Gagnon <egagnon@j-meg.com>
4  * Copyright 2007 Patrick Pelletier <pp.pelletier@gmail.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.sablecc.sablecc.automaton;
20
21 import java.util.Collections JavaDoc;
22 import java.util.SortedMap JavaDoc;
23 import java.util.TreeMap JavaDoc;
24
25 import org.sablecc.sablecc.alphabet.Symbol;
26 import org.sablecc.sablecc.exception.InternalException;
27
28 /**
29  * A <code>MinimalDfaState<code> is a minimalist state.
30  */

31 public final class MinimalDfaState<T extends Comparable JavaDoc<? super T>>
32         implements Comparable JavaDoc<MinimalDfaState<T>> {
33
34     /** The <code>MinimalDfa<code> of this <code>MinimalDfaState<code>. */
35     private final MinimalDfa<T> minimalDfa;
36
37     /** The identification number of this <code>MinimalDfaState<code>. */
38     private final int id;
39
40     /**
41      * A <code>SortedMap</code> that maps each symbol to its corresponding
42      * <code>MinimalDfaState<code>. Represents the transitions.
43      */

44     private SortedMap JavaDoc<Symbol<T>, MinimalDfaState<T>> transitions;
45
46     /** A stability status for this <code>MinimalDfaState<code>. */
47     private boolean isStable;
48
49     /**
50      * Cached string representation. Is <code>null</code> when not yet
51      * computed.
52      */

53     private String JavaDoc toString;
54
55     /**
56      * Constructs a <code>MinimalDfaState<code> with the provided
57      * <code>MinimalDfa</code>.
58      *
59      * @param minimalDfa
60      * the <code>MinimalDfa<code>.
61      * @throws InternalException
62      * if the provided <code>MinimalDfa<code> is <code>null</code>.
63      */

64     MinimalDfaState(
65             MinimalDfa<T> minimalDfa) {
66
67         if (minimalDfa == null) {
68             throw new InternalException("minimalDfa may not be null");
69         }
70
71         this.minimalDfa = minimalDfa;
72
73         this.id = minimalDfa.getNextStateId();
74         minimalDfa.addState(this);
75
76         this.transitions = new TreeMap JavaDoc<Symbol<T>, MinimalDfaState<T>>();
77
78         this.isStable = false;
79     }
80
81     /**
82      * Returns the <code>MinimalDfa<code> of this <code>MinimalDfaState<code>.
83      *
84      * @return the <code>MinimalDfa<code>.
85      */

86     public MinimalDfa<T> getMinimalDfa() {
87
88         return this.minimalDfa;
89     }
90
91     /**
92      * Returns the identification number of this <code>MinimalDfaState<code>.
93      *
94      * @return the identification number.
95      */

96     public int getId() {
97
98         return this.id;
99     }
100
101     /**
102      * Returns a set of the transitions of this <code>MinimalDfaState<code>.
103      *
104      * @return the <code>SortedMap</code> of the transitions.
105      * @throws InternalException
106      * if this <code>MinimalDfaState<code> is not stable.
107      */

108     public SortedMap JavaDoc<Symbol<T>, MinimalDfaState<T>> getTransitions() {
109
110         if (!this.isStable) {
111             throw new InternalException("the state is not stable yet");
112         }
113
114         return this.transitions;
115     }
116
117     /**
118      * Returns a <code>MinimalDfaState<code> representing
119      * the target of the provided symbol.
120      *
121      * @param symbol the symbol.
122      * @return the target <code>MinimalDfaState<code>.
123      * @throws InternalException
124      * if this instance is stable,
125      * if the provided symbol is <code>null</code> or
126      * if this instance's <code>MinimalDfa</code> contains the symbol.
127      */

128     public MinimalDfaState<T> getTarget(
129             Symbol<T> symbol) {
130
131         if (!this.isStable) {
132             throw new InternalException("the state is not stable yet");
133         }
134
135         if (symbol == null) {
136             throw new InternalException("symbol may not be null");
137         }
138
139         if (!this.minimalDfa.getAlphabet().getSymbols().contains(symbol)) {
140             throw new InternalException("invalid symbol");
141         }
142
143         MinimalDfaState<T> target = this.transitions.get(symbol);
144
145         if (target == null) {
146             target = this.minimalDfa.getDeadEndState();
147         }
148
149         return target;
150     }
151
152     /**
153      * Returns whether this instance is equal to the provided object. They are
154      * equal if they have equal IDs
155      *
156      * @param obj
157      * the object to compare with.
158      * @return <code>true</code> if this
159      * <code>MinimalDfaState<code> and the object are equal;
160      * <code>false</code> otherwise.
161      */

162     @Override JavaDoc
163     public boolean equals(
164             Object JavaDoc obj) {
165
166         if (this == obj) {
167             return true;
168         }
169
170         if (obj == null) {
171             return false;
172         }
173
174         if (getClass() != obj.getClass()) {
175             return false;
176         }
177
178         MinimalDfaState nfaState = (MinimalDfaState) obj;
179
180         return this.id == nfaState.id;
181     }
182
183     /**
184      * Returns the hash code of this <code>MinimalDfaState<code>.
185      *
186      * @return the hash code.
187      */

188     @Override JavaDoc
189     public int hashCode() {
190
191         return this.id;
192     }
193
194     /**
195      * Returns the string representation of this <code>MinimalDfaState<code>.
196      *
197      * @return the string representation.
198      */

199     @Override JavaDoc
200     public String JavaDoc toString() {
201
202         if (this.toString == null) {
203             this.toString = "dfaState" + this.id;
204         }
205
206         return this.toString;
207     }
208
209     /**
210      * Compares this <code>MinimalDfaState</code> to the provided one. It
211      * compares the identification number.
212      *
213      * @param minimalDfaState
214      * the <code>minimalDfaState</code> to compare with.
215      * @return an <code>int</code> value: 0 if the two instances are equals, a
216      * negative value if this <code>MinimalDfaState<code> is
217      * smaller, and a positive value if it is bigger.
218      * @throws InternalException
219      * if the provided <code>MinimalDfaState<code> is not in
220      * the same <code>MinimalDfa</code> as this one.
221      */

222     public int compareTo(
223             MinimalDfaState<T> minimalDfaState) {
224
225         if (this.minimalDfa != minimalDfaState.minimalDfa) {
226             throw new InternalException(
227                     "cannot compare states from distinct MinimalDFAs");
228         }
229
230         return this.id - minimalDfaState.id;
231     }
232
233     /**
234      * Adds a transition to this <code>MinimalDfaState<code>
235      * with the provided symbol and <code>MinimalDfaState<code>.
236      *
237      * @param symbol the symbol.
238      * @param minimalDfaState the <code>MinimalDfaState<code>.
239      * @throws InternalException
240      * if this <code>MinimalDfaState<code> is stable,
241      * if the provided symbol or <code>MinimalDfaState<code>
242      * is <code>null</code>, if this instance's <code>MinimalDfa</code>
243      * already contains the provided symbol or
244      * if it is not equal to the provided <code>MinimalDfaState</code>'s
245      * <code>MinimalDfa</code> or if the target is already set.
246      */

247     void addTransition(
248             Symbol<T> symbol,
249             MinimalDfaState<T> minimalDfaState) {
250
251         if (this.isStable) {
252             throw new InternalException("a stable state may not be modified");
253         }
254
255         if (symbol == null) {
256             throw new InternalException("symbol may not be null");
257         }
258
259         if (minimalDfaState == null) {
260             throw new InternalException("minimalDfaState may not be null");
261         }
262
263         if (!this.minimalDfa.getAlphabet().getSymbols().contains(symbol)) {
264             throw new InternalException("invalid symbol");
265         }
266
267         if (this.minimalDfa != minimalDfaState.minimalDfa) {
268             throw new InternalException("invalid dfaState");
269         }
270
271         if (minimalDfaState == this.minimalDfa.getUnstableDeadEndState()) {
272             // Don't add transition to dead-end state
273
if (this.transitions.get(symbol) != null) {
274                 throw new InternalException("target was already set");
275             }
276         }
277         else if (this.transitions.put(symbol, minimalDfaState) != null) {
278             throw new InternalException("target was already set");
279         }
280     }
281
282     /**
283      * Stabilizes this <code>MinimalDfaState<code>.
284      *
285      * @throws InternalException
286      * if this <code>MinimalDfaState<code> is already stable.
287      */

288     void stabilize() {
289
290         if (this.isStable) {
291             throw new InternalException("state is already stable");
292         }
293
294         this.transitions = Collections.unmodifiableSortedMap(this.transitions);
295
296         this.isStable = true;
297     }
298 }
299
Popular Tags