Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
StringEncoderComparator |
|
| 1.3333333333333333;1.333 |
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 | * http://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 | ||
22 | /** | |
23 | * Compares Strings using a {@link StringEncoder}. This comparator is used to sort Strings by an encoding scheme such as | |
24 | * Soundex, Metaphone, etc. This class can come in handy if one need to sort Strings by an encoded form of a name such | |
25 | * as Soundex. | |
26 | * | |
27 | * <p>This class is immutable and thread-safe.</p> | |
28 | * | |
29 | * @version $Id$ | |
30 | */ | |
31 | public class StringEncoderComparator implements Comparator { | |
32 | ||
33 | /** | |
34 | * Internal encoder instance. | |
35 | */ | |
36 | private final StringEncoder stringEncoder; | |
37 | ||
38 | /** | |
39 | * Constructs a new instance. | |
40 | * | |
41 | * @deprecated Creating an instance without a {@link StringEncoder} leads to a {@link NullPointerException}. Will be | |
42 | * removed in 2.0. | |
43 | */ | |
44 | @Deprecated | |
45 | 0 | public StringEncoderComparator() { |
46 | 0 | this.stringEncoder = null; // Trying to use this will cause things to break |
47 | 0 | } |
48 | ||
49 | /** | |
50 | * Constructs a new instance with the given algorithm. | |
51 | * | |
52 | * @param stringEncoder | |
53 | * the StringEncoder used for comparisons. | |
54 | */ | |
55 | 3 | public StringEncoderComparator(StringEncoder stringEncoder) { |
56 | 3 | this.stringEncoder = stringEncoder; |
57 | 3 | } |
58 | ||
59 | /** | |
60 | * Compares two strings based not on the strings themselves, but on an encoding of the two strings using the | |
61 | * StringEncoder this Comparator was created with. | |
62 | * | |
63 | * If an {@link EncoderException} is encountered, return <code>0</code>. | |
64 | * | |
65 | * @param o1 | |
66 | * the object to compare | |
67 | * @param o2 | |
68 | * the object to compare to | |
69 | * @return the Comparable.compareTo() return code or 0 if an encoding error was caught. | |
70 | * @see Comparable | |
71 | */ | |
72 | @Override | |
73 | public int compare(Object o1, Object o2) { | |
74 | ||
75 | 7 | int compareCode = 0; |
76 | ||
77 | try { | |
78 | 7 | Comparable s1 = (Comparable) this.stringEncoder.encode(o1); |
79 | 6 | Comparable s2 = (Comparable) this.stringEncoder.encode(o2); |
80 | 6 | compareCode = s1.compareTo(s2); |
81 | 1 | } catch (EncoderException ee) { |
82 | 1 | compareCode = 0; |
83 | 6 | } |
84 | 7 | return compareCode; |
85 | } | |
86 | ||
87 | } |