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 import org.apache.commons.lang3.Validate;
26
27 /**
28 * A {@link BinaryPredicate BinaryPredicate} that {@link #test tests}
29 * <code>true</code> iff the left argument is greater than the
30 * right argument under the specified {@link Comparator}.
31 * When no (or a <code>null</code> <code>Comparator</code> is specified,
32 * a {@link Comparable Comparable} <code>Comparator</code> is used.
33 *
34 * @param <T> the binary predicate input types
35 * @version $Revision: 1365328 $ $Date: 2012-07-24 19:19:23 -0300 (Tue, 24 Jul 2012) $
36 */
37 public final class IsGreaterThan<T> implements BinaryPredicate<T, T>, Serializable {
38
39 /**
40 * Basic IsGreaterThan instance.
41 */
42 public static final IsGreaterThan<Comparable<?>> INSTANCE = IsGreaterThan.<Comparable<?>>instance();
43
44 /**
45 * serialVersionUID declaration.
46 */
47 private static final long serialVersionUID = 377027098765821021L;
48
49 /**
50 * The wrapped comparator.
51 */
52 private final Comparator<? super T> comparator;
53
54 /**
55 * Construct a <code>IsGreaterThan</code> {@link BinaryPredicate predicate}
56 * for {@link Comparable Comparable}s.
57 */
58 @SuppressWarnings("unchecked")
59 public IsGreaterThan() {
60 this((Comparator<? super T>) ComparableComparator.INSTANCE);
61 }
62
63 /**
64 * Construct a <code>IsGreaterThan</code> {@link BinaryPredicate predicate}
65 * for the given {@link Comparator Comparator}.
66 *
67 * @param comparator the {@link Comparator Comparator}, when <code>null</code>,
68 * a <code>Comparator</code> for {@link Comparable Comparable}s will
69 * be used.
70 */
71 public IsGreaterThan(Comparator<? super T> comparator) {
72 this.comparator = Validate.notNull(comparator, "Comparator argument must not be null");
73 }
74
75 /**
76 * Return <code>true</code> iff the <i>left</i> parameter is
77 * greater than the <i>right</i> parameter under my current
78 * {@link Comparator Comparator}.
79 * {@inheritDoc}
80 */
81 public boolean test(T left, T right) {
82 return comparator.compare(left, right) > 0;
83 }
84
85 /**
86 * {@inheritDoc}
87 */
88 @Override
89 public boolean equals(Object that) {
90 return that == this || (that instanceof IsGreaterThan<?> && equals((IsGreaterThan<?>) that));
91 }
92
93 /**
94 * Learn whether a given IsGreaterThan is equal to this.
95 * @param that the IsGreaterThan to test
96 * @return boolean
97 */
98 public boolean equals(IsGreaterThan<?> that) {
99 if (null != that) {
100 return comparator.equals(that.comparator);
101 }
102 return false;
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 @Override
109 public int hashCode() {
110 int hash = "IsGreaterThan".hashCode();
111 // by construction, comparator is never null
112 hash ^= comparator.hashCode();
113 return hash;
114 }
115
116 /**
117 * {@inheritDoc}
118 */
119 @Override
120 public String toString() {
121 return "IsGreaterThan<" + comparator + ">";
122 }
123
124 /**
125 * Get a typed IsGreaterThan instance.
126 *
127 * @param <T> the binary predicate input types
128 * @return IsGreaterThan<T>
129 */
130 public static <T extends Comparable<?>> IsGreaterThan<T> instance() {
131 return new IsGreaterThan<T>();
132 }
133
134 /**
135 * Get an IsGreaterThan UnaryPredicate.
136 *
137 * @param <T> the binary predicate input types
138 * @param right the right side object of the IsGreaterThan comparison
139 * @return UnaryPredicate<T>
140 */
141 public static <T extends Comparable<?>> UnaryPredicate<T> instance(T right) {
142 return RightBoundPredicate.bind(new IsGreaterThan<T>(), right);
143 }
144
145 }