blob: 94d3dd79e70cae1629df2421729fa03af049f952 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26/*
27 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
28 * (C) Copyright IBM Corp. 1996 - All Rights Reserved
29 *
30 * The original version of this source code and documentation is copyrighted
31 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
32 * materials are provided under terms of a License Agreement between Taligent
33 * and Sun. This technology is protected by multiple US and International
34 * patents. This notice and attribution to Taligent may not be removed.
35 * Taligent is a registered trademark of Taligent, Inc.
36 *
37 */
38
39package java.text;
40
41/**
42 * A RuleBasedCollationKey is a concrete implementation of CollationKey class.
43 * The RuleBasedCollationKey class is used by the RuleBasedCollator class.
44 */
45
46final class RuleBasedCollationKey extends CollationKey {
47 /**
48 * Compare this RuleBasedCollationKey to target. The collation rules of the
49 * Collator object which created these keys are applied. <strong>Note:</strong>
50 * RuleBasedCollationKeys created by different Collators can not be compared.
51 * @param target target RuleBasedCollationKey
52 * @return Returns an integer value. Value is less than zero if this is less
53 * than target, value is zero if this and target are equal and value is greater than
54 * zero if this is greater than target.
55 * @see java.text.Collator#compare
56 */
57 public int compareTo(CollationKey target)
58 {
59 int result = key.compareTo(((RuleBasedCollationKey)(target)).key);
60 if (result <= Collator.LESS)
61 return Collator.LESS;
62 else if (result >= Collator.GREATER)
63 return Collator.GREATER;
64 return Collator.EQUAL;
65 }
66
67 /**
68 * Compare this RuleBasedCollationKey and the target for equality.
69 * The collation rules of the Collator object which created these keys are applied.
70 * <strong>Note:</strong> RuleBasedCollationKeys created by different Collators can not be
71 * compared.
72 * @param target the RuleBasedCollationKey to compare to.
73 * @return Returns true if two objects are equal, false otherwise.
74 */
75 public boolean equals(Object target) {
76 if (this == target) return true;
77 if (target == null || !getClass().equals(target.getClass())) {
78 return false;
79 }
80 RuleBasedCollationKey other = (RuleBasedCollationKey)target;
81 return key.equals(other.key);
82 }
83
84 /**
85 * Creates a hash code for this RuleBasedCollationKey. The hash value is calculated on the
86 * key itself, not the String from which the key was created. Thus
87 * if x and y are RuleBasedCollationKeys, then x.hashCode(x) == y.hashCode() if
88 * x.equals(y) is true. This allows language-sensitive comparison in a hash table.
89 * See the CollatinKey class description for an example.
90 * @return the hash value based on the string's collation order.
91 */
92 public int hashCode() {
93 return (key.hashCode());
94 }
95
96 /**
97 * Converts the RuleBasedCollationKey to a sequence of bits. If two RuleBasedCollationKeys
98 * could be legitimately compared, then one could compare the byte arrays
99 * for each of those keys to obtain the same result. Byte arrays are
100 * organized most significant byte first.
101 */
102 public byte[] toByteArray() {
103
104 char[] src = key.toCharArray();
105 byte[] dest = new byte[ 2*src.length ];
106 int j = 0;
107 for( int i=0; i<src.length; i++ ) {
108 dest[j++] = (byte)(src[i] >>> 8);
109 dest[j++] = (byte)(src[i] & 0x00ff);
110 }
111 return dest;
112 }
113
114 /**
115 * A RuleBasedCollationKey can only be generated by Collator objects.
116 */
117 RuleBasedCollationKey(String source, String key) {
118 super(source);
119 this.key = key;
120 }
121 private String key = null;
122
123}