2 *******************************************************************************
3 * Copyright (C) 2008, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.impl.jdkadapter;
9 import java.text.CollationKey;
11 import com.ibm.icu.text.Collator;
14 * CollatorICU is an adapter class which wraps ICU4J Collator and
15 * implements java.text.Collator APIs.
17 public class CollatorICU extends java.text.Collator {
19 private Collator fIcuCollator;
21 private CollatorICU(Collator icuCollator) {
22 fIcuCollator = icuCollator;
25 public static java.text.Collator wrap(Collator icuCollator) {
26 return new CollatorICU(icuCollator);
29 public Collator unwrap() {
33 public Object clone() {
34 CollatorICU other = (CollatorICU)super.clone();
36 other.fIcuCollator = (Collator)fIcuCollator.clone();
37 } catch (CloneNotSupportedException e) {
38 // ICU Collator clone() may throw CloneNotSupportedException,
39 // but JDK does not. We use UnsupportedOperationException instead
41 throw new UnsupportedOperationException("clone() is not supported by this ICU Collator.");
46 public int compare(Object o1, Object o2) {
47 return fIcuCollator.compare(o1, o2);
50 public int compare(String source, String target) {
51 return fIcuCollator.compare(source, target);
54 public boolean equals(Object that) {
55 if (that instanceof CollatorICU) {
56 return ((CollatorICU)that).fIcuCollator.equals(fIcuCollator);
61 public boolean equals(String source, String target) {
62 return fIcuCollator.equals(source, target);
65 public CollationKey getCollationKey(String source) {
66 com.ibm.icu.text.CollationKey icuCollKey = fIcuCollator.getCollationKey(source);
67 return CollationKeyICU.wrap(icuCollKey);
70 public int getDecomposition() {
71 int mode = java.text.Collator.NO_DECOMPOSITION;
73 if (fIcuCollator.getStrength() == Collator.IDENTICAL) {
74 return java.text.Collator.FULL_DECOMPOSITION;
76 int icuMode = fIcuCollator.getDecomposition();
77 if (icuMode == Collator.CANONICAL_DECOMPOSITION) {
78 mode = java.text.Collator.CANONICAL_DECOMPOSITION;
80 // else if (icuMode == Collator.NO_DECOMPOSITION) {
81 // mode = java.text.Collator.NO_DECOMPOSITION;
84 // throw new IllegalStateException("Unknown decomposition mode is used by the ICU Collator.");
90 public int getStrength() {
92 int icuStrength = fIcuCollator.getStrength();
93 switch (icuStrength) {
94 case Collator.IDENTICAL:
95 strength = java.text.Collator.IDENTICAL;
97 case Collator.PRIMARY:
98 strength = java.text.Collator.PRIMARY;
100 case Collator.SECONDARY:
101 strength = java.text.Collator.SECONDARY;
103 case Collator.TERTIARY:
104 strength = java.text.Collator.TERTIARY;
106 case Collator.QUATERNARY:
107 // Note: No quaternary support in Java..
108 // Return tertiary instead for now.
109 strength = java.text.Collator.TERTIARY;
112 throw new IllegalStateException("Unknown strength is used by the ICU Collator.");
117 public int hashCode() {
118 return fIcuCollator.hashCode();
121 public void setDecomposition(int decompositionMode) {
122 switch (decompositionMode) {
123 case java.text.Collator.CANONICAL_DECOMPOSITION:
124 fIcuCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
126 case java.text.Collator.NO_DECOMPOSITION:
127 fIcuCollator.setDecomposition(Collator.NO_DECOMPOSITION);
129 case java.text.Collator.FULL_DECOMPOSITION:
130 // Not supported by ICU.
131 // This option is interpreted as IDENTICAL strength.
132 fIcuCollator.setStrength(Collator.IDENTICAL);
135 throw new IllegalArgumentException("Invalid decomposition mode.");
139 public void setStrength(int newStrength) {
140 switch (newStrength) {
141 case java.text.Collator.IDENTICAL:
142 fIcuCollator.setStrength(Collator.IDENTICAL);
144 case java.text.Collator.PRIMARY:
145 fIcuCollator.setStrength(Collator.PRIMARY);
147 case java.text.Collator.SECONDARY:
148 fIcuCollator.setStrength(Collator.SECONDARY);
150 case java.text.Collator.TERTIARY:
151 fIcuCollator.setStrength(Collator.TERTIARY);
154 throw new IllegalArgumentException("Invalid strength.");