KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > util > CharArrayComparator


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.util;
17
18 import java.util.Comparator JavaDoc;
19
20 /**
21  * Performs a case-sensitive comparision of char arrays.
22  */

23 public class CharArrayComparator implements Comparator JavaDoc {
24     
25     public static final CharArrayComparator INSTANCE = new CharArrayComparator();
26
27     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
28         char[] a = (char[])o1;
29         char[] b = (char[])o2;
30         
31         int ai = 0;
32         int bi = 0;
33
34         for (; ai < a.length && bi < b.length; ++ai, ++bi) {
35             int c = a[ai] - b[bi];
36             if (c != 0) {
37                 return c;
38             }
39         }
40
41         if (ai == a.length && bi < b.length) {
42             // a is shorter
43
return -1;
44         }
45
46         if (ai < a.length && bi == b.length) {
47             // b is shorter
48
return 1;
49         }
50         
51         // they are equal
52
//
53
return 0;
54     }
55
56 }
57
Popular Tags