Merged gcj-eclipse branch to trunk.
[official-gcc.git] / libjava / classpath / java / text / Collator.java
blob9523613244084473e64161d3b533480c1c747fc5
1 /* Collator.java -- Perform locale dependent String comparisons.
2 Copyright (C) 1998, 1999, 2000, 2001, 2004, 2005 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)
9 any later version.
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
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.
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. */
39 package java.text;
41 import gnu.java.locale.LocaleHelper;
43 import java.util.Comparator;
44 import java.util.Locale;
45 import java.util.MissingResourceException;
46 import java.util.ResourceBundle;
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.
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.
69 * @author Tom Tromey (tromey@cygnus.com)
70 * @author Aaron M. Renn (arenn@urbanophile.com)
71 * @date March 18, 1999
73 public abstract class Collator implements Comparator<Object>, Cloneable
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.
81 public static final int PRIMARY = 0;
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.
89 public static final int SECONDARY = 1;
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.
97 public static final int TERTIARY = 2;
99 /**
100 * This constant is a strength value which indicates that any difference
101 * at all between character values are considered significant.
103 public static final int IDENTICAL = 3;
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.
111 public static final int NO_DECOMPOSITION = 0;
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.
119 public static final int CANONICAL_DECOMPOSITION = 1;
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.
127 public static final int FULL_DECOMPOSITION = 2;
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.
137 protected Collator ()
139 strength = TERTIARY;
140 decmp = CANONICAL_DECOMPOSITION;
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.
151 * @param source The first object to compare
152 * @param target The second object to compare
154 * @return A negative integer if str1 &lt; str2, 0 if str1 == str2, or
155 * a positive integer if str1 &gt; str2.
157 public abstract int compare (String source, String target);
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.
165 * @param o1 The first object to compare
166 * @param o2 The second object to compare
168 * @return A negative integer if obj1 &lt; obj2, 0 if obj1 == obj2, or
169 * a positive integer if obj1 &gt; obj2.
171 * @exception ClassCastException If the arguments are not instances
172 * of <code>String</code>.
174 public int compare (Object o1, Object o2)
176 return compare ((String) o1, (String) o2);
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>
190 * @param obj The <code>Object</code> to test for equality against
191 * this object.
193 * @return <code>true</code> if the specified object is equal to
194 * this one, <code>false</code> otherwise.
196 public boolean equals (Object obj)
198 if (! (obj instanceof Collator))
199 return false;
200 Collator c = (Collator) obj;
201 return decmp == c.decmp && strength == c.strength;
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.
209 * @param source The first <code>String</code> to compare
210 * @param target The second <code>String</code> to compare
212 * @return <code>true</code> if the two strings are equal,
213 * <code>false</code> otherwise.
215 public boolean equals (String source, String target)
217 return compare (source, target) == 0;
221 * This method returns a copy of this <code>Collator</code> object.
223 * @return A duplicate of this object.
225 public Object clone ()
229 return super.clone ();
231 catch (CloneNotSupportedException _)
233 return null;
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.
241 * @return The list of locales for which <code>Collator</code>'s exist.
243 public static synchronized Locale[] getAvailableLocales ()
245 return LocaleHelper.getCollatorLocales();
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.
254 * @param source The <code>String</code> to convert.
256 * @return A <code>CollationKey</code> for the specified <code>String</code>.
258 public abstract CollationKey getCollationKey (String source);
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.
267 * @return The current decomposition setting.
269 public synchronized int getDecomposition ()
271 return decmp;
275 * This method returns an instance of <code>Collator</code> for the
276 * default locale.
278 * @return A <code>Collator</code> for the default locale.
280 public static Collator getInstance ()
282 return getInstance (Locale.getDefault());
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.
290 * @param loc The desired locale to load a <code>Collator</code> for.
292 * @return A <code>Collator</code> for the requested locale
294 public static Collator getInstance (Locale loc)
296 ResourceBundle res;
297 String pattern;
300 res = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
301 loc, ClassLoader.getSystemClassLoader());
302 pattern = res.getString("collation_rules");
304 catch (MissingResourceException x)
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";
311 return new RuleBasedCollator (pattern);
313 catch (ParseException x)
315 throw (InternalError)new InternalError().initCause(x);
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.
324 * @return The current strength setting.
326 public synchronized int getStrength ()
328 return strength;
332 * This method returns a hash code value for this object.
334 * @return A hash value for this object.
336 public abstract int hashCode ();
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.
345 * @param mode The new decomposition setting.
347 * @exception IllegalArgumentException If the requested
348 * decomposition setting is not valid.
350 public synchronized void setDecomposition (int mode)
352 if (mode != NO_DECOMPOSITION
353 && mode != CANONICAL_DECOMPOSITION
354 && mode != FULL_DECOMPOSITION)
355 throw new IllegalArgumentException ();
356 decmp = mode;
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.
365 * @param strength The new strength setting.
367 * @exception IllegalArgumentException If the requested strength
368 * setting value is not valid.
370 public synchronized void setStrength (int strength)
372 if (strength != PRIMARY && strength != SECONDARY
373 && strength != TERTIARY && strength != IDENTICAL)
374 throw new IllegalArgumentException ();
375 this.strength = strength;
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)
383 buf.append (c);
387 * This is the current collation decomposition setting.
389 int decmp;
392 * This is the current collation strength setting.
394 int strength;