Source for java.text.Collator

   1: /* Collator.java -- Perform locale dependent String comparisons.
   2:    Copyright (C) 1998, 1999, 2000, 2001, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10:  
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.text;
  40: 
  41: import gnu.java.locale.LocaleHelper;
  42: 
  43: import java.util.Comparator;
  44: import java.util.Locale;
  45: import java.util.MissingResourceException;
  46: import java.util.ResourceBundle;
  47: 
  48: /**
  49:  * This class is the abstract superclass of classes which perform 
  50:  * locale dependent <code>String</code> comparisons.  A caller requests
  51:  * an instance of <code>Collator</code> for a particular locale using
  52:  * the <code>getInstance()</code> static method in this class.  That method
  53:  * will return a locale specific subclass of <code>Collator</code> which
  54:  * can be used to perform <code>String</code> comparisons for that locale.
  55:  * If a subclass of <code>Collator</code> cannot be located for a particular
  56:  * locale, a default instance for the current locale will be returned.  
  57:  *
  58:  * In addition to setting the correct locale, there are two additional
  59:  * settings that can be adjusted to affect <code>String</code> comparisons:
  60:  * strength and decomposition.  The strength value determines the level
  61:  * of signficance of character differences required for them to sort
  62:  * differently.  (For example, whether or not capital letters are considered
  63:  * different from lower case letters).  The decomposition value affects how
  64:  * variants of the same character are treated for sorting purposes.  (For
  65:  * example, whether or not an accent is signficant or not).  These settings
  66:  * are described in detail in the documentation for the methods and values
  67:  * that are related to them.
  68:  *
  69:  * @author Tom Tromey (tromey@cygnus.com)
  70:  * @author Aaron M. Renn (arenn@urbanophile.com)
  71:  * @date March 18, 1999
  72:  */
  73: public abstract class Collator implements Comparator, Cloneable
  74: {
  75:   /**
  76:    * This constant is a strength value which indicates that only primary
  77:    * differences between characters will be considered signficant.  As an
  78:    * example, two completely different English letters such as 'a' and 'b'
  79:    * are considered to have a primary difference.
  80:    */
  81:   public static final int PRIMARY = 0;
  82:   
  83:   /**
  84:    * This constant is a strength value which indicates that only secondary
  85:    * or primary differences between characters will be considered
  86:    * significant.  An example of a secondary difference between characters
  87:    * are instances of the same letter with different accented forms.
  88:    */
  89:   public static final int SECONDARY = 1;
  90:   
  91:   /**
  92:    * This constant is a strength value which indicates that tertiary,
  93:    * secondary, and primary differences will be considered during sorting.
  94:    * An example of a tertiary difference is capitalization of a given letter.
  95:    * This is the default value for the strength setting.
  96:    */
  97:   public static final int TERTIARY = 2;
  98:   
  99:   /**
 100:    * This constant is a strength value which indicates that any difference
 101:    * at all between character values are considered significant.
 102:    */
 103:   public static final int IDENTICAL = 3;
 104:   
 105:   /**
 106:    * This constant indicates that accented characters won't be decomposed
 107:    * when performing comparisons.  This will yield the fastest results, but
 108:    * will only work correctly in call cases for languages which do not
 109:    * use accents such as English.
 110:    */
 111:   public static final int NO_DECOMPOSITION = 0;
 112:   
 113:   /**
 114:    * This constant indicates that only characters which are canonical variants
 115:    * in Unicode 2.0 will be decomposed prior to performing comparisons.  This
 116:    * will cause accented languages to be sorted correctly.  This is the
 117:    * default decomposition value.
 118:    */
 119:   public static final int CANONICAL_DECOMPOSITION = 1;
 120:   
 121:   /**
 122:    * This constant indicates that both canonical variants and compatibility
 123:    * variants in Unicode 2.0 will be decomposed prior to performing
 124:    * comparisons.  This is the slowest mode, but is required to get the
 125:    * correct sorting for certain languages with certain special formats.
 126:    */
 127:   public static final int FULL_DECOMPOSITION = 2;
 128: 
 129:   /**
 130:    * This method initializes a new instance of <code>Collator</code> to have
 131:    * the default strength (TERTIARY) and decomposition 
 132:    * (CANONICAL_DECOMPOSITION) settings.  This constructor is protected and
 133:    * is for use by subclasses only.  Non-subclass callers should use the
 134:    * static <code>getInstance()</code> methods of this class to instantiate
 135:    * <code>Collation</code> objects for the desired locale.
 136:    */
 137:   protected Collator ()
 138:   {
 139:     strength = TERTIARY;
 140:     decmp = CANONICAL_DECOMPOSITION;
 141:   }
 142: 
 143:   /**
 144:    * This method compares the two <code>String</code>'s and returns an
 145:    * integer indicating whether or not the first argument is less than,
 146:    * equal to, or greater than the second argument.  The comparison is
 147:    * performed according to the rules of the locale for this 
 148:    * <code>Collator</code> and the strength and decomposition rules in
 149:    * effect.
 150:    *
 151:    * @param source The first object to compare
 152:    * @param target The second object to compare
 153:    *
 154:    * @return A negative integer if str1 &lt; str2, 0 if str1 == str2, or
 155:    * a positive integer if str1 &gt; str2. 
 156:    */
 157:   public abstract int compare (String source, String target);
 158: 
 159:   /**
 160:    * This method compares the two <code>Object</code>'s and returns an
 161:    * integer indicating whether or not the first argument is less than,
 162:    * equal to, or greater than the second argument.  These two objects
 163:    * must be <code>String</code>'s or an exception will be thrown.
 164:    *
 165:    * @param o1 The first object to compare
 166:    * @param o2 The second object to compare
 167:    *
 168:    * @return A negative integer if obj1 &lt; obj2, 0 if obj1 == obj2, or
 169:    * a positive integer if obj1 &gt; obj2. 
 170:    *
 171:    * @exception ClassCastException If the arguments are not instances
 172:    * of <code>String</code>. 
 173:    */
 174:   public int compare (Object o1, Object o2)
 175:   {
 176:     return compare ((String) o1, (String) o2);
 177:   }
 178: 
 179:   /**
 180:    * This method tests the specified object for equality against this
 181:    * object.  This will be true if and only if the following conditions are
 182:    * met:
 183:    * <ul>
 184:    * <li>The specified object is not <code>null</code>.</li>
 185:    * <li>The specified object is an instance of <code>Collator</code>.</li>
 186:    * <li>The specified object has the same strength and decomposition
 187:    * settings as this object.</li>
 188:    * </ul>
 189:    *
 190:    * @param obj The <code>Object</code> to test for equality against
 191:    *            this object. 
 192:    *
 193:    * @return <code>true</code> if the specified object is equal to
 194:    * this one, <code>false</code> otherwise.
 195:    */
 196:   public boolean equals (Object obj)
 197:   {
 198:     if (! (obj instanceof Collator))
 199:       return false;
 200:     Collator c = (Collator) obj;
 201:     return decmp == c.decmp && strength == c.strength;
 202:   }
 203: 
 204:   /**
 205:    * This method tests whether the specified <code>String</code>'s are equal
 206:    * according to the collation rules for the locale of this object and
 207:    * the current strength and decomposition settings.
 208:    *
 209:    * @param source The first <code>String</code> to compare
 210:    * @param target The second <code>String</code> to compare
 211:    *
 212:    * @return <code>true</code> if the two strings are equal,
 213:    * <code>false</code> otherwise. 
 214:    */
 215:   public boolean equals (String source, String target)
 216:   {
 217:     return compare (source, target) == 0;
 218:   }
 219: 
 220:   /**
 221:    * This method returns a copy of this <code>Collator</code> object.
 222:    *
 223:    * @return A duplicate of this object.
 224:    */
 225:   public Object clone ()
 226:   {
 227:     try
 228:       {
 229:     return super.clone ();
 230:       }
 231:     catch (CloneNotSupportedException _)
 232:       {
 233:     return null;
 234:       }
 235:   }
 236: 
 237:   /**
 238:    * This method returns an array of <code>Locale</code> objects which is
 239:    * the list of locales for which <code>Collator</code> objects exist.
 240:    *
 241:    * @return The list of locales for which <code>Collator</code>'s exist.
 242:    */
 243:   public static synchronized Locale[] getAvailableLocales ()
 244:   {
 245:     return LocaleHelper.getCollatorLocales();
 246:   }
 247: 
 248:   /**
 249:    * This method transforms the specified <code>String</code> into a
 250:    * <code>CollationKey</code> for faster comparisons.  This is useful when
 251:    * comparisons against a string might be performed multiple times, such
 252:    * as during a sort operation.
 253:    *
 254:    * @param source The <code>String</code> to convert.
 255:    *
 256:    * @return A <code>CollationKey</code> for the specified <code>String</code>.
 257:    */
 258:   public abstract CollationKey getCollationKey (String source);
 259: 
 260:   /**
 261:    * This method returns the current decomposition setting for this
 262:    * object.  This * will be one of NO_DECOMPOSITION,
 263:    * CANONICAL_DECOMPOSITION, or * FULL_DECOMPOSITION.  See the
 264:    * documentation for those constants for an * explanation of this
 265:    * setting.
 266:    *
 267:    * @return The current decomposition setting.
 268:    */
 269:   public synchronized int getDecomposition ()
 270:   {
 271:     return decmp;
 272:   }
 273: 
 274:   /**
 275:    * This method returns an instance of <code>Collator</code> for the
 276:    * default locale.
 277:    *
 278:    * @return A <code>Collator</code> for the default locale.
 279:    */
 280:   public static Collator getInstance ()
 281:   {
 282:     return getInstance (Locale.getDefault());
 283:   }
 284: 
 285:   /**
 286:    * This method returns an instance of <code>Collator</code> for the
 287:    * specified locale.  If no <code>Collator</code> exists for the desired
 288:    * locale, a <code>Collator</code> for the default locale will be returned.
 289:    *
 290:    * @param loc The desired locale to load a <code>Collator</code> for.
 291:    *
 292:    * @return A <code>Collator</code> for the requested locale
 293:    */
 294:   public static Collator getInstance (Locale loc)
 295:   {
 296:     ResourceBundle res;
 297:     String pattern;
 298:     try
 299:       {
 300:     res = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
 301:                        loc, ClassLoader.getSystemClassLoader());
 302:     pattern = res.getString("collation_rules");
 303:       }
 304:     catch (MissingResourceException x)
 305:       {
 306:     pattern = "<0<1<2<3<4<5<6<7<8<9<A,a<b,B<c,C<d,D<e,E<f,F<g,G<h,H<i,I<j,J<k,K" +
 307:         "<l,L<m,M<n,N<o,O<p,P<q,Q<r,R<s,S<t,T<u,U<v,V<w,W<x,X<y,Y<z,Z";
 308:       }
 309:     try
 310:       {
 311:     return new RuleBasedCollator (pattern);
 312:       }
 313:     catch (ParseException x)
 314:       {
 315:     throw (InternalError)new InternalError().initCause(x);
 316:       }
 317:   }
 318: 
 319:   /**
 320:    * This method returns the current strength setting for this object.  This
 321:    * will be one of PRIMARY, SECONDARY, TERTIARY, or IDENTICAL.  See the
 322:    * documentation for those constants for an explanation of this setting.
 323:    *
 324:    * @return The current strength setting.
 325:    */
 326:   public synchronized int getStrength ()
 327:   {
 328:     return strength;
 329:   }
 330: 
 331:   /**
 332:    * This method returns a hash code value for this object.
 333:    *
 334:    * @return A hash value for this object.
 335:    */
 336:   public abstract int hashCode ();
 337: 
 338:   /**
 339:    * This method sets the decomposition setting for this object to the
 340:    * specified value.  This must be one of NO_DECOMPOSITION,
 341:    * CANONICAL_DECOMPOSITION, or FULL_DECOMPOSITION.  Otherwise an
 342:    * exception will be thrown.  See the documentation for those
 343:    * contants for an explanation of this setting.
 344:    *
 345:    * @param mode The new decomposition setting.
 346:    *
 347:    * @exception IllegalArgumentException If the requested
 348:    * decomposition setting is not valid.
 349:    */
 350:   public synchronized void setDecomposition (int mode)
 351:   {
 352:     if (mode != NO_DECOMPOSITION
 353:     && mode != CANONICAL_DECOMPOSITION
 354:     && mode != FULL_DECOMPOSITION)
 355:       throw new IllegalArgumentException ();
 356:     decmp = mode;
 357:   }
 358: 
 359:   /**
 360:    * This method sets the strength setting for this object to the specified
 361:    * value.  This must be one of PRIMARY, SECONDARY, TERTIARY, or IDENTICAL.
 362:    * Otherwise an exception is thrown. See the documentation for these
 363:    * constants for an explanation of this setting.
 364:    * 
 365:    * @param strength The new strength setting.
 366:    *
 367:    * @exception IllegalArgumentException If the requested strength
 368:    * setting value is not valid.
 369:    */
 370:   public synchronized void setStrength (int strength)
 371:   {
 372:     if (strength != PRIMARY && strength != SECONDARY
 373:     && strength != TERTIARY && strength != IDENTICAL)
 374:       throw new IllegalArgumentException ();
 375:     this.strength = strength;
 376:   }
 377: 
 378:   // Decompose a single character and append results to the buffer.
 379:   // FIXME: for libgcj this is a native method which handles
 380:   // decomposition.  For Classpath, for now, it does nothing.
 381:   final void decomposeCharacter (char c, StringBuffer buf)
 382:   {
 383:     buf.append (c);
 384:   }
 385: 
 386:   /**
 387:    * This is the current collation decomposition setting.
 388:    */
 389:   int decmp;
 390: 
 391:   /**
 392:    * This is the current collation strength setting.
 393:    */
 394:   int strength;
 395: }