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