1 7 package com.ibm.icu.text; 8 9 import java.util.Comparator ; 10 import java.util.Locale ; 11 import java.util.MissingResourceException ; 12 import java.util.Set ; 13 14 import com.ibm.icu.impl.ICUDebug; 15 import com.ibm.icu.impl.ICUResourceBundle; 16 import com.ibm.icu.impl.LocaleUtility; 17 import com.ibm.icu.util.ULocale; 18 import com.ibm.icu.util.UResourceBundle; 19 import com.ibm.icu.util.VersionInfo; 20 21 120 public abstract class Collator implements Comparator , Cloneable 121 { 122 124 131 public final static int PRIMARY = 0; 132 133 143 public final static int SECONDARY = 1; 144 145 155 public final static int TERTIARY = 2; 156 157 169 public final static int QUATERNARY = 3; 170 171 184 public final static int IDENTICAL = 15; 185 186 193 public final static int FULL_DECOMPOSITION = IDENTICAL; 194 195 207 public final static int NO_DECOMPOSITION = 16; 208 209 223 public final static int CANONICAL_DECOMPOSITION = 17; 224 225 227 229 249 public void setStrength(int newStrength) 250 { 251 if ((newStrength != PRIMARY) && 252 (newStrength != SECONDARY) && 253 (newStrength != TERTIARY) && 254 (newStrength != QUATERNARY) && 255 (newStrength != IDENTICAL)) { 256 throw new IllegalArgumentException ("Incorrect comparison level."); 257 } 258 m_strength_ = newStrength; 259 } 260 261 291 public void setDecomposition(int decomposition) 292 { 293 if ((decomposition != NO_DECOMPOSITION) && 294 (decomposition != CANONICAL_DECOMPOSITION)) { 295 throw new IllegalArgumentException ("Wrong decomposition mode."); 296 } 297 m_decomposition_ = decomposition; 298 } 299 300 302 313 public static final Collator getInstance() 314 { 315 return getInstance(ULocale.getDefault()); 316 } 317 318 323 public Object clone() throws CloneNotSupportedException { 324 return super.clone(); 325 } 326 327 329 340 public static abstract class CollatorFactory { 341 349 public boolean visible() { 350 return true; 351 } 352 353 363 public Collator createCollator(ULocale loc) { 364 return createCollator(loc.toLocale()); 365 } 366 367 378 public Collator createCollator(Locale loc) { 379 return createCollator(ULocale.forLocale(loc)); 380 } 381 382 390 public String getDisplayName(Locale objectLocale, Locale displayLocale) { 391 return getDisplayName(ULocale.forLocale(objectLocale), ULocale.forLocale(displayLocale)); 392 } 393 394 403 public String getDisplayName(ULocale objectLocale, ULocale displayLocale) { 404 if (visible()) { 405 Set supported = getSupportedLocaleIDs(); 406 String name = objectLocale.getBaseName(); 407 if (supported.contains(name)) { 408 return objectLocale.getDisplayName(displayLocale); 409 } 410 } 411 return null; 412 } 413 414 421 public abstract Set getSupportedLocaleIDs(); 422 423 427 protected CollatorFactory() { 428 } 429 } 430 431 static abstract class ServiceShim { 432 abstract Collator getInstance(ULocale l); 433 abstract Object registerInstance(Collator c, ULocale l); 434 abstract Object registerFactory(CollatorFactory f); 435 abstract boolean unregister(Object k); 436 abstract Locale [] getAvailableLocales(); abstract ULocale[] getAvailableULocales(); 438 abstract String getDisplayName(ULocale ol, ULocale dl); 439 } 440 441 private static ServiceShim shim; 442 private static ServiceShim getShim() { 443 if (shim == null) { 448 try { 449 Class cls = Class.forName("com.ibm.icu.text.CollatorServiceShim"); 450 shim = (ServiceShim)cls.newInstance(); 451 } 452 catch (MissingResourceException e) 453 { 454 throw e; 455 } 456 catch (Exception e) { 457 if(DEBUG){ 459 e.printStackTrace(); 460 } 461 throw new RuntimeException (e.getMessage()); 462 } 464 } 465 return shim; 466 } 467 468 481 public static final Collator getInstance(ULocale locale) { 482 return getShim().getInstance(locale); 484 } 485 486 499 public static final Collator getInstance(Locale locale) { 500 return getInstance(ULocale.forLocale(locale)); 501 } 502 503 514 public static final Object registerInstance(Collator collator, ULocale locale) { 515 return getShim().registerInstance(collator, locale); 516 } 517 518 526 public static final Object registerFactory(CollatorFactory factory) { 527 return getShim().registerFactory(factory); 528 } 529 530 536 public static final boolean unregister(Object registryKey) { 537 if (shim == null) { 538 return false; 539 } 540 return shim.unregister(registryKey); 541 } 542 543 551 public static Locale [] getAvailableLocales() { 552 if (shim == null) { 554 return ICUResourceBundle.getAvailableLocales(ICUResourceBundle.ICU_COLLATION_BASE_NAME); 555 } 556 return shim.getAvailableLocales(); 557 } 558 559 567 public static final ULocale[] getAvailableULocales() { 568 if (shim == null) { 569 return ICUResourceBundle.getAvailableULocales(ICUResourceBundle.ICU_COLLATION_BASE_NAME); 570 } 571 return shim.getAvailableULocales(); 572 } 573 574 579 private static final String [] KEYWORDS = { "collation" }; 580 581 586 private static final String RESOURCE = "collations"; 587 588 592 private static final String BASE = ICUResourceBundle.ICU_COLLATION_BASE_NAME; 593 594 602 public static final String [] getKeywords() { 603 return KEYWORDS; 604 } 605 606 613 public static final String [] getKeywordValues(String keyword) { 614 if (!keyword.equals(KEYWORDS[0])) { 615 throw new IllegalArgumentException ("Invalid keyword: " + keyword); 616 } 617 return ICUResourceBundle.getKeywordValues(BASE, RESOURCE); 618 } 619 620 646 public static final ULocale getFunctionalEquivalent(String keyword, 647 ULocale locID, 648 boolean isAvailable[]) { 649 return ICUResourceBundle.getFunctionalEquivalent( 650 BASE, RESOURCE, keyword, locID, isAvailable); 651 } 652 653 664 public static final ULocale getFunctionalEquivalent(String keyword, 665 ULocale locID) { 666 return getFunctionalEquivalent(keyword, locID, null); 667 } 668 669 676 static public String getDisplayName(Locale objectLocale, Locale displayLocale) { 677 return getShim().getDisplayName(ULocale.forLocale(objectLocale), 678 ULocale.forLocale(displayLocale)); 679 } 680 681 689 static public String getDisplayName(ULocale objectLocale, ULocale displayLocale) { 690 return getShim().getDisplayName(objectLocale, displayLocale); 691 } 692 693 699 static public String getDisplayName(Locale objectLocale) { 700 return getShim().getDisplayName(ULocale.forLocale(objectLocale), ULocale.getDefault()); 701 } 702 703 710 static public String getDisplayName(ULocale objectLocale) { 711 return getShim().getDisplayName(objectLocale, ULocale.getDefault()); 712 } 713 714 730 public int getStrength() 731 { 732 return m_strength_; 733 } 734 735 749 public int getDecomposition() 750 { 751 return m_decomposition_; 752 } 753 754 775 public int compare(Object source, Object target) 776 { 777 if (!(source instanceof String ) || !(target instanceof String )) { 778 throw new IllegalArgumentException ("Arguments have to be of type String"); 779 } 780 return compare((String )source, (String )target); 781 } 782 783 785 796 public boolean equals(String source, String target) 797 { 798 return (compare(source, target) == 0); 799 } 800 801 809 public UnicodeSet getTailoredSet() 810 { 811 return new UnicodeSet(0, 0x10FFFF); 812 } 813 814 833 public abstract int compare(String source, String target); 834 835 851 public abstract CollationKey getCollationKey(String source); 852 853 869 public abstract RawCollationKey getRawCollationKey(String source, 870 RawCollationKey key); 871 872 894 public abstract int setVariableTop(String varTop); 895 896 903 public abstract int getVariableTop(); 904 905 915 public abstract void setVariableTop(int varTop); 916 917 922 public abstract VersionInfo getVersion(); 923 924 929 public abstract VersionInfo getUCAVersion(); 930 931 933 937 protected Collator() 938 { 939 } 940 941 943 945 948 private int m_strength_ = TERTIARY; 949 950 953 private int m_decomposition_ = CANONICAL_DECOMPOSITION; 954 955 private static final boolean DEBUG = ICUDebug.enabled("collator"); 956 957 959 961 963 987 public final ULocale getLocale(ULocale.Type type) { 988 return type == ULocale.ACTUAL_LOCALE ? 989 this.actualLocale : this.validLocale; 990 } 991 992 1009 final void setLocale(ULocale valid, ULocale actual) { 1010 if ((valid == null) != (actual == null)) { 1012 throw new IllegalArgumentException (); 1014 } 1016 this.validLocale = valid; 1019 this.actualLocale = actual; 1020 } 1021 1022 1027 private ULocale validLocale; 1028 1029 1035 private ULocale actualLocale; 1036 1037 } 1039 | Popular Tags |