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