src/java.base/share/classes/java/text/CollationKey.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: 47216
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 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996 - 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
/**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    42
 * A {@code CollationKey} represents a {@code String} under the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    43
 * rules of a specific {@code Collator} object. Comparing two
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    44
 * {@code CollationKey}s returns the relative order of the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    45
 * {@code String}s they represent. Using {@code CollationKey}s
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    46
 * to compare {@code String}s is generally faster than using
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    47
 * {@code Collator.compare}. Thus, when the {@code String}s
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * must be compared multiple times, for example when sorting a list
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    49
 * of {@code String}s. It's more efficient to use {@code CollationKey}s.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    52
 * You can not create {@code CollationKey}s directly. Rather,
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    53
 * generate them by calling {@code Collator.getCollationKey}.
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    54
 * You can only compare {@code CollationKey}s generated from
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    55
 * the same {@code Collator} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    58
 * Generating a {@code CollationKey} for a {@code String}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    59
 * involves examining the entire {@code String}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * and converting it to series of bits that can be compared bitwise. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * allows fast comparisons once the keys are generated. The cost of generating
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    62
 * keys is recouped in faster comparisons when {@code String}s need
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * to be compared many times. On the other hand, the result of a comparison
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    64
 * is often determined by the first couple of characters of each {@code String}.
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    65
 * {@code Collator.compare} examines only as many characters as it needs which
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * allows it to be faster when doing single comparisons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    68
 * The following example shows how {@code CollationKey}s might be used
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    69
 * to sort a list of {@code String}s.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <blockquote>
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 5506
diff changeset
    71
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * // Create an array of CollationKeys for the Strings to be sorted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * Collator myCollator = Collator.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * CollationKey[] keys = new CollationKey[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * keys[0] = myCollator.getCollationKey("Tom");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * keys[1] = myCollator.getCollationKey("Dick");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * keys[2] = myCollator.getCollationKey("Harry");
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 5506
diff changeset
    78
 * sort(keys);
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 5506
diff changeset
    79
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * //...
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 5506
diff changeset
    81
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * // Inside body of sort routine, compare keys this way
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 5506
diff changeset
    83
 * if (keys[i].compareTo(keys[j]) > 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *    // swap keys[i] and keys[j]
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 5506
diff changeset
    85
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * //...
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 5506
diff changeset
    87
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * // Finally, when we've returned from sort.
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 5506
diff changeset
    89
 * System.out.println(keys[0].getSourceString());
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 5506
diff changeset
    90
 * System.out.println(keys[1].getSourceString());
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 5506
diff changeset
    91
 * System.out.println(keys[2].getSourceString());
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 5506
diff changeset
    92
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @see          Collator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * @see          RuleBasedCollator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @author       Helena Shih
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 32649
diff changeset
    98
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
public abstract class CollationKey implements Comparable<CollationKey> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Compare this CollationKey to the target CollationKey. The collation rules of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Collator object which created these keys are applied. <strong>Note:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * CollationKeys created by different Collators can not be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param target target CollationKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @return Returns an integer value. Value is less than zero if this is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * than target, value is zero if this and target are equal and value is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * zero if this is greater than target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @see java.text.Collator#compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   112
    public abstract int compareTo(CollationKey target);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Returns the String that this CollationKey represents.
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 5506
diff changeset
   116
     *
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 5506
diff changeset
   117
     * @return the source string of this CollationKey
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public String getSourceString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Converts the CollationKey to a sequence of bits. If two CollationKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * could be legitimately compared, then one could compare the byte arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * for each of those keys to obtain the same result.  Byte arrays are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * organized most significant byte first.
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 5506
diff changeset
   129
     *
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 5506
diff changeset
   130
     * @return a byte array representation of the CollationKey
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   132
    public abstract byte[] toByteArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
   * CollationKey constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
   *
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 5506
diff changeset
   138
   * @param source the source string
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   139
   * @throws    NullPointerException if {@code source} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
   * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    protected CollationKey(String source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (source==null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this.source = source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   149
    private final String source;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
}