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 android.icu.text.CollationKey key;
23
24    public CollationKeyICU(String source, android.icu.text.CollationKey key) {
25        super(source);
26        this.key = key;
27    }
28
29    @Override
30    public int compareTo(CollationKey other) {
31        final android.icu.text.CollationKey otherKey;
32        if (other instanceof CollationKeyICU) {
33            otherKey = ((CollationKeyICU) other).key;
34        } else {
35            otherKey = new android.icu.text.CollationKey(other.getSourceString(),
36                    other.toByteArray());
37        }
38
39        return key.compareTo(otherKey);
40    }
41
42    @Override
43    public boolean equals(Object object) {
44        if (object == this) {
45            return true;
46        }
47        if (!(object instanceof CollationKey)) {
48            return false;
49        }
50        return compareTo((CollationKey) object) == 0;
51    }
52
53    /**
54     * Creates a hash code for this CollationKey.
55     * Compute the hash by iterating sparsely over about 32 (up to 63) bytes
56     * spaced evenly through the string.  For each byte, multiply the previous
57     * hash value by a prime number and add the new byte in, like a linear
58     * congruential random number generator, producing a pseudo-random
59     * deterministic value well distributed over the output range.
60     *
61     * @return hash value of collation key. Hash value is never 0.
62     * @stable ICU 2.4
63     */
64    @Override
65    public int hashCode() {
66        return key.hashCode();
67    }
68
69    @Override
70    public byte[] toByteArray() {
71        return key.toByteArray();
72    }
73}
74