• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
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  * @deprecated Please use {@link java.net.URL#openConnection} instead.
34  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
35  *     for further details.
36  */
37 @Deprecated
38 public class StringEncoderComparator implements Comparator {
39 
40     /**
41      * Internal encoder instance.
42      */
43     private StringEncoder stringEncoder;
44 
45     /**
46      * Constructs a new instance.
47      */
StringEncoderComparator()48     public StringEncoderComparator() {
49         // no init.
50     }
51 
52     /**
53      * Constructs a new instance with the given algorithm.
54      * @param stringEncoder the StringEncoder used for comparisons.
55      */
StringEncoderComparator(StringEncoder stringEncoder)56     public StringEncoderComparator(StringEncoder stringEncoder) {
57         this.stringEncoder = stringEncoder;
58     }
59 
60     /**
61      * Compares two strings based not on the strings
62      * themselves, but on an encoding of the two
63      * strings using the StringEncoder this Comparator
64      * was created with.
65      *
66      * If an {@link EncoderException} is encountered, return <code>0</code>.
67      *
68      * @param o1 the object to compare
69      * @param o2 the object to compare to
70      * @return the Comparable.compareTo() return code or 0 if an encoding error was caught.
71      * @see Comparable
72      */
compare(Object o1, Object o2)73     public int compare(Object o1, Object o2) {
74 
75         int compareCode = 0;
76 
77         try {
78             Comparable s1 = (Comparable) ((Encoder) this.stringEncoder).encode(o1);
79             Comparable s2 = (Comparable) ((Encoder) this.stringEncoder).encode(o2);
80             compareCode = s1.compareTo(s2);
81         }
82         catch (EncoderException ee) {
83             compareCode = 0;
84         }
85         return compareCode;
86     }
87 
88 }
89