1 7 8 20 21 package java.text; 22 23 import java.util.Locale ; 24 import java.util.MissingResourceException ; 25 import java.util.ResourceBundle ; 26 import sun.misc.SoftCache; 27 import sun.text.resources.LocaleData; 28 29 30 107 108 public abstract class Collator 109 implements java.util.Comparator <Object >, Cloneable 110 { 111 119 public final static int PRIMARY = 0; 120 129 public final static int SECONDARY = 1; 130 138 public final static int TERTIARY = 2; 139 140 151 public final static int IDENTICAL = 3; 152 153 161 public final static int NO_DECOMPOSITION = 0; 162 163 176 public final static int CANONICAL_DECOMPOSITION = 1; 177 178 195 public final static int FULL_DECOMPOSITION = 2; 196 197 203 public static synchronized Collator getInstance() { 204 return getInstance(Locale.getDefault()); 205 } 206 207 214 public static synchronized 215 Collator getInstance(Locale desiredLocale) 216 { 217 RuleBasedCollator result = null; 218 result = (RuleBasedCollator ) cache.get(desiredLocale); 219 if (result != null) { 220 return (Collator )result.clone(); } 222 223 String colString = ""; 226 int decomp = CANONICAL_DECOMPOSITION; 227 228 try { 229 ResourceBundle resource = LocaleData.getLocaleElements(desiredLocale); 230 231 colString = resource.getString("CollationElements"); 232 decomp = ((Integer )resource.getObject("CollationDecomp")).intValue(); 233 } catch (MissingResourceException e) { 234 } 236 try 237 { 238 result = new RuleBasedCollator ( CollationRules.DEFAULTRULES + 239 colString, 240 decomp ); 241 } 242 catch(ParseException foo) 243 { 244 try { 246 result = new RuleBasedCollator ( CollationRules.DEFAULTRULES ); 247 } catch (ParseException bar) { 248 } 250 } 251 result.setDecomposition(NO_DECOMPOSITION); 255 256 cache.put(desiredLocale,result); 257 return (Collator )result.clone(); 258 } 259 260 279 public abstract int compare(String source, String target); 280 281 296 public int compare(Object o1, Object o2) { 297 return compare((String )o1, (String )o2); 298 } 299 300 311 public abstract CollationKey getCollationKey(String source); 312 313 322 public boolean equals(String source, String target) 323 { 324 return (compare(source, target) == Collator.EQUAL); 325 } 326 327 338 public synchronized int getStrength() 339 { 340 return strength; 341 } 342 343 356 public synchronized void setStrength(int newStrength) { 357 if ((newStrength != PRIMARY) && 358 (newStrength != SECONDARY) && 359 (newStrength != TERTIARY) && 360 (newStrength != IDENTICAL)) 361 throw new IllegalArgumentException ("Incorrect comparison level."); 362 strength = newStrength; 363 } 364 365 384 public synchronized int getDecomposition() 385 { 386 return decmp; 387 } 388 399 public synchronized void setDecomposition(int decompositionMode) { 400 if ((decompositionMode != NO_DECOMPOSITION) && 401 (decompositionMode != CANONICAL_DECOMPOSITION) && 402 (decompositionMode != FULL_DECOMPOSITION)) 403 throw new IllegalArgumentException ("Wrong decomposition mode."); 404 decmp = decompositionMode; 405 } 406 407 417 public static synchronized Locale [] getAvailableLocales() { 418 return LocaleData.getAvailableLocales("CollationElements"); 419 } 420 421 424 public Object clone() 425 { 426 try { 427 return (Collator )super.clone(); 428 } catch (CloneNotSupportedException e) { 429 throw new InternalError (); 430 } 431 } 432 433 439 public boolean equals(Object that) 440 { 441 if (this == that) return true; 442 if (that == null) return false; 443 if (getClass() != that.getClass()) return false; 444 Collator other = (Collator ) that; 445 return ((strength == other.strength) && 446 (decmp == other.decmp)); 447 } 448 449 452 abstract public int hashCode(); 453 454 460 protected Collator() 461 { 462 strength = TERTIARY; 463 decmp = CANONICAL_DECOMPOSITION; 464 } 465 466 private int strength = 0; 467 private int decmp = 0; 468 private static SoftCache cache = new SoftCache(); 469 470 478 final static int LESS = -1; 479 484 final static int EQUAL = 0; 485 490 final static int GREATER = 1; 491 } 492 | Popular Tags |