KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > codec > StringEncoderComparator


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.codec;
18
19 import java.util.Comparator JavaDoc;
20
21 /**
22  * Strings are comparable, and this comparator allows
23  * you to configure it with an instance of a class
24  * which implements StringEncoder. This comparator
25  * is used to sort Strings by an encoding scheme such
26  * as Soundex, Metaphone, etc. This class can come in
27  * handy if one need to sort Strings by an encoded
28  * form of a name such as Soundex.
29  *
30  * @author Apache Software Foundation
31  * @version $Id: StringEncoderComparator.java,v 1.14 2004/06/21 23:24:17 ggregory Exp $
32  */

33 public class StringEncoderComparator implements Comparator JavaDoc {
34
35     /**
36      * Internal encoder instance.
37      */

38     private StringEncoder stringEncoder;
39
40     /**
41      * Constructs a new instance.
42      */

43     public StringEncoderComparator() {
44         // no init.
45
}
46
47     /**
48      * Constructs a new instance with the given algorithm.
49      * @param stringEncoder the StringEncoder used for comparisons.
50      */

51     public StringEncoderComparator(StringEncoder stringEncoder) {
52         this.stringEncoder = stringEncoder;
53     }
54
55     /**
56      * Compares two strings based not on the strings
57      * themselves, but on an encoding of the two
58      * strings using the StringEncoder this Comparator
59      * was created with.
60      *
61      * If an {@link EncoderException} is encountered, return <code>0</code>.
62      *
63      * @param o1 the object to compare
64      * @param o2 the object to compare to
65      * @return the Comparable.compareTo() return code or 0 if an encoding error was caught.
66      * @see Comparable
67      */

68     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
69
70         int compareCode = 0;
71
72         try {
73             Comparable JavaDoc s1 = (Comparable JavaDoc) ((Encoder) this.stringEncoder).encode(o1);
74             Comparable JavaDoc s2 = (Comparable JavaDoc) ((Encoder) this.stringEncoder).encode(o2);
75             compareCode = s1.compareTo(s2);
76         }
77         catch (EncoderException ee) {
78             compareCode = 0;
79         }
80         return compareCode;
81     }
82
83 }
84
Popular Tags