1 /* Collator.java -- Perform locale dependent String comparisons.
2 Copyright (C) 1998, 1999, 2000, 2001, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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., 59 Temple Place, Suite 330, Boston, MA
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
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. */
41 import java
.util
.Comparator
;
42 import java
.util
.Locale
;
43 import java
.util
.MissingResourceException
;
44 import java
.util
.ResourceBundle
;
47 * This class is the abstract superclass of classes which perform
48 * locale dependent <code>String</code> comparisons. A caller requests
49 * an instance of <code>Collator</code> for a particular locale using
50 * the <code>getInstance()</code> static method in this class. That method
51 * will return a locale specific subclass of <code>Collator</code> which
52 * can be used to perform <code>String</code> comparisons for that locale.
53 * If a subclass of <code>Collator</code> cannot be located for a particular
54 * locale, a default instance for the current locale will be returned.
56 * In addition to setting the correct locale, there are two additional
57 * settings that can be adjusted to affect <code>String</code> comparisons:
58 * strength and decomposition. The strength value determines the level
59 * of signficance of character differences required for them to sort
60 * differently. (For example, whether or not capital letters are considered
61 * different from lower case letters). The decomposition value affects how
62 * variants of the same character are treated for sorting purposes. (For
63 * example, whether or not an accent is signficant or not). These settings
64 * are described in detail in the documentation for the methods and values
65 * that are related to them.
67 * @author Tom Tromey <tromey@cygnus.com>
68 * @author Aaron M. Renn (arenn@urbanophile.com)
69 * @date March 18, 1999
71 /* Written using "Java Class Libraries", 2nd edition, plus online
72 * API docs for JDK 1.2 from http://www.javasoft.com.
73 * Status: Mostly complete, but parts stubbed out. Look for FIXME.
75 public abstract class Collator
implements Comparator
, Cloneable
78 * This constant is a strength value which indicates that only primary
79 * differences between characters will be considered signficant. As an
80 * example, two completely different English letters such as 'a' and 'b'
81 * are considered to have a primary difference.
83 public static final int PRIMARY
= 0;
86 * This constant is a strength value which indicates that only secondary
87 * or primary differences between characters will be considered
88 * significant. An example of a secondary difference between characters
89 * are instances of the same letter with different accented forms.
91 public static final int SECONDARY
= 1;
94 * This constant is a strength value which indicates that tertiary,
95 * secondary, and primary differences will be considered during sorting.
96 * An example of a tertiary difference is capitalization of a given letter.
97 * This is the default value for the strength setting.
99 public static final int TERTIARY
= 2;
102 * This constant is a strength value which indicates that any difference
103 * at all between character values are considered significant.
105 public static final int IDENTICAL
= 3;
108 * This constant indicates that accented characters won't be decomposed
109 * when performing comparisons. This will yield the fastest results, but
110 * will only work correctly in call cases for languages which do not
111 * use accents such as English.
113 public static final int NO_DECOMPOSITION
= 0;
116 * This constant indicates that only characters which are canonical variants
117 * in Unicode 2.0 will be decomposed prior to performing comparisons. This
118 * will cause accented languages to be sorted correctly. This is the
119 * default decomposition value.
121 public static final int CANONICAL_DECOMPOSITION
= 1;
124 * This constant indicates that both canonical variants and compatibility
125 * variants in Unicode 2.0 will be decomposed prior to performing
126 * comparisons. This is the slowest mode, but is required to get the
127 * correct sorting for certain languages with certain special formats.
129 public static final int FULL_DECOMPOSITION
= 2;
132 * This method initializes a new instance of <code>Collator</code> to have
133 * the default strength (TERTIARY) and decomposition
134 * (CANONICAL_DECOMPOSITION) settings. This constructor is protected and
135 * is for use by subclasses only. Non-subclass callers should use the
136 * static <code>getInstance()</code> methods of this class to instantiate
137 * <code>Collation</code> objects for the desired locale.
139 protected Collator ()
142 decmp
= CANONICAL_DECOMPOSITION
;
146 * This method compares the two <code>String</code>'s and returns an
147 * integer indicating whether or not the first argument is less than,
148 * equal to, or greater than the second argument. The comparison is
149 * performed according to the rules of the locale for this
150 * <code>Collator</code> and the strength and decomposition rules in
153 * @param str1 The first object to compare
154 * @param str2 The second object to compare
156 * @return A negative integer if str1 < str2, 0 if str1 == str2, or
157 * a positive integer if str1 > str2.
159 public abstract int compare (String source
, String target
);
162 * This method compares the two <code>Object</code>'s and returns an
163 * integer indicating whether or not the first argument is less than,
164 * equal to, or greater than the second argument. These two objects
165 * must be <code>String</code>'s or an exception will be thrown.
167 * @param obj1 The first object to compare
168 * @param obj2 The second object to compare
170 * @return A negative integer if obj1 < obj2, 0 if obj1 == obj2, or
171 * a positive integer if obj1 > obj2.
173 * @exception ClassCastException If the arguments are not instances
174 * of <code>String</code>.
176 public int compare (Object o1
, Object o2
)
178 return compare ((String
) o1
, (String
) o2
);
182 * This method tests the specified object for equality against this
183 * object. This will be true if and only if the following conditions are
186 * <li>The specified object is not <code>null</code>.</li>
187 * <li>The specified object is an instance of <code>Collator</code>.</li>
188 * <li>The specified object has the same strength and decomposition
189 * settings as this object.</li>
192 * @param obj The <code>Object</code> to test for equality against
195 * @return <code>true</code> if the specified object is equal to
196 * this one, <code>false</code> otherwise.
198 public boolean equals (Object obj
)
200 if (! (obj
instanceof Collator
))
202 Collator c
= (Collator
) obj
;
203 return decmp
== c
.decmp
&& strength
== c
.strength
;
207 * This method tests whether the specified <code>String</code>'s are equal
208 * according to the collation rules for the locale of this object and
209 * the current strength and decomposition settings.
211 * @param str1 The first <code>String</code> to compare
212 * @param str2 The second <code>String</code> to compare
214 * @return <code>true</code> if the two strings are equal,
215 * <code>false</code> otherwise.
217 public boolean equals (String source
, String target
)
219 return compare (source
, target
) == 0;
223 * This method returns a copy of this <code>Collator</code> object.
225 * @return A duplicate of this object.
227 public Object
clone ()
231 return super.clone ();
233 catch (CloneNotSupportedException _
)
240 * This method returns an array of <code>Locale</code> objects which is
241 * the list of locales for which <code>Collator</code> objects exist.
243 * @return The list of locales for which <code>Collator</code>'s exist.
245 public static synchronized Locale
[] getAvailableLocales ()
248 Locale
[] l
= new Locale
[1];
254 * This method transforms the specified <code>String</code> into a
255 * <code>CollationKey</code> for faster comparisons. This is useful when
256 * comparisons against a string might be performed multiple times, such
257 * as during a sort operation.
259 * @param str The <code>String</code> to convert.
261 * @return A <code>CollationKey</code> for the specified <code>String</code>.
263 public abstract CollationKey
getCollationKey (String source
);
266 * This method returns the current decomposition setting for this
267 * object. This * will be one of NO_DECOMPOSITION,
268 * CANONICAL_DECOMPOSITION, or * FULL_DECOMPOSITION. See the
269 * documentation for those constants for an * explanation of this
272 * @return The current decomposition setting.
274 public synchronized int getDecomposition ()
280 * This method returns an instance of <code>Collator</code> for the
283 * @return A <code>Collator</code> for the default locale.
285 public static Collator
getInstance ()
287 return getInstance (Locale
.getDefault());
291 * This method returns an instance of <code>Collator</code> for the
292 * specified locale. If no <code>Collator</code> exists for the desired
293 * locale, a <code>Collator</code> for the default locale will be returned.
295 * @param locale The desired localed to load a <code>Collator</code> for.
297 * @return A <code>Collator</code> for the requested locale
299 public static Collator
getInstance (Locale loc
)
305 res
= ResourceBundle
.getBundle("gnu.java.locale.LocaleInformation",
306 loc
, ClassLoader
.getSystemClassLoader());
307 pattern
= res
.getString("collation_rules");
309 catch (MissingResourceException x
)
315 return new RuleBasedCollator (pattern
);
317 catch (ParseException x
)
324 * This method returns the current strength setting for this object. This
325 * will be one of PRIMARY, SECONDARY, TERTIARY, or IDENTICAL. See the
326 * documentation for those constants for an explanation of this setting.
328 * @return The current strength setting.
330 public synchronized int getStrength ()
336 * This method returns a hash code value for this object.
338 * @return A hash value for this object.
340 public abstract int hashCode ();
343 * This method sets the decomposition setting for this object to the
344 * specified value. This must be one of NO_DECOMPOSITION,
345 * CANONICAL_DECOMPOSITION, or FULL_DECOMPOSITION. Otherwise an
346 * exception will be thrown. See the documentation for those
347 * contants for an explanation of this setting.
349 * @param decmp The new decomposition setting.
351 * @exception IllegalArgumentException If the requested
352 * decomposition setting is not valid.
354 public synchronized void setDecomposition (int mode
)
356 if (mode
!= NO_DECOMPOSITION
357 && mode
!= CANONICAL_DECOMPOSITION
358 && mode
!= FULL_DECOMPOSITION
)
359 throw new IllegalArgumentException ();
364 * This method sets the strength setting for this object to the specified
365 * value. This must be one of PRIMARY, SECONDARY, TERTIARY, or IDENTICAL.
366 * Otherwise an exception is thrown. See the documentation for these
367 * constants for an explanation of this setting.
369 * @param strength The new strength setting.
371 * @exception IllegalArgumentException If the requested strength
372 * setting value is not valid.
374 public synchronized void setStrength (int strength
)
376 if (strength
!= PRIMARY
&& strength
!= SECONDARY
377 && strength
!= TERTIARY
&& strength
!= IDENTICAL
)
378 throw new IllegalArgumentException ();
379 this.strength
= strength
;
382 // Decompose a single character and append results to the buffer.
383 native final void decomposeCharacter (char c
, StringBuffer buf
);
386 * This is the current collation decomposition setting.
391 * This is the current collation strength setting.