01: package net.sf.saxon.sort;
02:
03: import java.io.Serializable;
04: import java.util.Comparator;
05:
06: /**
07: * A collating sequence that uses Unicode codepoint ordering
08: */
09:
10: // NOTE: ideally, this class would be a subclass of java.text.Collator, so it
11: // could be used anywhere a Collator can be used. However, it doesn't appear to
12: // be possible to subclass java.text.Collator, because this would require implementing
13: // getCollationKey(), and the CollationKey() object does not have a public constructor.
14: public class CodepointCollator implements Comparator, Serializable {
15:
16: private static CodepointCollator theInstance = new CodepointCollator();
17:
18: public static CodepointCollator getInstance() {
19: return theInstance;
20: }
21:
22: /**
23: * Compare two string objects.
24: * @return <0 if a<b, 0 if a=b, >0 if a>b
25: * @throws ClassCastException if the objects are of the wrong type for this Comparer
26: */
27:
28: public int compare(Object a, Object b) {
29: return ((String) a).compareTo((String) b);
30: }
31:
32: /**
33: * Compare two CharSequence objects. This is hand-coded to avoid converting the objects into
34: * Strings.
35: * @return <0 if a<b, 0 if a=b, >0 if a>b
36: * @throws ClassCastException if the objects are of the wrong type for this Comparer
37: */
38:
39: public int compareCS(CharSequence a, CharSequence b) {
40: int alen = a.length();
41: int blen = b.length();
42: int i = 0;
43: int j = 0;
44: while (true) {
45: if (i == alen) {
46: if (j == blen) {
47: return 0;
48: } else {
49: return -1;
50: }
51: }
52: if (j == blen) {
53: return +1;
54: }
55: int c = (int) a.charAt(i++) - (int) b.charAt(j++);
56: if (c != 0) {
57: return c;
58: }
59: }
60: }
61:
62: }
63:
64: //
65: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
66: // you may not use this file except in compliance with the License. You may obtain a copy of the
67: // License at http://www.mozilla.org/MPL/
68: //
69: // Software distributed under the License is distributed on an "AS IS" basis,
70: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
71: // See the License for the specific language governing rights and limitations under the License.
72: //
73: // The Original Code is: all this file.
74: //
75: // The Initial Developer of the Original Code is Michael H. Kay
76: //
77: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
78: //
79: // Contributor(s): none
80: //
|