Collator.java 15 KB

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