View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.codec;
19  
20  import java.util.Comparator;
21  
22  /**
23   * Compares Strings using a {@link StringEncoder}. This comparator is used to sort Strings by an encoding scheme such as
24   * Soundex, Metaphone, etc. This class can come in handy if one need to sort Strings by an encoded form of a name such
25   * as Soundex.
26   * 
27   * @author Apache Software Foundation
28   * @version $Id: StringEncoderComparator.java 1201520 2011-11-13 21:36:18Z ggregory $
29   */
30  public class StringEncoderComparator implements Comparator {
31  
32      /**
33       * Internal encoder instance.
34       */
35      private final StringEncoder stringEncoder;
36  
37      /**
38       * Constructs a new instance.
39       * 
40       * @deprecated Creating an instance without a {@link StringEncoder} leads to a {@link NullPointerException}. Will be
41       *             removed in 2.0.
42       */
43      public StringEncoderComparator() {
44          this.stringEncoder = null; // Trying to use this will cause things to break
45      }
46  
47      /**
48       * Constructs a new instance with the given algorithm.
49       * 
50       * @param stringEncoder
51       *            the StringEncoder used for comparisons.
52       */
53      public StringEncoderComparator(StringEncoder stringEncoder) {
54          this.stringEncoder = stringEncoder;
55      }
56  
57      /**
58       * Compares two strings based not on the strings themselves, but on an encoding of the two strings using the
59       * StringEncoder this Comparator was created with.
60       * 
61       * If an {@link EncoderException} is encountered, return <code>0</code>.
62       * 
63       * @param o1
64       *            the object to compare
65       * @param o2
66       *            the object to compare to
67       * @return the Comparable.compareTo() return code or 0 if an encoding error was caught.
68       * @see Comparable
69       */
70      public int compare(Object o1, Object o2) {
71  
72          int compareCode = 0;
73  
74          try {
75              Comparable s1 = (Comparable) this.stringEncoder.encode(o1);
76              Comparable s2 = (Comparable) this.stringEncoder.encode(o2);
77              compareCode = s1.compareTo(s2);
78          } catch (EncoderException ee) {
79              compareCode = 0;
80          }
81          return compareCode;
82      }
83  
84  }