KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.sort;
2 import java.io.Serializable JavaDoc;
3 import java.util.Comparator JavaDoc;
4
5
6 /**
7  * A collating sequence that uses Unicode codepoint ordering
8  */

9
10
11 // NOTE: ideally, this class would be a subclass of java.text.Collator, so it
12
// could be used anywhere a Collator can be used. However, it doesn't appear to
13
// be possible to subclass java.text.Collator, because this would require implementing
14
// getCollationKey(), and the CollationKey() object does not have a public constructor.
15

16 public class CodepointCollator implements Comparator JavaDoc, Serializable JavaDoc {
17
18     private static CodepointCollator theInstance = new CodepointCollator();
19
20     public static CodepointCollator getInstance() {
21         return theInstance;
22     }
23
24     /**
25     * Compare two string objects.
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 JavaDoc a, Object JavaDoc b) {
31         return ((String JavaDoc)a).compareTo((String JavaDoc)b);
32     }
33
34     /**
35      * Compare two CharSequence objects. This is hand-coded to avoid converting the objects into
36      * Strings.
37      * @return <0 if a<b, 0 if a=b, >0 if a>b
38      * @throws ClassCastException if the objects are of the wrong type for this Comparer
39      */

40
41     public int compareCS(CharSequence JavaDoc a, CharSequence JavaDoc b) {
42         int alen = a.length();
43         int blen = b.length();
44         int i = 0;
45         int j = 0;
46         while (true) {
47             if (i == alen) {
48                 if (j == blen) {
49                     return 0;
50                 } else {
51                     return -1;
52                 }
53             }
54             if (j == blen) {
55                 return +1;
56             }
57             int c = (int)a.charAt(i++) - (int)b.charAt(j++);
58             if (c != 0) {
59                 return c;
60             }
61         }
62     }
63
64 }
65
66
67 //
68
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
69
// you may not use this file except in compliance with the License. You may obtain a copy of the
70
// License at http://www.mozilla.org/MPL/
71
//
72
// Software distributed under the License is distributed on an "AS IS" basis,
73
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
74
// See the License for the specific language governing rights and limitations under the License.
75
//
76
// The Original Code is: all this file.
77
//
78
// The Initial Developer of the Original Code is Michael H. Kay
79
//
80
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
81
//
82
// Contributor(s): none
83
//
Popular Tags