KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > alphabet > AlphabetMergeResult


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.alphabet;
20
21 import java.util.SortedMap JavaDoc;
22 import java.util.SortedSet JavaDoc;
23 import java.util.TreeSet JavaDoc;
24
25 import org.sablecc.sablecc.exception.InternalException;
26
27 /**
28  * An instance of this class encapsulates the result of merging two alphabets.
29  * It allows for retrieving the new alphabet and for mapping old symbols to sets
30  * of new symbols.
31  */

32 public final class AlphabetMergeResult<T extends Comparable JavaDoc<? super T>> {
33
34     /** The new alphabet. */
35     private final Alphabet<T> newAlphabet;
36
37     /** The first merged alphabet. */
38     private final Alphabet<T> mergedAlphabet1;
39
40     /**
41      * The symbol map of the first alphabet. It maps each old symbol to a set of
42      * new symbols that cover the same intervals.
43      */

44     private final SortedMap JavaDoc<Symbol<T>, SortedSet JavaDoc<Symbol<T>>> mergedAlphabet1SymbolMap;
45
46     /** The second merged alphabet. */
47     private final Alphabet<T> mergedAlphabet2;
48
49     /**
50      * The symbol map of the second alphabet. It maps each old symbol to a set
51      * of new symbols that cover the same intervals.
52      */

53     private final SortedMap JavaDoc<Symbol<T>, SortedSet JavaDoc<Symbol<T>>> mergedAlphabet2SymbolMap;
54
55     /**
56      * Constructs a new instance for the result of merging an alphabet with
57      * itself.
58      *
59      * @param alphabet
60      * the alphabet.
61      * @throws InternalException
62      * if the alphabet is <code>null</code>.
63      */

64     AlphabetMergeResult(
65             Alphabet<T> alphabet) {
66
67         if (alphabet == null) {
68             throw new InternalException("alphabet may not be null");
69         }
70
71         this.newAlphabet = alphabet;
72
73         this.mergedAlphabet1 = null;
74         this.mergedAlphabet1SymbolMap = null;
75
76         this.mergedAlphabet2 = null;
77         this.mergedAlphabet2SymbolMap = null;
78     }
79
80     /**
81      * Constructs a new instance for the result of merging two distinct
82      * alphabets.
83      *
84      * @param newAlphabet
85      * the new alphabet.
86      * @param mergedAlphabet1
87      * the first merged alphabet.
88      * @param mergedAlphabet1SymbolMap
89      * the symbol map of the first alphabet.
90      * @param mergedAlphabet2
91      * the second merged alphabet.
92      * @param mergedAlphabet2SymbolMap
93      * the symbol map of the second alphabet.
94      * @throws InternalException
95      * if any parameter is <code>null</code>, or if the merged
96      * alphabets are not distinct.
97      */

98     AlphabetMergeResult(
99             Alphabet<T> newAlphabet,
100             Alphabet<T> mergedAlphabet1,
101             SortedMap JavaDoc<Symbol<T>, SortedSet JavaDoc<Symbol<T>>> mergedAlphabet1SymbolMap,
102             Alphabet<T> mergedAlphabet2,
103             SortedMap JavaDoc<Symbol<T>, SortedSet JavaDoc<Symbol<T>>> mergedAlphabet2SymbolMap) {
104
105         if (newAlphabet == null) {
106             throw new InternalException("newAlphabet may not be null");
107         }
108
109         if (mergedAlphabet1 == null) {
110             throw new InternalException("mergedAlphabet1 may not be null");
111         }
112
113         if (mergedAlphabet1SymbolMap == null) {
114             throw new InternalException(
115                     "mergedAlphabet1SymbolMap may not be null");
116         }
117
118         for (Symbol<T> oldSymbol : mergedAlphabet1.getSymbols()) {
119             if (mergedAlphabet1SymbolMap.get(oldSymbol) == null) {
120                 throw new InternalException(
121                         "mergedAlphabet1SymbolMap is invalid");
122             }
123         }
124
125         if (mergedAlphabet2 == null) {
126             throw new InternalException("mergedAlphabet2 may not be null");
127         }
128
129         if (mergedAlphabet2SymbolMap == null) {
130             throw new InternalException(
131                     "mergedAlphabet2SymbolMap may not be null");
132         }
133
134         if (mergedAlphabet1 == mergedAlphabet2) {
135             throw new InternalException("wrong constructor");
136         }
137
138         for (Symbol<T> oldSymbol : mergedAlphabet2.getSymbols()) {
139             if (mergedAlphabet2SymbolMap.get(oldSymbol) == null) {
140                 throw new InternalException(
141                         "mergedAlphabet2SymbolMap is invalid");
142             }
143         }
144
145         this.newAlphabet = newAlphabet;
146
147         this.mergedAlphabet1 = mergedAlphabet1;
148         this.mergedAlphabet1SymbolMap = mergedAlphabet1SymbolMap;
149
150         this.mergedAlphabet2 = mergedAlphabet2;
151         this.mergedAlphabet2SymbolMap = mergedAlphabet2SymbolMap;
152     }
153
154     /**
155      * Returns the new alphabet resulting from the merge.
156      *
157      * @return the new alphabet.
158      */

159     public Alphabet<T> getNewAlphabet() {
160
161         return this.newAlphabet;
162     }
163
164     /**
165      * Returns the set of new symbols covering the same intervals as the
166      * provided old symbol from the provided merged alphabet.
167      *
168      * @param oldSymbol
169      * the symbol.
170      * @param mergedAlphabet
171      * the alphabet.
172      * @return the set of new symbols.
173      * @throws InternalException
174      * if the old symbol or the old alphabet is <code>null</code>.
175      */

176     public SortedSet JavaDoc<Symbol<T>> getNewSymbols(
177             Symbol<T> oldSymbol,
178             Alphabet<T> mergedAlphabet) {
179
180         if (oldSymbol == null) {
181             throw new InternalException("oldSymbol may not be null");
182         }
183
184         if (mergedAlphabet == null) {
185             throw new InternalException("mergedAlphabet may not be null");
186         }
187
188         if (!mergedAlphabet.getSymbols().contains(oldSymbol)) {
189             throw new InternalException(
190                     "oldSymbol is not an element of mergedAlphabet");
191         }
192
193         // special case for an alphabet merged with itslef
194
if (this.mergedAlphabet1 == null) {
195             if (mergedAlphabet != this.newAlphabet) {
196                 throw new InternalException("mergedAlphabet is invalid");
197             }
198
199             TreeSet JavaDoc<Symbol<T>> set = new TreeSet JavaDoc<Symbol<T>>();
200             set.add(oldSymbol);
201             return set;
202         }
203
204         if (mergedAlphabet == this.mergedAlphabet1) {
205             return this.mergedAlphabet1SymbolMap.get(oldSymbol);
206         }
207
208         if (mergedAlphabet == this.mergedAlphabet2) {
209             return this.mergedAlphabet2SymbolMap.get(oldSymbol);
210         }
211
212         throw new InternalException("mergedAlphabet is invalid");
213     }
214 }
215
Popular Tags