CollationKeyICU.java revision 3df3bfcbad6f4da20964424c4772985d1b1586a1
1/**
2*******************************************************************************
3* Copyright (C) 1996-2005, International Business Machines Corporation and    *
4* others. All Rights Reserved.                                                *
5*******************************************************************************
6*
7*
8*******************************************************************************
9*/
10
11package libcore.icu;
12
13import java.text.CollationKey;
14
15/**
16 * A concrete implementation of the abstract java.text.CollationKey.
17 */
18public final class CollationKeyICU extends CollationKey {
19    /**
20     * The key.
21     */
22    private final com.ibm.icu.text.CollationKey key;
23
24    /**
25     * Cached hash value.
26     */
27    private int hashCode;
28
29    public CollationKeyICU(String source, com.ibm.icu.text.CollationKey key) {
30        super(source);
31        this.key = key;
32    }
33
34    @Override public int compareTo(CollationKey other) {
35        final com.ibm.icu.text.CollationKey otherKey;
36        if (other instanceof CollationKeyICU) {
37            otherKey = ((CollationKeyICU) other).key;
38        } else {
39            otherKey = new com.ibm.icu.text.CollationKey(other.getSourceString(),
40                    other.toByteArray());
41        }
42
43        return key.compareTo(otherKey);
44    }
45
46    @Override public boolean equals(Object object) {
47        if (object == this) {
48            return true;
49        }
50        if (!(object instanceof CollationKey)) {
51            return false;
52        }
53        return compareTo((CollationKey) object) == 0;
54    }
55
56    /**
57     * <p>Returns a hash code for this CollationKey. The hash value is calculated
58     * on the key itself, not the String from which the key was created. Thus
59     * if x and y are CollationKeys, then x.hashCode(x) == y.hashCode()
60     * if x.equals(y) is true. This allows language-sensitive comparison in a
61     * hash table.
62     * </p>
63     * @return the hash value.
64     * @stable ICU 2.8
65     */
66    @Override public int hashCode() {
67        return key.hashCode();
68    }
69
70    @Override public byte[] toByteArray() {
71        return key.toByteArray();
72    }
73}
74