001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.codec; 019 020import java.util.Comparator; 021import java.util.Objects; 022 023/** 024 * 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 025 * come in handy if one need to sort Strings by an encoded form of a name such as Soundex. 026 * <p> 027 * This class is immutable and thread-safe. 028 * </p> 029 */ 030@SuppressWarnings("rawtypes") 031// TODO ought to implement Comparator<String> but that's not possible whilst maintaining binary compatibility. 032public class StringEncoderComparator implements Comparator { 033 034 /** 035 * Internal encoder instance. 036 */ 037 private final StringEncoder stringEncoder; 038 039 /** 040 * Constructs a new instance. 041 * 042 * @deprecated Creating an instance without a {@link StringEncoder} leads to a {@link NullPointerException}. Will be removed in 2.0. 043 */ 044 @Deprecated 045 public StringEncoderComparator() { 046 this.stringEncoder = null; // Trying to use this will cause things to break 047 } 048 049 /** 050 * Constructs a new instance with the given algorithm. 051 * 052 * @param stringEncoder the StringEncoder used for comparisons. 053 * @throws NullPointerException if the StringEncoder is null. 054 */ 055 public StringEncoderComparator(final StringEncoder stringEncoder) { 056 this.stringEncoder = Objects.requireNonNull(stringEncoder, "stringEncoder"); 057 } 058 059 /** 060 * 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. 061 * If an {@link EncoderException} is encountered, return {@code 0}. 062 * 063 * @param o1 the object to compare. 064 * @param o2 the object to compare to. 065 * @return The Comparable.compareTo() return code or 0 if an encoding error was caught. 066 * @see Comparable 067 */ 068 @Override 069 public int compare(final Object o1, final Object o2) { 070 int compareCode = 0; 071 try { 072 @SuppressWarnings("unchecked") // May fail with CCE if encode returns something that is not Comparable 073 // However this was always the case. 074 final Comparable<Comparable<?>> s1 = (Comparable<Comparable<?>>) stringEncoder.encode(o1); 075 final Comparable<?> s2 = (Comparable<?>) stringEncoder.encode(o2); 076 compareCode = s1.compareTo(s2); 077 } catch (final EncoderException ee) { 078 compareCode = 0; 079 } 080 return compareCode; 081 } 082}