Collator.java 14 KB

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