001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.text;
019:
020: /**
021: * CollationKey represents the collation order of a particular String for a
022: * specific Collator. CollationKeys can be compared to determine the relative
023: * ordering of their source Strings. This is useful when the Strings must be
024: * compared multiple times, as in sorting.
025: */
026: public final class CollationKey implements Comparable<CollationKey> {
027:
028: private String source;
029:
030: private com.ibm.icu.text.CollationKey icuKey;
031:
032: CollationKey(String source, com.ibm.icu.text.CollationKey key) {
033: this .source = source;
034: this .icuKey = key;
035: }
036:
037: /**
038: * Compare the receiver to the specified CollationKey to determine the
039: * relative ordering.
040: *
041: * @param value
042: * a CollationKey
043: * @return an int < 0 if this CollationKey is less than the specified
044: * CollationKey, 0 if they are equal, and > 0 if this CollationKey
045: * is greater
046: */
047: public int compareTo(CollationKey value) {
048: return icuKey.compareTo(value.icuKey);
049: }
050:
051: /**
052: * Compares the specified object to this CollationKey and answer if they are
053: * equal. The object must be an instance of CollationKey and have the same
054: * source string and collation key. The instances of CollationKey must have
055: * been created by the same Collator.
056: *
057: * @param object
058: * the object to compare with this object
059: * @return true if the specified object is equal to this CollationKey, false
060: * otherwise
061: *
062: * @see #hashCode
063: */
064: @Override
065: public boolean equals(Object object) {
066: if (!(object instanceof CollationKey)) {
067: return false;
068: }
069: CollationKey collationKey = (CollationKey) object;
070: return icuKey.equals(collationKey.icuKey);
071: }
072:
073: /**
074: * Answer the String from which this CollationKey was created.
075: *
076: * @return a String
077: */
078: public String getSourceString() {
079: return this .source;
080: }
081:
082: /**
083: * Answers an integer hash code for the receiver. Objects which are equal
084: * answer the same value for this method.
085: *
086: * @return the receiver's hash
087: *
088: * @see #equals
089: */
090: @Override
091: public int hashCode() {
092: return icuKey.hashCode();
093: }
094:
095: /**
096: * Answer the collation key as a byte array.
097: *
098: * @return an array of bytes
099: */
100: public byte[] toByteArray() {
101: return icuKey.toByteArray();
102: }
103: }
|