KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > text > RawCollationKey


1 /**
2  *******************************************************************************
3  * Copyright (C) 1996-2006, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7 package com.ibm.icu.text;
8
9 import com.ibm.icu.util.ByteArrayWrapper;
10
11 /**
12  * <p>
13  * Simple class wrapper to store the internal byte representation of a
14  * CollationKey. Unlike the CollationKey, this class do not contain information
15  * on the source string the sort order represents. RawCollationKey is mutable
16  * and users can reuse its objects with the method in
17  * RuleBasedCollator.getRawCollationKey(..).
18  * </p>
19  * <p>
20  * Please refer to the documentation on CollationKey for a detail description
21  * on the internal byte representation. Note the internal byte representation
22  * is always null-terminated.
23  * </p>
24  * <code>
25  * Example of use:<br>
26  * String str[] = {.....};
27  * RuleBasedCollator collator = (RuleBasedCollator)Collator.getInstance();
28  * RawCollationKey key = new RawCollationKey(128);
29  * for (int i = 0; i &lt; str.length; i ++) {
30  * collator.getRawCollationKey(str[i], key);
31  * // do something with key.bytes
32  * }
33  * </code>
34  * <p><strong>Note:</strong> Comparison between RawCollationKeys created by
35  * different Collators might return incorrect results.
36  * See class documentation for Collator.</p>
37  * @stable ICU 2.8
38  * @see RuleBasedCollator
39  * @see CollationKey
40  */

41 public final class RawCollationKey extends ByteArrayWrapper
42 {
43     // public constructors --------------------------------------------------
44

45     /**
46      * Default constructor, internal byte array is null and its size set to 0.
47      * @stable ICU 2.8
48      */

49     public RawCollationKey()
50     {
51     }
52
53     /**
54      * RawCollationKey created with an empty internal byte array of length
55      * capacity. Size of the internal byte array will be set to 0.
56      * @param capacity length of internal byte array
57      * @stable ICU 2.8
58      */

59     public RawCollationKey(int capacity)
60     {
61         bytes = new byte[capacity];
62     }
63
64     /**
65      * RawCollationKey created, adopting bytes as the internal byte array.
66      * Size of the internal byte array will be set to 0.
67      * @param bytes byte array to be adopted by RawCollationKey
68      * @stable ICU 2.8
69      */

70     public RawCollationKey(byte[] bytes)
71     {
72         this.bytes = bytes;
73     }
74     
75     /**
76      * Construct a RawCollationKey from a byte array and size.
77      * @param bytesToAdopt the byte array to adopt
78      * @param size the length of valid data in the byte array
79      * @throws IndexOutOfBoundsException if bytesToAdopt == null and size != 0, or
80      * size < 0, or size > bytesToAdopt.length.
81      * @stable ICU 2.8
82      */

83     public RawCollationKey(byte[] bytesToAdopt, int size)
84     {
85         super(bytesToAdopt, size);
86     }
87
88     /**
89      * Compare this RawCollationKey to another, which must not be null. This overrides
90      * the inherited implementation to ensure the returned values are -1, 0, or 1.
91      * @param rhs the RawCollationKey to compare to.
92      * @return -1, 0, or 1 as this compares less than, equal to, or
93      * greater than rhs.
94      * @throws ClassCastException if the other object is not a RawCollationKey.
95      * @draft ICU 3.2
96      * @provisional This API might change or be removed in a future release.
97      */

98     public int compareTo(Object JavaDoc rhs) {
99         int result = super.compareTo((RawCollationKey)rhs);
100         return result < 0 ? -1 : result == 0 ? 0 : 1;
101     }
102 }
103
Popular Tags