KickJava   Java API By Example, From Geeks To Geeks.

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


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