001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.functor.core.comparator;
018
019 import java.io.Serializable;
020 import java.util.Comparator;
021
022 import org.apache.commons.functor.BinaryPredicate;
023 import org.apache.commons.functor.UnaryPredicate;
024 import org.apache.commons.functor.adapter.RightBoundPredicate;
025
026 /**
027 * A {@link BinaryPredicate BinaryPredicate} that {@link #test tests}
028 * <code>true</code> iff the left argument is greater than or equal
029 * to the right argument under the specified {@link Comparator}.
030 * When no (or a <code>null</code> <code>Comparator</code> is specified,
031 * a {@link Comparable Comparable} <code>Comparator</code> is used.
032 *
033 * @version $Revision: 1166343 $ $Date: 2011-09-07 21:45:53 +0200 (Wed, 07 Sep 2011) $
034 * @author Rodney Waldhoff
035 */
036 public final class IsGreaterThanOrEqual<T> implements BinaryPredicate<T, T>, Serializable {
037
038 /**
039 * Basic IsGreaterThanOrEqual instance.
040 */
041 public static final IsGreaterThanOrEqual<Comparable<?>> INSTANCE = IsGreaterThanOrEqual
042 .<Comparable<?>> instance();
043
044 /**
045 * serialVersionUID declaration.
046 */
047 private static final long serialVersionUID = 5262405026444050167L;
048
049 private final Comparator<? super T> comparator;
050
051 /**
052 * Construct a <code>IsGreaterThanOrEqual</code> {@link BinaryPredicate predicate}
053 * for {@link Comparable Comparable}s.
054 */
055 @SuppressWarnings("unchecked")
056 public IsGreaterThanOrEqual() {
057 this(ComparableComparator.INSTANCE);
058 }
059
060 /**
061 * Construct a <code>IsGreaterThanOrEqual</code> {@link BinaryPredicate predicate}
062 * for the given {@link Comparator Comparator}.
063 *
064 * @param comparator the {@link Comparator Comparator}, when <code>null</code>,
065 * a <code>Comparator</code> for {@link Comparable Comparable}s will
066 * be used.
067 */
068 public IsGreaterThanOrEqual(Comparator<? super T> comparator) {
069 if (comparator == null) {
070 throw new IllegalArgumentException("Comparator argument must not be null");
071 }
072 this.comparator = comparator;
073 }
074
075 /**
076 * Return <code>true</code> iff the <i>left</i> parameter is
077 * greater than or equal to the <i>right</i> parameter under my current
078 * {@link Comparator Comparator}.
079 * {@inheritDoc}
080 */
081 public boolean test(T left, T right) {
082 return comparator.compare(left, right) >= 0;
083 }
084
085 /**
086 * {@inheritDoc}
087 */
088 public boolean equals(Object that) {
089 return that == this || (that instanceof IsGreaterThanOrEqual<?> && equals((IsGreaterThanOrEqual<?>) that));
090 }
091
092 /**
093 * Learn whether another IsGreaterThanOrEqual is equal to this.
094 * @param that IsGreaterThanOrEqual to test
095 * @return boolean
096 */
097 public boolean equals(IsGreaterThanOrEqual<?> that) {
098 if (null != that) {
099 if (null == comparator) {
100 return null == that.comparator;
101 }
102 return comparator.equals(that.comparator);
103 }
104 return false;
105 }
106
107 /**
108 * {@inheritDoc}
109 */
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 public String toString() {
121 return "IsGreaterThanOrEqual<" + comparator + ">";
122 }
123
124 /**
125 * Get a typed IsGreaterThanOrEqual instance.
126 * @param <T>
127 * @return IsGreaterThanOrEqual<T>
128 */
129 public static <T extends Comparable<?>> IsGreaterThanOrEqual<T> instance() {
130 return new IsGreaterThanOrEqual<T>();
131 }
132
133 /**
134 * Get an IsGreaterThanOrEqual UnaryPredicate.
135 * @param right the right side object of the IsGreaterThanOrEqual comparison
136 * @return UnaryPredicate
137 */
138 public static <T extends Comparable<?>> UnaryPredicate<T> instance(T right) {
139 return RightBoundPredicate.bind(new IsGreaterThanOrEqual<T>(), right);
140 }
141
142 }