1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/* 2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Copyright 2001-2004 The Apache Software Foundation. 3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * you may not use this file except in compliance with the License. 6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * You may obtain a copy of the License at 7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * See the License for the specific language governing permissions and 14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * limitations under the License. 15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.commons.codec; 18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.Comparator; 20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/** 22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Strings are comparable, and this comparator allows 23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * you to configure it with an instance of a class 24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * which implements StringEncoder. This comparator 25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * is used to sort Strings by an encoding scheme such 26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * as Soundex, Metaphone, etc. This class can come in 27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * handy if one need to sort Strings by an encoded 28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * form of a name such as Soundex. 29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author Apache Software Foundation 31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @version $Id: StringEncoderComparator.java,v 1.14 2004/06/21 23:24:17 ggregory Exp $ 32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic class StringEncoderComparator implements Comparator { 34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Internal encoder instance. 37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private StringEncoder stringEncoder; 39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Constructs a new instance. 42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public StringEncoderComparator() { 44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // no init. 45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Constructs a new instance with the given algorithm. 49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param stringEncoder the StringEncoder used for comparisons. 50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 51069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public StringEncoderComparator(StringEncoder stringEncoder) { 52069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project this.stringEncoder = stringEncoder; 53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 54069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Compares two strings based not on the strings 57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * themselves, but on an encoding of the two 58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * strings using the StringEncoder this Comparator 59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * was created with. 60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * If an {@link EncoderException} is encountered, return <code>0</code>. 62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param o1 the object to compare 64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param o2 the object to compare to 65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return the Comparable.compareTo() return code or 0 if an encoding error was caught. 66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @see Comparable 67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public int compare(Object o1, Object o2) { 69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project int compareCode = 0; 71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project try { 73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project Comparable s1 = (Comparable) ((Encoder) this.stringEncoder).encode(o1); 74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project Comparable s2 = (Comparable) ((Encoder) this.stringEncoder).encode(o2); 75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project compareCode = s1.compareTo(s2); 76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project catch (EncoderException ee) { 78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project compareCode = 0; 79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return compareCode; 81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project} 84