KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > text > RuleBasedCollationKey


1 /*
2  * @(#)RuleBasedCollationKey.java 1.2 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 /*
9  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
10  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
11  *
12  * The original version of this source code and documentation is copyrighted
13  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
14  * materials are provided under terms of a License Agreement between Taligent
15  * and Sun. This technology is protected by multiple US and International
16  * patents. This notice and attribution to Taligent may not be removed.
17  * Taligent is a registered trademark of Taligent, Inc.
18  *
19  */

20
21 package java.text;
22
23 /**
24  * A RuleBasedCollationKey is a concrete implementation of CollationKey class.
25  * The RuleBasedCollationKey class is used by the RuleBasedCollator class.
26  */

27
28 final class RuleBasedCollationKey extends CollationKey JavaDoc {
29     /**
30      * Compare this RuleBasedCollationKey to target. The collation rules of the
31      * Collator object which created these keys are applied. <strong>Note:</strong>
32      * RuleBasedCollationKeys created by different Collators can not be compared.
33      * @param target target RuleBasedCollationKey
34      * @return Returns an integer value. Value is less than zero if this is less
35      * than target, value is zero if this and target are equal and value is greater than
36      * zero if this is greater than target.
37      * @see java.text.Collator#compare
38      */

39     public int compareTo(CollationKey JavaDoc target)
40     {
41         int result = key.compareTo(((RuleBasedCollationKey JavaDoc)(target)).key);
42         if (result <= Collator.LESS)
43             return Collator.LESS;
44         else if (result >= Collator.GREATER)
45             return Collator.GREATER;
46         return Collator.EQUAL;
47     }
48
49     /**
50      * Compare this RuleBasedCollationKey and the target for equality.
51      * The collation rules of the Collator object which created these keys are applied.
52      * <strong>Note:</strong> RuleBasedCollationKeys created by different Collators can not be
53      * compared.
54      * @param target the RuleBasedCollationKey to compare to.
55      * @return Returns true if two objects are equal, false otherwise.
56      */

57     public boolean equals(Object JavaDoc target) {
58         if (this == target) return true;
59         if (target == null || !getClass().equals(target.getClass())) {
60             return false;
61         }
62         RuleBasedCollationKey JavaDoc other = (RuleBasedCollationKey JavaDoc)target;
63         return key.equals(other.key);
64     }
65
66     /**
67      * Creates a hash code for this RuleBasedCollationKey. The hash value is calculated on the
68      * key itself, not the String from which the key was created. Thus
69      * if x and y are RuleBasedCollationKeys, then x.hashCode(x) == y.hashCode() if
70      * x.equals(y) is true. This allows language-sensitive comparison in a hash table.
71      * See the CollatinKey class description for an example.
72      * @return the hash value based on the string's collation order.
73      */

74     public int hashCode() {
75         return (key.hashCode());
76     }
77
78     /**
79      * Converts the RuleBasedCollationKey to a sequence of bits. If two RuleBasedCollationKeys
80      * could be legitimately compared, then one could compare the byte arrays
81      * for each of those keys to obtain the same result. Byte arrays are
82      * organized most significant byte first.
83      */

84     public byte[] toByteArray() {
85
86         char[] src = key.toCharArray();
87         byte[] dest = new byte[ 2*src.length ];
88         int j = 0;
89         for( int i=0; i<src.length; i++ ) {
90             dest[j++] = (byte)(src[i] >>> 8);
91             dest[j++] = (byte)(src[i] & 0x00ff);
92         }
93         return dest;
94     }
95
96     /**
97      * A RuleBasedCollationKey can only be generated by Collator objects.
98      */

99     RuleBasedCollationKey(String JavaDoc source, String JavaDoc key) {
100     super(source);
101         this.key = key;
102     }
103     private String JavaDoc key = null;
104
105 }
106
107
108
109
Popular Tags