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 * http://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 018 package org.apache.commons.codec; 019 020 import java.util.Comparator; 021 022 /** 023 * Strings are comparable, and this comparator allows 024 * you to configure it with an instance of a class 025 * which implements StringEncoder. This comparator 026 * is used to sort Strings by an encoding scheme such 027 * as Soundex, Metaphone, etc. This class can come in 028 * handy if one need to sort Strings by an encoded 029 * form of a name such as Soundex. 030 * 031 * @author Apache Software Foundation 032 * @version $Id: StringEncoderComparator.java 793391 2009-07-12 18:38:08Z ggregory $ 033 */ 034 public class StringEncoderComparator implements Comparator { 035 036 /** 037 * Internal encoder instance. 038 */ 039 private final StringEncoder stringEncoder; 040 041 /** 042 * Constructs a new instance. 043 * @deprecated as creating without a StringEncoder will lead to a 044 * broken NullPointerException creating comparator. 045 */ 046 public StringEncoderComparator() { 047 this.stringEncoder = null; // Trying to use this will cause things to break 048 } 049 050 /** 051 * Constructs a new instance with the given algorithm. 052 * @param stringEncoder the StringEncoder used for comparisons. 053 */ 054 public StringEncoderComparator(StringEncoder stringEncoder) { 055 this.stringEncoder = stringEncoder; 056 } 057 058 /** 059 * Compares two strings based not on the strings 060 * themselves, but on an encoding of the two 061 * strings using the StringEncoder this Comparator 062 * was created with. 063 * 064 * If an {@link EncoderException} is encountered, return <code>0</code>. 065 * 066 * @param o1 the object to compare 067 * @param o2 the object to compare to 068 * @return the Comparable.compareTo() return code or 0 if an encoding error was caught. 069 * @see Comparable 070 */ 071 public int compare(Object o1, Object o2) { 072 073 int compareCode = 0; 074 075 try { 076 Comparable s1 = (Comparable) this.stringEncoder.encode(o1); 077 Comparable s2 = (Comparable) this.stringEncoder.encode(o2); 078 compareCode = s1.compareTo(s2); 079 } 080 catch (EncoderException ee) { 081 compareCode = 0; 082 } 083 return compareCode; 084 } 085 086 }