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