src/java.base/share/classes/java/text/RuleBasedCollator.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58288 48e480e56aad
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 51759
diff changeset
     2
 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *   The original version of this source code and documentation is copyrighted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * materials are provided under terms of a License Agreement between Taligent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * and Sun. This technology is protected by multiple US and International
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * patents. This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *   Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
package java.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.text.Normalizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    46
 * The {@code RuleBasedCollator} class is a concrete subclass of
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    47
 * {@code Collator} that provides a simple, data-driven, table
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * collator.  With this class you can create a customized table-based
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    49
 * {@code Collator}.  {@code RuleBasedCollator} maps
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * characters to sort keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    53
 * {@code RuleBasedCollator} has the following restrictions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * for efficiency (other subclasses may be used for more complex languages) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <li>If a special collation rule controlled by a &lt;modifier&gt; is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
      specified it applies to the whole collator object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <li>All non-mentioned characters are at the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *     collation order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * The collation table is composed of a list of collation rules, where each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * rule is of one of three forms:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *    &lt;modifier&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *    &lt;relation&gt; &lt;text-argument&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *    &lt;reset&gt; &lt;text-argument&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * The definitions of the rule elements is as follows:
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
    71
 * <UL>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *    <LI><strong>Text-Argument</strong>: A text-argument is any sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *        characters, excluding special characters (that is, common
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *        whitespace characters [0009-000D, 0020] and rule syntax characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *        [0021-002F, 003A-0040, 005B-0060, 007B-007E]). If those
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *        characters are desired, you can put them in single quotes
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
    77
 *        (e.g. ampersand =&gt; '&amp;'). Note that unquoted white space characters
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    78
 *        are ignored; e.g. {@code b c} is treated as {@code bc}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *    <LI><strong>Modifier</strong>: There are currently two modifiers that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *        turn on special collation rules.
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
    81
 *        <UL>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *            <LI>'@' : Turns on backwards sorting of accents (secondary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *                      differences), as in French.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *            <LI>'!' : Turns on Thai/Lao vowel-consonant swapping.  If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *                      rule is in force when a Thai vowel of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *                      &#92;U0E40-&#92;U0E44 precedes a Thai consonant of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *                      &#92;U0E01-&#92;U0E2E OR a Lao vowel of the range &#92;U0EC0-&#92;U0EC4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *                      precedes a Lao consonant of the range &#92;U0E81-&#92;U0EAE then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *                      the vowel is placed after the consonant for collation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *                      purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *        </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *        <p>'@' : Indicates that accents are sorted backwards, as in French.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *    <LI><strong>Relation</strong>: The relations are the following:
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
    94
 *        <UL>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *            <LI>'&lt;' : Greater, as a letter difference (primary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *            <LI>';' : Greater, as an accent difference (secondary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *            <LI>',' : Greater, as a case difference (tertiary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *            <LI>'=' : Equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *        </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *    <LI><strong>Reset</strong>: There is a single reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *        which is used primarily for contractions and expansions, but which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *        can also be used to add a modification at the end of a set of rules.
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
   103
 *        <p>'&amp;' : Indicates that the next rule follows the position to where
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *            the reset text-argument would be sorted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * This sounds more complicated than it is in practice. For example, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * following are equivalent ways of expressing the same thing:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * a &lt; b &lt; c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * a &lt; b &amp; b &lt; c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * a &lt; c &amp; a &lt; b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * Notice that the order is important, as the subsequent item goes immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * after the text-argument. The following are not equivalent:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * a &lt; b &amp; a &lt; c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * a &lt; c &amp; a &lt; b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * Either the text-argument must already be present in the sequence, or some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * initial substring of the text-argument must be present. (e.g. "a &lt; b &amp; ae &lt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * e" is valid since "a" is present in the sequence before "ae" is reset). In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * this latter case, "ae" is not entered and treated as a single character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * instead, "e" is sorted as if it were expanded to two characters: "a"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * followed by an "e". This difference appears in natural languages: in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * traditional Spanish "ch" is treated as though it contracts to a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * character (expressed as "c &lt; ch &lt; d"), while in traditional German
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * a-umlaut is treated as though it expanded to two characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * (expressed as "a,A &lt; b,B ... &amp;ae;&#92;u00e3&amp;AE;&#92;u00c3").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * [&#92;u00e3 and &#92;u00c3 are, of course, the escape sequences for a-umlaut.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * <strong>Ignorable Characters</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * For ignorable characters, the first rule must start with a relation (the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * examples we have used above are really fragments; "a &lt; b" really should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * "&lt; a &lt; b"). If, however, the first relation is not "&lt;", then all the all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * text-arguments up to the first "&lt;" are ignorable. For example, ", - &lt; a &lt; b"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * makes "-" an ignorable character, as we saw earlier in the word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * "black-birds". In the samples for different languages, you see that most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * accents are ignorable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <p><strong>Normalization and Accents</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   149
 * {@code RuleBasedCollator} automatically processes its rule table to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * include both pre-composed and combining-character versions of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * accented characters.  Even if the provided rule string contains only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * base characters and separate combining accent characters, the pre-composed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * accented characters matching all canonical combinations of characters from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * the rule string will be entered in the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * This allows you to use a RuleBasedCollator to compare accented strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * even when the collator is set to NO_DECOMPOSITION.  There are two caveats,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * however.  First, if the strings to be collated contain combining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * sequences that may not be in canonical order, you should set the collator to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * CANONICAL_DECOMPOSITION or FULL_DECOMPOSITION to enable sorting of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * combining sequences.  Second, if the strings contain characters with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * compatibility decompositions (such as full-width and half-width forms),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * you must use FULL_DECOMPOSITION, since the rule tables only include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * canonical mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * <p><strong>Errors</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * The following are errors:
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
   169
 * <UL>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *     <LI>A text-argument contains unquoted punctuation symbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *        (e.g. "a &lt; b-c &lt; d").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *     <LI>A relation or reset character not followed by a text-argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *        (e.g. "a &lt; ,b").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *     <LI>A reset where the text-argument (or an initial substring of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *         text-argument) is not already in the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *         (e.g. "a &lt; b &amp; e &lt; f")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * </UL>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   178
 * If you produce one of these errors, a {@code RuleBasedCollator} throws
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   179
 * a {@code ParseException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * <p><strong>Examples</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * <p>Simple:     "&lt; a &lt; b &lt; c &lt; d"
7511
54a6d9105e27 6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents: 6834
diff changeset
   183
 * <p>Norwegian:  "&lt; a, A &lt; b, B &lt; c, C &lt; d, D &lt; e, E &lt; f, F
54a6d9105e27 6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents: 6834
diff changeset
   184
 *                 &lt; g, G &lt; h, H &lt; i, I &lt; j, J &lt; k, K &lt; l, L
54a6d9105e27 6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents: 6834
diff changeset
   185
 *                 &lt; m, M &lt; n, N &lt; o, O &lt; p, P &lt; q, Q &lt; r, R
54a6d9105e27 6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents: 6834
diff changeset
   186
 *                 &lt; s, S &lt; t, T &lt; u, U &lt; v, V &lt; w, W &lt; x, X
54a6d9105e27 6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents: 6834
diff changeset
   187
 *                 &lt; y, Y &lt; z, Z
54a6d9105e27 6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents: 6834
diff changeset
   188
 *                 &lt; &#92;u00E6, &#92;u00C6
54a6d9105e27 6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents: 6834
diff changeset
   189
 *                 &lt; &#92;u00F8, &#92;u00D8
54a6d9105e27 6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents: 6834
diff changeset
   190
 *                 &lt; &#92;u00E5 = a&#92;u030A, &#92;u00C5 = A&#92;u030A;
54a6d9105e27 6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents: 6834
diff changeset
   191
 *                      aa, AA"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   194
 * To create a {@code RuleBasedCollator} object with specialized
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   195
 * rules tailored to your needs, you construct the {@code RuleBasedCollator}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   196
 * with the rules contained in a {@code String} object. For example:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * String simple = "&lt; a&lt; b&lt; c&lt; d";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * RuleBasedCollator mySimple = new RuleBasedCollator(simple);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * Or:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * <pre>
7511
54a6d9105e27 6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents: 6834
diff changeset
   206
 * String Norwegian = "&lt; a, A &lt; b, B &lt; c, C &lt; d, D &lt; e, E &lt; f, F &lt; g, G &lt; h, H &lt; i, I" +
54a6d9105e27 6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents: 6834
diff changeset
   207
 *                    "&lt; j, J &lt; k, K &lt; l, L &lt; m, M &lt; n, N &lt; o, O &lt; p, P &lt; q, Q &lt; r, R" +
54a6d9105e27 6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents: 6834
diff changeset
   208
 *                    "&lt; s, S &lt; t, T &lt; u, U &lt; v, V &lt; w, W &lt; x, X &lt; y, Y &lt; z, Z" +
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
   209
 *                    "&lt; &#92;u00E6, &#92;u00C6" +     // Latin letter ae &amp; AE
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
   210
 *                    "&lt; &#92;u00F8, &#92;u00D8" +     // Latin letter o &amp; O with stroke
7511
54a6d9105e27 6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents: 6834
diff changeset
   211
 *                    "&lt; &#92;u00E5 = a&#92;u030A," +  // Latin letter a with ring above
54a6d9105e27 6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents: 6834
diff changeset
   212
 *                    "  &#92;u00C5 = A&#92;u030A;" +  // Latin letter A with ring above
54a6d9105e27 6515695: [Col] java.text.RuleBasedCollator - JavaDoc "Examples" - Two bugs in sample code
peytoia
parents: 6834
diff changeset
   213
 *                    "  aa, AA";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * RuleBasedCollator myNorwegian = new RuleBasedCollator(Norwegian);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * A new collation rules string can be created by concatenating rules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * strings. For example, the rules returned by {@link #getRules()} could
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   221
 * be concatenated to combine multiple {@code RuleBasedCollator}s.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * The following example demonstrates how to change the order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * non-spacing accents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * // old rule
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * String oldRules = "=&#92;u0301;&#92;u0300;&#92;u0302;&#92;u0308"    // main accents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 *                 + ";&#92;u0327;&#92;u0303;&#92;u0304;&#92;u0305"    // main accents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 *                 + ";&#92;u0306;&#92;u0307;&#92;u0309;&#92;u030A"    // main accents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 *                 + ";&#92;u030B;&#92;u030C;&#92;u030D;&#92;u030E"    // main accents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 *                 + ";&#92;u030F;&#92;u0310;&#92;u0311;&#92;u0312"    // main accents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 *                 + "&lt; a , A ; ae, AE ; &#92;u00e6 , &#92;u00c6"
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
   235
 *                 + "&lt; b , B &lt; c, C &lt; e, E &amp; C &lt; d, D";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 * // change the order of accent characters
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
   237
 * String addOn = "&amp; &#92;u0300 ; &#92;u0308 ; &#92;u0302";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 * RuleBasedCollator myCollator = new RuleBasedCollator(oldRules + addOn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * @see        Collator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * @see        CollationElementIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 * @author     Helena Shih, Laura Werner, Richard Gillam
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 32649
diff changeset
   245
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
public class RuleBasedCollator extends Collator{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    // IMPLEMENTATION NOTES:  The implementation of the collation algorithm is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    // divided across three classes: RuleBasedCollator, RBCollationTables, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    // CollationElementIterator.  RuleBasedCollator contains the collator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    // transient state and includes the code that uses the other classes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    // implement comparison and sort-key building.  RuleBasedCollator also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    // contains the logic to handle French secondary accent sorting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    // A RuleBasedCollator has two CollationElementIterators.  State doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    // need to be preserved in these objects between calls to compare() or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    // getCollationKey(), but the objects persist anyway to avoid wasting extra
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    // creation time.  compare() and getCollationKey() are synchronized to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    // thread safety with this scheme.  The CollationElementIterator is responsible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    // for generating collation elements from strings and returning one element at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    // a time (sometimes there's a one-to-many or many-to-one mapping between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    // characters and collation elements-- this class handles that).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    // CollationElementIterator depends on RBCollationTables, which contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    // collator's static state.  RBCollationTables contains the actual data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    // tables specifying the collation order of characters for a particular locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    // or use.  It also contains the base logic that CollationElementIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    // uses to map from characters to collation elements.  A single RBCollationTables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    // object is shared among all RuleBasedCollators for the same locale, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    // thus by all the CollationElementIterators they create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * RuleBasedCollator constructor.  This takes the table rules and builds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * a collation table out of them.  Please see RuleBasedCollator class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * description for more details on the collation rule syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @see java.util.Locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param rules the collation rules to build the collation table from.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 51759
diff changeset
   276
     * @throws    ParseException A format exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * will be thrown if the build process of the rules fails. For
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
   278
     * example, build rule "a &lt; ? &lt; d" will cause the constructor to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * throw the ParseException because the '?' is not quoted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public RuleBasedCollator(String rules) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        this(rules, Collator.CANONICAL_DECOMPOSITION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * RuleBasedCollator constructor.  This takes the table rules and builds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * a collation table out of them.  Please see RuleBasedCollator class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * description for more details on the collation rule syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @see java.util.Locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param rules the collation rules to build the collation table from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param decomp the decomposition strength used to build the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * collation table and to perform comparisons.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 51759
diff changeset
   293
     * @throws    ParseException A format exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * will be thrown if the build process of the rules fails. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * example, build rule "a < ? < d" will cause the constructor to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * throw the ParseException because the '?' is not quoted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    RuleBasedCollator(String rules, int decomp) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        setStrength(Collator.TERTIARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        setDecomposition(decomp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        tables = new RBCollationTables(rules, decomp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * "Copy constructor."  Used in clone() for performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    private RuleBasedCollator(RuleBasedCollator that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        setStrength(that.getStrength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        setDecomposition(that.getDecomposition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        tables = that.tables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Gets the table-based rules for the collation object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @return returns the collation rules that the table collation object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * was created from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public String getRules()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return tables.getRules();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
   324
     * Returns a CollationElementIterator for the given String.
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
   325
     *
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
   326
     * @param source the string to be collated
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
   327
     * @return a {@code CollationElementIterator} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @see java.text.CollationElementIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public CollationElementIterator getCollationElementIterator(String source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return new CollationElementIterator( source, this );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
   335
     * Returns a CollationElementIterator for the given CharacterIterator.
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
   336
     *
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
   337
     * @param source the character iterator to be collated
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 7511
diff changeset
   338
     * @return a {@code CollationElementIterator} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @see java.text.CollationElementIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public CollationElementIterator getCollationElementIterator(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                                CharacterIterator source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return new CollationElementIterator( source, this );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Compares the character data stored in two different strings based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * collation rules.  Returns information about whether a string is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * than, greater than or equal to another string in a language.
51759
ac6e9a2ebc04 8210786: Typo s/overriden/overridden/ in several places
igerasim
parents: 47216
diff changeset
   351
     * This can be overridden in a subclass.
6834
303c8cca4a31 6970930: RuleBasedCollator.compare(String,null) throws IAE (should be NPE)
peytoia
parents: 5506
diff changeset
   352
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   353
     * @throws    NullPointerException if {@code source} or {@code target} is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public synchronized int compare(String source, String target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    {
6834
303c8cca4a31 6970930: RuleBasedCollator.compare(String,null) throws IAE (should be NPE)
peytoia
parents: 5506
diff changeset
   357
        if (source == null || target == null) {
303c8cca4a31 6970930: RuleBasedCollator.compare(String,null) throws IAE (should be NPE)
peytoia
parents: 5506
diff changeset
   358
            throw new NullPointerException();
303c8cca4a31 6970930: RuleBasedCollator.compare(String,null) throws IAE (should be NPE)
peytoia
parents: 5506
diff changeset
   359
        }
303c8cca4a31 6970930: RuleBasedCollator.compare(String,null) throws IAE (should be NPE)
peytoia
parents: 5506
diff changeset
   360
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        // The basic algorithm here is that we use CollationElementIterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        // to step through both the source and target strings.  We compare each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        // collation element in the source string against the corresponding one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        // in the target, checking for differences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        // If a difference is found, we set <result> to LESS or GREATER to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        // indicate whether the source string is less or greater than the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        // However, it's not that simple.  If we find a tertiary difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        // (e.g. 'A' vs. 'a') near the beginning of a string, it can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        // overridden by a primary difference (e.g. "A" vs. "B") later in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        // the string.  For example, "AA" < "aB", even though 'A' > 'a'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        // To keep track of this, we use strengthResult to keep track of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        // strength of the most significant difference that has been found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        // so far.  When we find a difference whose strength is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        // strengthResult, it overrides the last difference (if any) that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        // was found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        int result = Collator.EQUAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (sourceCursor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            sourceCursor = getCollationElementIterator(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            sourceCursor.setText(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (targetCursor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            targetCursor = getCollationElementIterator(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            targetCursor.setText(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        int sOrder = 0, tOrder = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        boolean initialCheckSecTer = getStrength() >= Collator.SECONDARY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        boolean checkSecTer = initialCheckSecTer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        boolean checkTertiary = getStrength() >= Collator.TERTIARY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        boolean gets = true, gett = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        while(true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            // Get the next collation element in each of the strings, unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            // we've been requested to skip it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            if (gets) sOrder = sourceCursor.next(); else gets = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (gett) tOrder = targetCursor.next(); else gett = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            // If we've hit the end of one of the strings, jump out of the loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            if ((sOrder == CollationElementIterator.NULLORDER)||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                (tOrder == CollationElementIterator.NULLORDER))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            int pSOrder = CollationElementIterator.primaryOrder(sOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            int pTOrder = CollationElementIterator.primaryOrder(tOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            // If there's no difference at this position, we can skip it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (sOrder == tOrder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                if (tables.isFrenchSec() && pSOrder != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    if (!checkSecTer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        // in french, a secondary difference more to the right is stronger,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        // so accents have to be checked with each base element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        checkSecTer = initialCheckSecTer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                        // but tertiary differences are less important than the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        // secondary difference, so checking tertiary remains disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        checkTertiary = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            // Compare primary differences first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            if ( pSOrder != pTOrder )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                if (sOrder == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    // The entire source element is ignorable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    // Skip to the next source element, but don't fetch another target element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    gett = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                if (tOrder == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    gets = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                // The source and target elements aren't ignorable, but it's still possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                // for the primary component of one of the elements to be ignorable....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                if (pSOrder == 0)  // primary order in source is ignorable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    // The source's primary is ignorable, but the target's isn't.  We treat ignorables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    // as a secondary difference, so remember that we found one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    if (checkSecTer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        result = Collator.GREATER;  // (strength is SECONDARY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        checkSecTer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    // Skip to the next source element, but don't fetch another target element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    gett = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                else if (pTOrder == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    // record differences - see the comment above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    if (checkSecTer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        result = Collator.LESS;  // (strength is SECONDARY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                        checkSecTer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    // Skip to the next source element, but don't fetch another target element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    gets = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    // Neither of the orders is ignorable, and we already know that the primary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    // orders are different because of the (pSOrder != pTOrder) test above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    // Record the difference and stop the comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    if (pSOrder < pTOrder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                        return Collator.LESS;  // (strength is PRIMARY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        return Collator.GREATER;  // (strength is PRIMARY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            } else { // else of if ( pSOrder != pTOrder )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                // primary order is the same, but complete order is different. So there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                // are no base elements at this point, only ignorables (Since the strings are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                // normalized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                if (checkSecTer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    // a secondary or tertiary difference may still matter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    short secSOrder = CollationElementIterator.secondaryOrder(sOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    short secTOrder = CollationElementIterator.secondaryOrder(tOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    if (secSOrder != secTOrder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                        // there is a secondary difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                        result = (secSOrder < secTOrder) ? Collator.LESS : Collator.GREATER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                                                // (strength is SECONDARY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                        checkSecTer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        // (even in french, only the first secondary difference within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                        //  a base character matters)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                        if (checkTertiary) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                            // a tertiary difference may still matter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                            short terSOrder = CollationElementIterator.tertiaryOrder(sOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                            short terTOrder = CollationElementIterator.tertiaryOrder(tOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                            if (terSOrder != terTOrder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                                // there is a tertiary difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                                result = (terSOrder < terTOrder) ? Collator.LESS : Collator.GREATER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                                                // (strength is TERTIARY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                                checkTertiary = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                } // if (checkSecTer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            }  // if ( pSOrder != pTOrder )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        } // while()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        if (sOrder != CollationElementIterator.NULLORDER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            // (tOrder must be CollationElementIterator::NULLORDER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            //  since this point is only reached when sOrder or tOrder is NULLORDER.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            // The source string has more elements, but the target string hasn't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                if (CollationElementIterator.primaryOrder(sOrder) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    // We found an additional non-ignorable base character in the source string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    // This is a primary difference, so the source is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    return Collator.GREATER; // (strength is PRIMARY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                else if (CollationElementIterator.secondaryOrder(sOrder) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    // Additional secondary elements mean the source string is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    if (checkSecTer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        result = Collator.GREATER;  // (strength is SECONDARY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                        checkSecTer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            } while ((sOrder = sourceCursor.next()) != CollationElementIterator.NULLORDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        else if (tOrder != CollationElementIterator.NULLORDER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            // The target string has more elements, but the source string hasn't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                if (CollationElementIterator.primaryOrder(tOrder) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    // We found an additional non-ignorable base character in the target string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    // This is a primary difference, so the source is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    return Collator.LESS; // (strength is PRIMARY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                else if (CollationElementIterator.secondaryOrder(tOrder) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    // Additional secondary elements in the target mean the source string is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    if (checkSecTer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                        result = Collator.LESS;  // (strength is SECONDARY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        checkSecTer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            } while ((tOrder = targetCursor.next()) != CollationElementIterator.NULLORDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        // For IDENTICAL comparisons, we use a bitwise character comparison
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        // as a tiebreaker if all else is equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        if (result == 0 && getStrength() == IDENTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            int mode = getDecomposition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            Normalizer.Form form;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            if (mode == CANONICAL_DECOMPOSITION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                form = Normalizer.Form.NFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            } else if (mode == FULL_DECOMPOSITION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                form = Normalizer.Form.NFKD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                return source.compareTo(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            String sourceDecomposition = Normalizer.normalize(source, form);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            String targetDecomposition = Normalizer.normalize(target, form);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            return sourceDecomposition.compareTo(targetDecomposition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Transforms the string into a series of characters that can be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * with CollationKey.compareTo. This overrides java.text.Collator.getCollationKey.
51759
ac6e9a2ebc04 8210786: Typo s/overriden/overridden/ in several places
igerasim
parents: 47216
diff changeset
   570
     * It can be overridden in a subclass.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    public synchronized CollationKey getCollationKey(String source)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        // The basic algorithm here is to find all of the collation elements for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        // character in the source string, convert them to a char representation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        // and put them into the collation key.  But it's trickier than that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        // Each collation element in a string has three components: primary (A vs B),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        // secondary (A vs A-acute), and tertiary (A' vs a); and a primary difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        // at the end of a string takes precedence over a secondary or tertiary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        // difference earlier in the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        // To account for this, we put all of the primary orders at the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        // string, followed by the secondary and tertiary orders, separated by nulls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        // Here's a hypothetical example, with the collation element represented as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        // a three-digit number, one digit for primary, one for secondary, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        // String:              A     a     B   \u00e9 <--(e-acute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        // Collation Elements: 101   100   201  510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        // Collation Key:      1125<null>0001<null>1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        // To make things even trickier, secondary differences (accent marks) are compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        // starting at the *end* of the string in languages with French secondary ordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        // But when comparing the accent marks on a single base character, they are compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        // from the beginning.  To handle this, we reverse all of the accents that belong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        // to each base character, then we reverse the entire string of secondary orderings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        // at the end.  Taking the same example above, a French collator might return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        // this instead:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        // Collation Key:      1125<null>1000<null>1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if (source == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        if (primResult == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            primResult = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            secResult = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            terResult = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            primResult.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            secResult.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            terResult.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        int order = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        boolean compareSec = (getStrength() >= Collator.SECONDARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        boolean compareTer = (getStrength() >= Collator.TERTIARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        int secOrder = CollationElementIterator.NULLORDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        int terOrder = CollationElementIterator.NULLORDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        int preSecIgnore = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (sourceCursor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            sourceCursor = getCollationElementIterator(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            sourceCursor.setText(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        // walk through each character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        while ((order = sourceCursor.next()) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
               CollationElementIterator.NULLORDER)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            secOrder = CollationElementIterator.secondaryOrder(order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            terOrder = CollationElementIterator.tertiaryOrder(order);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            if (!CollationElementIterator.isIgnorable(order))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                primResult.append((char) (CollationElementIterator.primaryOrder(order)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                                    + COLLATIONKEYOFFSET));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                if (compareSec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    // accumulate all of the ignorable/secondary characters attached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    // to a given base character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    if (tables.isFrenchSec() && preSecIgnore < secResult.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                        // We're doing reversed secondary ordering and we've hit a base
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                        // (non-ignorable) character.  Reverse any secondary orderings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                        // that applied to the last base character.  (see block comment above.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                        RBCollationTables.reverse(secResult, preSecIgnore, secResult.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    // Remember where we are in the secondary orderings - this is how far
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    // back to go if we need to reverse them later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    secResult.append((char)(secOrder+ COLLATIONKEYOFFSET));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    preSecIgnore = secResult.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                if (compareTer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    terResult.append((char)(terOrder+ COLLATIONKEYOFFSET));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                if (compareSec && secOrder != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    secResult.append((char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                        (secOrder + tables.getMaxSecOrder() + COLLATIONKEYOFFSET));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                if (compareTer && terOrder != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                    terResult.append((char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                        (terOrder + tables.getMaxTerOrder() + COLLATIONKEYOFFSET));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (tables.isFrenchSec())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            if (preSecIgnore < secResult.length()) {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19054
diff changeset
   675
                // If we've accumulated any secondary characters after the last base character,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                // reverse them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                RBCollationTables.reverse(secResult, preSecIgnore, secResult.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            // And now reverse the entire secResult to get French secondary ordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            RBCollationTables.reverse(secResult, 0, secResult.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        primResult.append((char)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        secResult.append((char)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        secResult.append(terResult.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        primResult.append(secResult.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        if (getStrength() == IDENTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            primResult.append((char)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            int mode = getDecomposition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            if (mode == CANONICAL_DECOMPOSITION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                primResult.append(Normalizer.normalize(source, Normalizer.Form.NFD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            } else if (mode == FULL_DECOMPOSITION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                primResult.append(Normalizer.normalize(source, Normalizer.Form.NFKD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                primResult.append(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        return new RuleBasedCollationKey(source, primResult.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Standard override; no change in semantics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        // if we know we're not actually a subclass of RuleBasedCollator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        // (this class really should have been made final), bypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        // Object.clone() and use our "copy constructor".  This is faster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        if (getClass() == RuleBasedCollator.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            return new RuleBasedCollator(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            RuleBasedCollator result = (RuleBasedCollator) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            result.primResult = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            result.secResult = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            result.terResult = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            result.sourceCursor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            result.targetCursor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * Compares the equality of two collation objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @param obj the table-based collation object to be compared with this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @return true if the current table-based collation object is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * as the table-based collation object obj; false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        if (obj == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (!super.equals(obj)) return false;  // super does class check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        RuleBasedCollator other = (RuleBasedCollator) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        // all other non-transient information is also contained in rules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        return (getRules().equals(other.getRules()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * Generates the hash code for the table-based collation object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        return getRules().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * Allows CollationElementIterator access to the tables object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    RBCollationTables getTables() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        return tables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    // ==============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    // private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    // ==============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   754
    static final int CHARINDEX = 0x70000000;  // need look up in .commit()
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   755
    static final int EXPANDCHARINDEX = 0x7E000000; // Expand index follows
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   756
    static final int CONTRACTCHARINDEX = 0x7F000000;  // contract indexes follow
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   757
    static final int UNMAPPED = 0xFFFFFFFF;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   759
    private static final int COLLATIONKEYOFFSET = 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    private RBCollationTables tables = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    // Internal objects that are cached across calls so that they don't have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    // be created/destroyed on every call to compare() and getCollationKey()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    private StringBuffer primResult = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    private StringBuffer secResult = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    private StringBuffer terResult = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    private CollationElementIterator sourceCursor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    private CollationElementIterator targetCursor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
}