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