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 LowercaseFirstComparer implements Comparator,
14: java.io.Serializable {
15:
16: private Collator baseCollator;
17:
18: public LowercaseFirstComparer(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 lowercase comes first.
26: * @return <0 if a<b, 0 if a=b, >0 if a>b
27: * @throws ClassCastException if the objects are of the wrong type for this Comparer
28: */
29:
30: public int compare(Object a, Object b) {
31:
32: int diff = baseCollator.compare(a, b);
33: if (diff != 0) {
34: return diff;
35: }
36: char[] a1 = ((String) a).toCharArray();
37: char[] b1 = ((String) b).toCharArray();
38: int alen = a1.length;
39: int blen = b1.length;
40: int i = 0;
41: int j = 0;
42:
43: while (true) {
44: if (i == alen)
45: return 0;
46: diff = a1[i++] - b1[j++];
47: if (diff != 0) {
48: return (Character.isLowerCase(a1[i - 1]) ? -1 : +1);
49: }
50: }
51:
52: }
53:
54: }
55:
56: //
57: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
58: // you may not use this file except in compliance with the License. You may obtain a copy of the
59: // License at http://www.mozilla.org/MPL/
60: //
61: // Software distributed under the License is distributed on an "AS IS" basis,
62: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
63: // See the License for the specific language governing rights and limitations under the License.
64: //
65: // The Original Code is: all this file.
66: //
67: // The Initial Developer of this module is Michael H. Kay.
68: //
69: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
70: //
71: // Contributor(s): none.
72: //
|