1 18 19 package org.sablecc.sablecc.alphabet; 20 21 import java.util.SortedMap ; 22 import java.util.SortedSet ; 23 import java.util.TreeSet ; 24 25 import org.sablecc.sablecc.exception.InternalException; 26 27 32 public final class AlphabetMergeResult<T extends Comparable <? super T>> { 33 34 35 private final Alphabet<T> newAlphabet; 36 37 38 private final Alphabet<T> mergedAlphabet1; 39 40 44 private final SortedMap <Symbol<T>, SortedSet <Symbol<T>>> mergedAlphabet1SymbolMap; 45 46 47 private final Alphabet<T> mergedAlphabet2; 48 49 53 private final SortedMap <Symbol<T>, SortedSet <Symbol<T>>> mergedAlphabet2SymbolMap; 54 55 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 98 AlphabetMergeResult( 99 Alphabet<T> newAlphabet, 100 Alphabet<T> mergedAlphabet1, 101 SortedMap <Symbol<T>, SortedSet <Symbol<T>>> mergedAlphabet1SymbolMap, 102 Alphabet<T> mergedAlphabet2, 103 SortedMap <Symbol<T>, SortedSet <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 159 public Alphabet<T> getNewAlphabet() { 160 161 return this.newAlphabet; 162 } 163 164 176 public SortedSet <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 if (this.mergedAlphabet1 == null) { 195 if (mergedAlphabet != this.newAlphabet) { 196 throw new InternalException("mergedAlphabet is invalid"); 197 } 198 199 TreeSet <Symbol<T>> set = new TreeSet <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 |