Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ComparatorFunction |
|
| 1.7142857142857142;1.714 |
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 | package org.apache.commons.functor.core.comparator; | |
18 | ||
19 | import java.io.Serializable; | |
20 | import java.util.Comparator; | |
21 | ||
22 | import org.apache.commons.functor.BinaryFunction; | |
23 | ||
24 | /** | |
25 | * Adapts a {@link Comparator Comparator} to the | |
26 | * {@link BinaryFunction} interface. | |
27 | * | |
28 | * @version $Revision: 1156349 $ $Date: 2011-08-10 22:21:14 +0200 (Wed, 10 Aug 2011) $ | |
29 | * @author Rodney Waldhoff | |
30 | */ | |
31 | 0 | public final class ComparatorFunction<T> implements BinaryFunction<T, T, Integer>, Serializable { |
32 | ||
33 | /** | |
34 | * Basic ComparatorFunction instance. | |
35 | */ | |
36 | 2 | public static final ComparatorFunction<Comparable<?>> INSTANCE = ComparatorFunction.<Comparable<?>>instance(); |
37 | ||
38 | /** | |
39 | * serialVersionUID declaration. | |
40 | */ | |
41 | private static final long serialVersionUID = 1642024869929206095L; | |
42 | ||
43 | private final Comparator<? super T> comparator; | |
44 | ||
45 | /** | |
46 | * Create a new ComparatorFunction. | |
47 | * @param comparator to wrap | |
48 | */ | |
49 | 10 | public ComparatorFunction(Comparator<? super T> comparator) { |
50 | 10 | if (comparator == null) { |
51 | 0 | throw new IllegalArgumentException("Comparator must not be null"); |
52 | } | |
53 | 10 | this.comparator = comparator; |
54 | 10 | } |
55 | ||
56 | /** | |
57 | * {@inheritDoc} | |
58 | */ | |
59 | public Integer evaluate(T left, T right) { | |
60 | 50 | return Integer.valueOf(comparator.compare(left, right)); |
61 | } | |
62 | ||
63 | /** | |
64 | * {@inheritDoc} | |
65 | */ | |
66 | public boolean equals(Object that) { | |
67 | 22 | return that == this || (that instanceof ComparatorFunction<?> && equals((ComparatorFunction<?>) that)); |
68 | } | |
69 | ||
70 | /** | |
71 | * Learn whether a specified ComparatorFunction is equal to this. | |
72 | * @param that the ComparatorFunction to test | |
73 | * @return boolean | |
74 | */ | |
75 | public boolean equals(ComparatorFunction<?> that) { | |
76 | 10 | return null != that && comparator.equals(that.comparator); |
77 | } | |
78 | ||
79 | /** | |
80 | * {@inheritDoc} | |
81 | */ | |
82 | public int hashCode() { | |
83 | 26 | return "ComparatorFunction".hashCode() ^ comparator.hashCode(); |
84 | } | |
85 | ||
86 | /** | |
87 | * {@inheritDoc} | |
88 | */ | |
89 | public String toString() { | |
90 | 18 | return "ComparatorFunction<" + comparator + ">"; |
91 | } | |
92 | ||
93 | /** | |
94 | * Get a basic ComparatorFunction instance. | |
95 | * @return ComparatorFunction | |
96 | */ | |
97 | @SuppressWarnings("unchecked") | |
98 | public static <T> ComparatorFunction<T> instance() { | |
99 | 6 | return new ComparatorFunction<T>(ComparableComparator.INSTANCE); |
100 | } | |
101 | } |