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   * <p>This class is immutable and thread-safe.</p>
28   *
29   * @version $Id: StringEncoderComparator.java 1468177 2013-04-15 18:35:15Z ggregory $
30   */
31  @SuppressWarnings("rawtypes")
32  // TODO ought to implement Comparator<String> but that's not possible whilst maintaining binary compatibility.
33  public class StringEncoderComparator implements Comparator {
34  
35      /**
36       * Internal encoder instance.
37       */
38      private final StringEncoder stringEncoder;
39  
40      /**
41       * Constructs a new instance.
42       *
43       * @deprecated Creating an instance without a {@link StringEncoder} leads to a {@link NullPointerException}. Will be
44       *             removed in 2.0.
45       */
46      @Deprecated
47      public StringEncoderComparator() {
48          this.stringEncoder = null; // Trying to use this will cause things to break
49      }
50  
51      /**
52       * Constructs a new instance with the given algorithm.
53       *
54       * @param stringEncoder
55       *            the StringEncoder used for comparisons.
56       */
57      public StringEncoderComparator(final StringEncoder stringEncoder) {
58          this.stringEncoder = stringEncoder;
59      }
60  
61      /**
62       * Compares two strings based not on the strings themselves, but on an encoding of the two strings using the
63       * StringEncoder this Comparator was created with.
64       *
65       * If an {@link EncoderException} is encountered, return <code>0</code>.
66       *
67       * @param o1
68       *            the object to compare
69       * @param o2
70       *            the object to compare to
71       * @return the Comparable.compareTo() return code or 0 if an encoding error was caught.
72       * @see Comparable
73       */
74      @Override
75      public int compare(final Object o1, final Object o2) {
76  
77          int compareCode = 0;
78  
79          try {
80              @SuppressWarnings("unchecked") // May fail with CCE if encode returns something that is not Comparable
81              // However this was always the case.
82              final Comparable<Comparable<?>> s1 = (Comparable<Comparable<?>>) this.stringEncoder.encode(o1);
83              final Comparable<?> s2 = (Comparable<?>) this.stringEncoder.encode(o2);
84              compareCode = s1.compareTo(s2);
85          } catch (final EncoderException ee) {
86              compareCode = 0;
87          }
88          return compareCode;
89      }
90  
91  }