1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ******************************************************************************* 5 * Copyright (C) 2008, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 package com.ibm.icu.impl.jdkadapter; 10 11 import java.text.CollationKey; 12 13 import com.ibm.icu.text.Collator; 14 15 /** 16 * CollatorICU is an adapter class which wraps ICU4J Collator and 17 * implements java.text.Collator APIs. 18 */ 19 public class CollatorICU extends java.text.Collator { 20 21 private Collator fIcuCollator; 22 23 private CollatorICU(Collator icuCollator) { 24 fIcuCollator = icuCollator; 25 } 26 27 public static java.text.Collator wrap(Collator icuCollator) { 28 return new CollatorICU(icuCollator); 29 } 30 31 public Collator unwrap() { 32 return fIcuCollator; 33 } 34 35 @Override 36 public Object clone() { 37 CollatorICU other = (CollatorICU)super.clone(); 38 try { 39 other.fIcuCollator = (Collator)fIcuCollator.clone(); 40 } catch (CloneNotSupportedException e) { 41 // ICU Collator clone() may throw CloneNotSupportedException, 42 // but JDK does not. We use UnsupportedOperationException instead 43 // as workwround. 44 throw new UnsupportedOperationException("clone() is not supported by this ICU Collator."); 45 } 46 return other; 47 } 48 49 @Override 50 public int compare(Object o1, Object o2) { 51 return fIcuCollator.compare(o1, o2); 52 } 53 54 @Override 55 public int compare(String source, String target) { 56 return fIcuCollator.compare(source, target); 57 } 58 59 @Override 60 public boolean equals(Object that) { 61 if (that instanceof CollatorICU) { 62 return ((CollatorICU)that).fIcuCollator.equals(fIcuCollator); 63 } 64 return false; 65 } 66 67 @Override 68 public boolean equals(String source, String target) { 69 return fIcuCollator.equals(source, target); 70 } 71 72 @Override 73 public CollationKey getCollationKey(String source) { 74 com.ibm.icu.text.CollationKey icuCollKey = fIcuCollator.getCollationKey(source); 75 return CollationKeyICU.wrap(icuCollKey); 76 } 77 78 @Override 79 public int getDecomposition() { 80 int mode = java.text.Collator.NO_DECOMPOSITION; 81 82 if (fIcuCollator.getStrength() == Collator.IDENTICAL) { 83 return java.text.Collator.FULL_DECOMPOSITION; 84 } 85 int icuMode = fIcuCollator.getDecomposition(); 86 if (icuMode == Collator.CANONICAL_DECOMPOSITION) { 87 mode = java.text.Collator.CANONICAL_DECOMPOSITION; 88 } 89 // else if (icuMode == Collator.NO_DECOMPOSITION) { 90 // mode = java.text.Collator.NO_DECOMPOSITION; 91 // } 92 // else { 93 // throw new IllegalStateException("Unknown decomposition mode is used by the ICU Collator."); 94 // } 95 96 return mode; 97 } 98 99 @Override 100 public int getStrength() { 101 int strength; 102 int icuStrength = fIcuCollator.getStrength(); 103 switch (icuStrength) { 104 case Collator.IDENTICAL: 105 strength = java.text.Collator.IDENTICAL; 106 break; 107 case Collator.PRIMARY: 108 strength = java.text.Collator.PRIMARY; 109 break; 110 case Collator.SECONDARY: 111 strength = java.text.Collator.SECONDARY; 112 break; 113 case Collator.TERTIARY: 114 strength = java.text.Collator.TERTIARY; 115 break; 116 case Collator.QUATERNARY: 117 // Note: No quaternary support in Java.. 118 // Return tertiary instead for now. 119 strength = java.text.Collator.TERTIARY; 120 break; 121 default: 122 throw new IllegalStateException("Unknown strength is used by the ICU Collator."); 123 } 124 return strength; 125 } 126 127 @Override 128 public int hashCode() { 129 return fIcuCollator.hashCode(); 130 } 131 132 @Override 133 public void setDecomposition(int decompositionMode) { 134 switch (decompositionMode) { 135 case java.text.Collator.CANONICAL_DECOMPOSITION: 136 fIcuCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION); 137 break; 138 case java.text.Collator.NO_DECOMPOSITION: 139 fIcuCollator.setDecomposition(Collator.NO_DECOMPOSITION); 140 break; 141 case java.text.Collator.FULL_DECOMPOSITION: 142 // Not supported by ICU. 143 // This option is interpreted as IDENTICAL strength. 144 fIcuCollator.setStrength(Collator.IDENTICAL); 145 break; 146 default: 147 throw new IllegalArgumentException("Invalid decomposition mode."); 148 } 149 } 150 151 @Override 152 public void setStrength(int newStrength) { 153 switch (newStrength) { 154 case java.text.Collator.IDENTICAL: 155 fIcuCollator.setStrength(Collator.IDENTICAL); 156 break; 157 case java.text.Collator.PRIMARY: 158 fIcuCollator.setStrength(Collator.PRIMARY); 159 break; 160 case java.text.Collator.SECONDARY: 161 fIcuCollator.setStrength(Collator.SECONDARY); 162 break; 163 case java.text.Collator.TERTIARY: 164 fIcuCollator.setStrength(Collator.TERTIARY); 165 break; 166 default: 167 throw new IllegalArgumentException("Invalid strength."); 168 } 169 } 170 171 } 172