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 * https://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 import java.util.Objects;
22
23 /**
24 * Compares Strings using a {@link StringEncoder}. This comparator is used to sort Strings by an encoding scheme such as Soundex, Metaphone, etc. This class can
25 * come in handy if one need to sort Strings by an encoded form of a name such as Soundex.
26 * <p>
27 * This class is immutable and thread-safe.
28 * </p>
29 */
30 @SuppressWarnings("rawtypes")
31 // TODO ought to implement Comparator<String> but that's not possible whilst maintaining binary compatibility.
32 public class StringEncoderComparator implements Comparator {
33
34 /**
35 * Internal encoder instance.
36 */
37 private final StringEncoder stringEncoder;
38
39 /**
40 * Constructs a new instance.
41 *
42 * @deprecated Creating an instance without a {@link StringEncoder} leads to a {@link NullPointerException}. Will be 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 the StringEncoder used for comparisons.
53 * @throws NullPointerException if the StringEncoder is null.
54 */
55 public StringEncoderComparator(final StringEncoder stringEncoder) {
56 this.stringEncoder = Objects.requireNonNull(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 StringEncoder this Comparator was created with.
61 * If an {@link EncoderException} is encountered, return {@code 0}.
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 @Override
69 public int compare(final Object o1, final Object o2) {
70 int compareCode = 0;
71 try {
72 @SuppressWarnings("unchecked") // May fail with CCE if encode returns something that is not Comparable
73 // However this was always the case.
74 final Comparable<Comparable<?>> s1 = (Comparable<Comparable<?>>) stringEncoder.encode(o1);
75 final Comparable<?> s2 = (Comparable<?>) stringEncoder.encode(o2);
76 compareCode = s1.compareTo(s2);
77 } catch (final EncoderException ee) {
78 compareCode = 0;
79 }
80 return compareCode;
81 }
82 }