001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2007 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.cnd.utils.cache;
041:
042: import java.util.Comparator;
043:
044: /**
045: *
046: * @author Alexander Simon
047: */
048: public final class CharSequenceKey implements CharSequence,
049: Comparable<CharSequenceKey> {
050: private static final CharSequence EMPTY = create("");
051: public static final Comparator<CharSequence> Comparator = new CharSequenceComparator();
052: public static final Comparator<CharSequence> ComparatorIgnoreCase = new CharSequenceComparatorIgnoreCase();
053: private final Object value;
054: private int hash;
055:
056: public static CharSequence create(CharSequence s) {
057: if (s == null) {
058: return null;
059: }
060: //return s.toString();
061: if (s instanceof CharSequenceKey) {
062: return (CharSequenceKey) s;
063: } else if (s instanceof String) {
064: return new CharSequenceKey((String) s);
065: }
066: return new CharSequenceKey(s.toString());
067: }
068:
069: public static CharSequence empty() {
070: return EMPTY;
071: }
072:
073: private CharSequenceKey(String s) {
074: char[] v = s.toCharArray();
075: int n = v.length;
076: byte[] b = new byte[n];
077: for (int i = 0; i < n; i++) {
078: int o = v[i];
079: if ((o & 0xFF) != o) {
080: value = v;
081: return;
082: }
083: b[i] = (byte) o;
084: }
085: value = b;
086: }
087:
088: public int length() {
089: if (value instanceof byte[]) {
090: return ((byte[]) value).length;
091: }
092: return ((char[]) value).length;
093: }
094:
095: public char charAt(int index) {
096: if (value instanceof byte[]) {
097: int r = ((byte[]) value)[index] & 0xFF;
098: return (char) r;
099: }
100: return ((char[]) value)[index];
101: }
102:
103: @Override
104: public boolean equals(Object object) {
105: if (this == object) {
106: return true;
107: }
108: if (object instanceof CharSequenceKey) {
109: CharSequenceKey otherString = (CharSequenceKey) object;
110: int n = length();
111: if (n == otherString.length()) {
112: int i = 0;
113: while (n-- != 0) {
114: if (charAt(i) != otherString.charAt(i)) {
115: return false;
116: }
117: i++;
118: }
119: return true;
120: }
121: }
122: return false;
123: }
124:
125: @Override
126: public int hashCode() {
127: int h = hash;
128: if (h == 0) {
129: if (value instanceof byte[]) {
130: byte[] v = (byte[]) value;
131: int n = v.length;
132: for (int i = 0; i < n; i++) {
133: h = 31 * h + v[i];
134: }
135: } else {
136: char[] v = (char[]) value;
137: int n = v.length;
138: for (int i = 0; i < n; i++) {
139: h = 31 * h + v[i];
140: }
141: }
142: hash = h;
143: }
144: return h;
145: }
146:
147: public CharSequence subSequence(int beginIndex, int endIndex) {
148: return new CharSequenceKey(toString().substring(beginIndex,
149: endIndex));
150: }
151:
152: @Override
153: public String toString() {
154: if (value instanceof byte[]) {
155: byte[] v = (byte[]) value;
156: int n = v.length;
157: char[] r = new char[n];
158: for (int i = 0; i < n; i++) {
159: int c = v[i] & 0xFF;
160: r[i] = (char) c;
161: }
162: return new String(r);
163: }
164: char[] v = (char[]) value;
165: return new String(v);
166: }
167:
168: private static class CharSequenceComparator implements
169: Comparator<CharSequence> {
170: public int compare(CharSequence o1, CharSequence o2) {
171: int len1 = o1.length();
172: int len2 = o2.length();
173: int n = Math.min(len1, len2);
174: int k = 0;
175: while (k < n) {
176: char c1 = o1.charAt(k);
177: char c2 = o2.charAt(k);
178: if (c1 != c2) {
179: return c1 - c2;
180: }
181: k++;
182: }
183: return len1 - len2;
184: }
185: }
186:
187: public int compareTo(CharSequenceKey o) {
188: return Comparator.compare(this , o);
189: }
190:
191: private static class CharSequenceComparatorIgnoreCase implements
192: Comparator<CharSequence> {
193: public int compare(CharSequence o1, CharSequence o2) {
194: int n1 = o1.length();
195: int n2 = o2.length();
196: for (int i1 = 0, i2 = 0; i1 < n1 && i2 < n2; i1++, i2++) {
197: char c1 = o1.charAt(i1);
198: char c2 = o2.charAt(i2);
199: if (c1 != c2) {
200: c1 = Character.toUpperCase(c1);
201: c2 = Character.toUpperCase(c2);
202: if (c1 != c2) {
203: c1 = Character.toLowerCase(c1);
204: c2 = Character.toLowerCase(c2);
205: if (c1 != c2) {
206: return c1 - c2;
207: }
208: }
209: }
210: }
211: return n1 - n2;
212: }
213: }
214: }
|