StringEncoderComparator.java

  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. package org.apache.commons.codec;

  18. import java.util.Comparator;

  19. /**
  20.  * Compares Strings using a {@link StringEncoder}. This comparator is used to sort Strings by an encoding scheme such as
  21.  * Soundex, Metaphone, etc. This class can come in handy if one need to sort Strings by an encoded form of a name such
  22.  * as Soundex.
  23.  *
  24.  * <p>This class is immutable and thread-safe.</p>
  25.  */
  26. @SuppressWarnings("rawtypes")
  27. // TODO ought to implement Comparator<String> but that's not possible whilst maintaining binary compatibility.
  28. public class StringEncoderComparator implements Comparator {

  29.     /**
  30.      * Internal encoder instance.
  31.      */
  32.     private final StringEncoder stringEncoder;

  33.     /**
  34.      * Constructs a new instance.
  35.      *
  36.      * @deprecated Creating an instance without a {@link StringEncoder} leads to a {@link NullPointerException}. Will be
  37.      *             removed in 2.0.
  38.      */
  39.     @Deprecated
  40.     public StringEncoderComparator() {
  41.         this.stringEncoder = null; // Trying to use this will cause things to break
  42.     }

  43.     /**
  44.      * Constructs a new instance with the given algorithm.
  45.      *
  46.      * @param stringEncoder
  47.      *            the StringEncoder used for comparisons.
  48.      */
  49.     public StringEncoderComparator(final StringEncoder stringEncoder) {
  50.         this.stringEncoder = stringEncoder;
  51.     }

  52.     /**
  53.      * Compares two strings based not on the strings themselves, but on an encoding of the two strings using the
  54.      * StringEncoder this Comparator was created with.
  55.      *
  56.      * If an {@link EncoderException} is encountered, return {@code 0}.
  57.      *
  58.      * @param o1
  59.      *            the object to compare
  60.      * @param o2
  61.      *            the object to compare to
  62.      * @return the Comparable.compareTo() return code or 0 if an encoding error was caught.
  63.      * @see Comparable
  64.      */
  65.     @Override
  66.     public int compare(final Object o1, final Object o2) {

  67.         int compareCode = 0;

  68.         try {
  69.             @SuppressWarnings("unchecked") // May fail with CCE if encode returns something that is not Comparable
  70.             // However this was always the case.
  71.             final Comparable<Comparable<?>> s1 = (Comparable<Comparable<?>>) this.stringEncoder.encode(o1);
  72.             final Comparable<?> s2 = (Comparable<?>) this.stringEncoder.encode(o2);
  73.             compareCode = s1.compareTo(s2);
  74.         } catch (final EncoderException ee) {
  75.             compareCode = 0;
  76.         }
  77.         return compareCode;
  78.     }

  79. }