Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Max |
|
| 1.6666666666666667;1.667 |
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 | import org.apache.commons.functor.UnaryFunction; | |
24 | import org.apache.commons.functor.adapter.RightBoundFunction; | |
25 | ||
26 | /** | |
27 | * Adapts a {@link Comparator Comparator} to the | |
28 | * {@link BinaryFunction} interface. | |
29 | * | |
30 | * @version $Revision: 1166353 $ $Date: 2011-09-07 21:53:49 +0200 (Wed, 07 Sep 2011) $ | |
31 | * @author Rodney Waldhoff | |
32 | */ | |
33 | public final class Max<T> implements BinaryFunction<T, T, T>, Serializable { | |
34 | ||
35 | /** | |
36 | * Basic Max instance. | |
37 | */ | |
38 | 2 | public static final Max<Comparable<?>> INSTANCE = Max.<Comparable<?>>instance(); |
39 | ||
40 | /** | |
41 | * serialVersionUID declaration. | |
42 | */ | |
43 | private static final long serialVersionUID = 6514424464263828685L; | |
44 | ||
45 | private final Comparator<T> comparator; | |
46 | ||
47 | /** | |
48 | * Create a new Max. | |
49 | */ | |
50 | @SuppressWarnings("unchecked") | |
51 | public Max() { | |
52 | 20 | this(ComparableComparator.instance()); |
53 | 20 | } |
54 | ||
55 | /** | |
56 | * Create a new Max. | |
57 | * @param comparator Comparator to use | |
58 | */ | |
59 | 24 | public Max(Comparator<T> comparator) { |
60 | 24 | if (comparator == null) { |
61 | 0 | throw new IllegalArgumentException("Comparator argument must not be null"); |
62 | } | |
63 | 24 | this.comparator = comparator; |
64 | 24 | } |
65 | ||
66 | /** | |
67 | * {@inheritDoc} | |
68 | */ | |
69 | public T evaluate(T left, T right) { | |
70 | 12 | return (comparator.compare(left, right) >= 0) ? left : right; |
71 | } | |
72 | ||
73 | /** | |
74 | * {@inheritDoc} | |
75 | */ | |
76 | public boolean equals(Object that) { | |
77 | 26 | return that == this || (that instanceof Max<?> && equals((Max<?>) that)); |
78 | } | |
79 | ||
80 | /** | |
81 | * Learn whether another Max is equal to this. | |
82 | * @param that Max to test | |
83 | * @return boolean | |
84 | */ | |
85 | public boolean equals(Max<?> that) { | |
86 | 18 | return null != that && comparator.equals(that.comparator); |
87 | } | |
88 | ||
89 | /** | |
90 | * {@inheritDoc} | |
91 | */ | |
92 | public int hashCode() { | |
93 | 30 | return "Max".hashCode() ^ comparator.hashCode(); |
94 | } | |
95 | ||
96 | /** | |
97 | * {@inheritDoc} | |
98 | */ | |
99 | public String toString() { | |
100 | 22 | return "Max<" + comparator + ">"; |
101 | } | |
102 | ||
103 | /** | |
104 | * Get a Max instance. | |
105 | * @return Max | |
106 | */ | |
107 | public static <T extends Comparable<?>> Max<T> instance() { | |
108 | 20 | return new Max<T>(); |
109 | } | |
110 | ||
111 | /** | |
112 | * Get a Max UnaryFunction. | |
113 | * @param right the right side argument of the Max function | |
114 | * @return UnaryFunction<T, T> | |
115 | */ | |
116 | public static <T extends Comparable<?>> UnaryFunction<T, T> instance(T right) { | |
117 | 0 | return RightBoundFunction.bind(new Max<T>(), right); |
118 | } | |
119 | ||
120 | } |