1 7 8 20 21 package java.text; 22 23 import java.util.ArrayList ; 24 25 45 46 final class MergeCollation { 47 48 52 public MergeCollation(String pattern) throws ParseException 53 { 54 for (int i = 0; i < statusArray.length; i++) 55 statusArray[i] = 0; 56 setPattern(pattern); 57 } 58 59 62 public String getPattern() { 63 return getPattern(true); 64 } 65 66 71 public String getPattern(boolean withWhiteSpace) { 72 StringBuffer result = new StringBuffer (); 73 PatternEntry tmp = null; 74 ArrayList extList = null; 75 int i; 76 for (i = 0; i < patterns.size(); ++i) { 77 PatternEntry entry = (PatternEntry ) patterns.get(i); 78 if (entry.extension.length() != 0) { 79 if (extList == null) 80 extList = new ArrayList (); 81 extList.add(entry); 82 } else { 83 if (extList != null) { 84 PatternEntry last = findLastWithNoExtension(i-1); 85 for (int j = extList.size() - 1; j >= 0 ; j--) { 86 tmp = (PatternEntry )(extList.get(j)); 87 tmp.addToBuffer(result, false, withWhiteSpace, last); 88 } 89 extList = null; 90 } 91 entry.addToBuffer(result, false, withWhiteSpace, null); 92 } 93 } 94 if (extList != null) { 95 PatternEntry last = findLastWithNoExtension(i-1); 96 for (int j = extList.size() - 1; j >= 0 ; j--) { 97 tmp = (PatternEntry )(extList.get(j)); 98 tmp.addToBuffer(result, false, withWhiteSpace, last); 99 } 100 extList = null; 101 } 102 return result.toString(); 103 } 104 105 private final PatternEntry findLastWithNoExtension(int i) { 106 for (--i;i >= 0; --i) { 107 PatternEntry entry = (PatternEntry ) patterns.get(i); 108 if (entry.extension.length() == 0) { 109 return entry; 110 } 111 } 112 return null; 113 } 114 115 120 public String emitPattern() { 121 return emitPattern(true); 122 } 123 124 131 public String emitPattern(boolean withWhiteSpace) { 132 StringBuffer result = new StringBuffer (); 133 for (int i = 0; i < patterns.size(); ++i) 134 { 135 PatternEntry entry = (PatternEntry ) patterns.get(i); 136 if (entry != null) { 137 entry.addToBuffer(result, true, withWhiteSpace, null); 138 } 139 } 140 return result.toString(); 141 } 142 143 146 public void setPattern(String pattern) throws ParseException 147 { 148 patterns.clear(); 149 addPattern(pattern); 150 } 151 152 156 public void addPattern(String pattern) throws ParseException 157 { 158 if (pattern == null) 159 return; 160 161 PatternEntry.Parser parser = new PatternEntry.Parser (pattern); 162 163 PatternEntry entry = parser.next(); 164 while (entry != null) { 165 fixEntry(entry); 166 entry = parser.next(); 167 } 168 } 169 170 174 public int getCount() { 175 return patterns.size(); 176 } 177 178 183 public PatternEntry getItemAt(int index) { 184 return (PatternEntry ) patterns.get(index); 185 } 186 187 ArrayList patterns = new ArrayList (); 192 private transient PatternEntry saveEntry = null; 193 private transient PatternEntry lastEntry = null; 194 195 private transient StringBuffer excess = new StringBuffer (); 198 199 private transient byte[] statusArray = new byte[8192]; 208 private final byte BITARRAYMASK = (byte)0x1; 209 private final int BYTEPOWER = 3; 210 private final int BYTEMASK = (1 << BYTEPOWER) - 1; 211 212 218 private final void fixEntry(PatternEntry newEntry) throws ParseException 219 { 220 if (lastEntry != null && newEntry.chars.equals(lastEntry.chars) 226 && newEntry.extension.equals(lastEntry.extension)) { 227 if (newEntry.strength != Collator.IDENTICAL 228 && newEntry.strength != PatternEntry.RESET) { 229 throw new ParseException ("The entries " + lastEntry + " and " 230 + newEntry + " are adjacent in the rules, but have conflicting " 231 + "strengths: A character can't be unequal to itself.", -1); 232 } else { 233 return; 235 } 236 } 237 238 boolean changeLastEntry = true; 239 if (newEntry.strength != PatternEntry.RESET) { 240 int oldIndex = -1; 241 242 if ((newEntry.chars.length() == 1)) { 243 244 char c = newEntry.chars.charAt(0); 245 int statusIndex = c >> BYTEPOWER; 246 byte bitClump = statusArray[statusIndex]; 247 byte setBit = (byte)(BITARRAYMASK << (c & BYTEMASK)); 248 249 if (bitClump != 0 && (bitClump & setBit) != 0) { 250 oldIndex = patterns.lastIndexOf(newEntry); 251 } else { 252 statusArray[statusIndex] = (byte)(bitClump | setBit); 255 } 256 } else { 257 oldIndex = patterns.lastIndexOf(newEntry); 258 } 259 if (oldIndex != -1) { 260 patterns.remove(oldIndex); 261 } 262 263 excess.setLength(0); 264 int lastIndex = findLastEntry(lastEntry, excess); 265 266 if (excess.length() != 0) { 267 newEntry.extension = excess + newEntry.extension; 268 if (lastIndex != patterns.size()) { 269 lastEntry = saveEntry; 270 changeLastEntry = false; 271 } 272 } 273 if (lastIndex == patterns.size()) { 274 patterns.add(newEntry); 275 saveEntry = newEntry; 276 } else { 277 patterns.add(lastIndex, newEntry); 278 } 279 } 280 if (changeLastEntry) { 281 lastEntry = newEntry; 282 } 283 } 284 285 private final int findLastEntry(PatternEntry entry, 286 StringBuffer excessChars) throws ParseException 287 { 288 if (entry == null) 289 return 0; 290 291 if (entry.strength != PatternEntry.RESET) { 292 295 int oldIndex = -1; 296 if ((entry.chars.length() == 1)) { 297 int index = entry.chars.charAt(0) >> BYTEPOWER; 298 if ((statusArray[index] & 299 (BITARRAYMASK << (entry.chars.charAt(0) & BYTEMASK))) != 0) { 300 oldIndex = patterns.lastIndexOf(entry); 301 } 302 } else { 303 oldIndex = patterns.lastIndexOf(entry); 304 } 305 if ((oldIndex == -1)) 306 throw new ParseException ("couldn't find last entry: " 307 + entry, oldIndex); 308 return oldIndex + 1; 309 } else { 310 int i; 311 for (i = patterns.size() - 1; i >= 0; --i) { 312 PatternEntry e = (PatternEntry ) patterns.get(i); 313 if (e.chars.regionMatches(0,entry.chars,0, 314 e.chars.length())) { 315 excessChars.append(entry.chars.substring(e.chars.length(), 316 entry.chars.length())); 317 break; 318 } 319 } 320 if (i == -1) 321 throw new ParseException ("couldn't find: " + entry, i); 322 return i + 1; 323 } 324 } 325 } 326 327 | Popular Tags |