01: package net.sf.saxon.sort;
02:
03: import java.text.Collator;
04: import java.util.Comparator;
05:
06: /**
07: * A Comparer used for comparing keys
08: *
09: * @author Michael H. Kay
10: *
11: */
12:
13: public class UppercaseFirstComparer implements Comparator,
14: java.io.Serializable {
15:
16: private Collator baseCollator;
17:
18: public UppercaseFirstComparer(Collator base) {
19: baseCollator = base;
20: baseCollator.setStrength(Collator.SECONDARY);
21: }
22:
23: /**
24: * Compare two string objects: case is irrelevant, unless the strings are equal ignoring
25: * case, in which case uppercase comes first.
26: * @return <0 if a<b, 0 if a=b, >0 if a>b
27: * @throws ClassCastException if the objects do not implement the CharSequence interface
28: */
29:
30: public int compare(Object a, Object b) {
31: int diff = baseCollator.compare(a, b);
32: if (diff != 0) {
33: return diff;
34: }
35:
36: CharSequence a1 = (CharSequence) a;
37: CharSequence b1 = (CharSequence) b;
38: for (int i = 0; i < a1.length(); i++) {
39: if (a1.charAt(i) != b1.charAt(i)) {
40: return (Character.isUpperCase(a1.charAt(i)) ? -1 : +1);
41: }
42: }
43: return 0;
44:
45: // char[] a1 = ((String)a).toCharArray();
46: // char[] b1 = ((String)b).toCharArray();
47: // int alen = a1.length;
48: // // the strings must be the same length, or we wouldn't have got this far
49: // int i = 0;
50: // int j = 0;
51: //
52: // while (i < alen) {
53: // if (i==alen) return 0;
54: // diff = a1[i++] - b1[j++];
55: // if (diff!=0) {
56: // return (Character.isUpperCase(a1[i-1]) ? -1 : +1);
57: // }
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 this module 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: //
|