KickJava   Java API By Example, From Geeks To Geeks.

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


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.SortedMap JavaDoc;
22 import java.util.TreeMap JavaDoc;
23
24 import org.sablecc.sablecc.exception.InternalException;
25
26 /**
27  * An instance of this class encapsulates the result of combining two
28  * <code>Nfa</code>. It allows for retrieving the new <code>Nfa</code> and
29  * for mapping old states to sets of new states.
30  */

31 class NfaCombineResult<T extends Comparable JavaDoc<? super T>> {
32
33     /** The new <code>Nfa</code>. */
34     private Nfa<T> newNfa;
35
36     /** The first combined <code>Nfa</code>. */
37     private Nfa<T> oldNfa1;
38
39     /**
40      * A <code>SortedMap</code> that maps each state contained in the first
41      * old <code>Nfa</code> to its state in this <code>NfaCombineResult</code>.
42      */

43     private SortedMap JavaDoc<NfaState<T>, NfaState<T>> oldNfa1StateMap = new TreeMap JavaDoc<NfaState<T>, NfaState<T>>();
44
45     /** The second combined <code>Nfa</code>. */
46     private Nfa<T> oldNfa2;
47
48     /**
49      * A <code>SortedMap</code> that maps each state contained in the second
50      * old <code>Nfa</code> to its state in this <code>NfaCombineResult</code>.
51      */

52     private SortedMap JavaDoc<NfaState<T>, NfaState<T>> oldNfa2StateMap = new TreeMap JavaDoc<NfaState<T>, NfaState<T>>();
53
54     /**
55      * Constructs a new instance for the result of combining two
56      * <code>Nfa</code> instances.
57      *
58      * @param newNfa
59      * the new <code>Nfa</code>.
60      * @param oldNfa1
61      * the first combined <code>Nfa</code>.
62      * @param oldNfa1StateMap
63      * the states of first <code>Nfa</code>.
64      * @param oldNfa2
65      * the second combined <code>Nfa</code>.
66      * @param oldNfa2StateMap
67      * the states of second <code>Nfa</code>.
68      * @throws InternalException
69      * if one of the <code>Nfa</code> instances, state map or
70      * states is <code>null</code>.
71      */

72     NfaCombineResult(
73             Nfa<T> newNfa,
74             Nfa<T> oldNfa1,
75             SortedMap JavaDoc<NfaState<T>, NfaState<T>> oldNfa1StateMap,
76             Nfa<T> oldNfa2,
77             SortedMap JavaDoc<NfaState<T>, NfaState<T>> oldNfa2StateMap) {
78
79         if (newNfa == null) {
80             throw new InternalException("newNfa may not be null");
81         }
82
83         if (oldNfa1 == null) {
84             throw new InternalException("oldNfa1 may not be null");
85         }
86
87         if (oldNfa1StateMap == null) {
88             throw new InternalException("oldNfa1StateMap may not be null");
89         }
90
91         if (oldNfa2 == null) {
92             throw new InternalException("oldNfa2 may not be null");
93         }
94
95         if (oldNfa2StateMap == null) {
96             throw new InternalException("oldNfa2StateMap may not be null");
97         }
98
99         for (NfaState<T> oldState : oldNfa1.getStates()) {
100             if (oldNfa1StateMap.get(oldState) == null) {
101                 throw new InternalException("invalid oldNfa1StateMap");
102             }
103         }
104
105         for (NfaState<T> oldState : oldNfa2.getStates()) {
106             if (oldNfa2StateMap.get(oldState) == null) {
107                 throw new InternalException("invalid oldNfa2StateMap");
108             }
109         }
110
111         this.newNfa = newNfa;
112         this.oldNfa1 = oldNfa1;
113         this.oldNfa1StateMap = oldNfa1StateMap;
114         this.oldNfa2 = oldNfa2;
115         this.oldNfa2StateMap = oldNfa2StateMap;
116     }
117
118     /**
119      * Returns the new <code>Nfa</code> instance.
120      *
121      * @return the new <code>Nfa</code>.
122      */

123     Nfa<T> getNewNfa() {
124
125         return this.newNfa;
126     }
127
128     /**
129      * Returns the state of this instance corresponding to a provided old state.
130      * The provided old state must have the same <code>Nfa</code> as the first
131      * old state of this instance.
132      *
133      * @param oldState
134      * the old state.
135      * @return the corresponding state.
136      * @throws InternalException
137      * if the <code>oldState</code> is <code>null</code> or
138      * invalid.
139      */

140     NfaState<T> getNewNfa1State(
141             NfaState<T> oldState) {
142
143         if (oldState == null) {
144             throw new InternalException("oldState may not be null");
145         }
146
147         if (oldState.getNfa() != this.oldNfa1) {
148             throw new InternalException("invalid oldState");
149         }
150
151         return this.oldNfa1StateMap.get(oldState);
152     }
153
154     /**
155      * Returns the state of this instance corresponding to a provided old state.
156      * The provided old state must have the same <code>Nfa</code> as the
157      * second old state of this instance.
158      *
159      * @param oldState
160      * the old state.
161      * @return the corresponding state.
162      * @throws InternalException
163      * if the <code>oldState</code> is <code>null</code> or
164      * invalid.
165      */

166     NfaState<T> getNewNfa2State(
167             NfaState<T> oldState) {
168
169         if (oldState == null) {
170             throw new InternalException("oldState may not be null");
171         }
172
173         if (oldState.getNfa() != this.oldNfa2) {
174             throw new InternalException("invalid oldState");
175         }
176
177         return this.oldNfa2StateMap.get(oldState);
178     }
179
180     /**
181      * Returns the start state of the first old state for the new
182      * <code>Nfa</code>.
183      *
184      * @return the first start state.
185      */

186     NfaState<T> getNewNfa1StartState() {
187
188         return this.oldNfa1StateMap.get(this.oldNfa1.getStartState());
189     }
190
191     /**
192      * Returns the start state of the second old state for the new
193      * <code>Nfa</code>.
194      *
195      * @return the second start state.
196      */

197     NfaState<T> getNewNfa2StartState() {
198
199         return this.oldNfa2StateMap.get(this.oldNfa2.getStartState());
200     }
201
202     /**
203      * Returns the acceptation state of the first old state for the new
204      * <code>Nfa</code>.
205      *
206      * @return the first acceptation state.
207      */

208     NfaState<T> getNewNfa1AcceptState() {
209
210         return this.oldNfa1StateMap.get(this.oldNfa1.getAcceptState());
211     }
212
213     /**
214      * Returns the acceptation state of the second old state for the new
215      * <code>Nfa</code>.
216      *
217      * @return the second acceptation state.
218      */

219     NfaState<T> getNewNfa2AcceptState() {
220
221         return this.oldNfa2StateMap.get(this.oldNfa2.getAcceptState());
222     }
223 }
224
Popular Tags