KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > sort > UppercaseFirstComparer


1 package net.sf.saxon.sort;
2 import java.text.Collator JavaDoc;
3 import java.util.Comparator JavaDoc;
4
5 /**
6  * A Comparer used for comparing keys
7  *
8  * @author Michael H. Kay
9  *
10  */

11
12 public class UppercaseFirstComparer implements Comparator JavaDoc, java.io.Serializable JavaDoc {
13
14     private Collator JavaDoc baseCollator;
15
16     public UppercaseFirstComparer(Collator JavaDoc base) {
17         baseCollator = base;
18         baseCollator.setStrength(Collator.SECONDARY);
19     }
20
21     /**
22     * Compare two string objects: case is irrelevant, unless the strings are equal ignoring
23     * case, in which case uppercase comes first.
24     * @return <0 if a<b, 0 if a=b, >0 if a>b
25     * @throws ClassCastException if the objects do not implement the CharSequence interface
26     */

27
28     public int compare(Object JavaDoc a, Object JavaDoc b) {
29         int diff = baseCollator.compare(a, b);
30         if (diff != 0) {
31             return diff;
32         }
33
34         CharSequence JavaDoc a1 = (CharSequence JavaDoc)a;
35         CharSequence JavaDoc b1 = (CharSequence JavaDoc)b;
36         for (int i=0; i<a1.length(); i++) {
37             if (a1.charAt(i) != b1.charAt(i)) {
38                 return (Character.isUpperCase(a1.charAt(i)) ? -1 : +1);
39             }
40         }
41         return 0;
42
43
44 // char[] a1 = ((String)a).toCharArray();
45
// char[] b1 = ((String)b).toCharArray();
46
// int alen = a1.length;
47
// // the strings must be the same length, or we wouldn't have got this far
48
// int i = 0;
49
// int j = 0;
50
//
51
// while (i < alen) {
52
// if (i==alen) return 0;
53
// diff = a1[i++] - b1[j++];
54
// if (diff!=0) {
55
// return (Character.isUpperCase(a1[i-1]) ? -1 : +1);
56
// }
57
// }
58

59     }
60
61 }
62
63 //
64
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
65
// you may not use this file except in compliance with the License. You may obtain a copy of the
66
// License at http://www.mozilla.org/MPL/
67
//
68
// Software distributed under the License is distributed on an "AS IS" basis,
69
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
70
// See the License for the specific language governing rights and limitations under the License.
71
//
72
// The Original Code is: all this file.
73
//
74
// The Initial Developer of this module is Michael H. Kay.
75
//
76
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
77
//
78
// Contributor(s): none.
79
//
Popular Tags