Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IsEquivalent |
|
| 1.8888888888888888;1.889 |
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.BinaryPredicate; | |
23 | import org.apache.commons.functor.UnaryPredicate; | |
24 | import org.apache.commons.functor.adapter.RightBoundPredicate; | |
25 | ||
26 | /** | |
27 | * A {@link BinaryPredicate BinaryPredicate} that {@link #test tests} | |
28 | * <code>true</code> iff the left argument is equal to the | |
29 | * right argument under the specified {@link Comparator}. | |
30 | * When no (or a <code>null</code> <code>Comparator</code> is specified, | |
31 | * a {@link Comparable Comparable} <code>Comparator</code> is used. | |
32 | * | |
33 | * @see org.apache.commons.functor.core.IsEqual | |
34 | * | |
35 | * @version $Revision: 1166340 $ $Date: 2011-09-07 21:44:28 +0200 (Wed, 07 Sep 2011) $ | |
36 | * @author Rodney Waldhoff | |
37 | * | |
38 | */ | |
39 | public final class IsEquivalent<T> implements BinaryPredicate<T, T>, Serializable { | |
40 | ||
41 | /** | |
42 | * Basic IsEquivalent instance. | |
43 | */ | |
44 | 2 | public static final IsEquivalent<Comparable<?>> INSTANCE = IsEquivalent.<Comparable<?>> instance(); |
45 | ||
46 | /** | |
47 | * serialVersionUID declaration. | |
48 | */ | |
49 | private static final long serialVersionUID = -6392784113015793664L; | |
50 | ||
51 | private final Comparator<? super T> comparator; | |
52 | ||
53 | /** | |
54 | * Create a new IsEquivalent. | |
55 | */ | |
56 | @SuppressWarnings("unchecked") | |
57 | public IsEquivalent() { | |
58 | 0 | this(ComparableComparator.INSTANCE); |
59 | 0 | } |
60 | ||
61 | /** | |
62 | * Construct an <code>IsEquivalent</code> {@link BinaryPredicate predicate} | |
63 | * for the given {@link Comparator Comparator}. | |
64 | * | |
65 | * @param comparator the {@link Comparator Comparator}, when <code>null</code>, | |
66 | * a <code>Comparator</code> for {@link Comparable Comparable}s will | |
67 | * be used. | |
68 | */ | |
69 | 12 | public IsEquivalent(Comparator<? super T> comparator) { |
70 | 12 | if (comparator == null) { |
71 | 0 | throw new IllegalArgumentException("Comparator must not be null"); |
72 | } | |
73 | 12 | this.comparator = comparator; |
74 | 12 | } |
75 | ||
76 | /** | |
77 | * Return <code>true</code> iff the <i>left</i> parameter is | |
78 | * equal to the <i>right</i> parameter under my current | |
79 | * {@link Comparator Comparator}. | |
80 | * {@inheritDoc} | |
81 | */ | |
82 | public boolean test(T left, T right) { | |
83 | 26 | return comparator.compare(left, right) == 0; |
84 | } | |
85 | ||
86 | /** | |
87 | * {@inheritDoc} | |
88 | */ | |
89 | public boolean equals(Object that) { | |
90 | 22 | return that == this || (that instanceof IsEquivalent<?> && equals((IsEquivalent<?>) that)); |
91 | } | |
92 | ||
93 | /** | |
94 | * Learn whether a given IsEquivalent is equal to this. | |
95 | * @param that IsEquivalent to test | |
96 | * @return boolean | |
97 | */ | |
98 | public boolean equals(IsEquivalent<?> that) { | |
99 | 10 | if (null != that) { |
100 | 10 | if (null == comparator) { |
101 | 0 | return null == that.comparator; |
102 | } | |
103 | 10 | return comparator.equals(that.comparator); |
104 | } | |
105 | 0 | return false; |
106 | } | |
107 | ||
108 | /** | |
109 | * {@inheritDoc} | |
110 | */ | |
111 | public int hashCode() { | |
112 | 24 | int hash = "IsEquivalent".hashCode(); |
113 | // by construction, comparator is never null | |
114 | 24 | hash ^= comparator.hashCode(); |
115 | 24 | return hash; |
116 | } | |
117 | ||
118 | /** | |
119 | * {@inheritDoc} | |
120 | */ | |
121 | public String toString() { | |
122 | 16 | return "IsEquivalent<" + comparator + ">"; |
123 | } | |
124 | ||
125 | /** | |
126 | * Get a basic IsEquivalent instance. | |
127 | * @param T | |
128 | * @return IsEquivalent<T> | |
129 | */ | |
130 | @SuppressWarnings("unchecked") | |
131 | public static <T extends Comparable<?>> IsEquivalent<T> instance() { | |
132 | 10 | return new IsEquivalent<T>(ComparableComparator.INSTANCE); |
133 | } | |
134 | ||
135 | /** | |
136 | * Get an IsEquivalent instance that always compares to <code>arg</code>. | |
137 | * @param right argument | |
138 | * @return UnaryPredicate | |
139 | */ | |
140 | public static <T extends Comparable<?>> UnaryPredicate<T> instance(T right) { | |
141 | 4 | return RightBoundPredicate.bind(instance(), right); |
142 | } | |
143 | ||
144 | } |