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 greater than or equal 29 * to the 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 * @version $Revision: 1166343 $ $Date: 2011-09-07 21:45:53 +0200 (Wed, 07 Sep 2011) $ 34 * @author Rodney Waldhoff 35 */ 36 public final class IsGreaterThanOrEqual<T> implements BinaryPredicate<T, T>, Serializable { 37 38 /** 39 * Basic IsGreaterThanOrEqual instance. 40 */ 41 public static final IsGreaterThanOrEqual<Comparable<?>> INSTANCE = IsGreaterThanOrEqual 42 .<Comparable<?>> instance(); 43 44 /** 45 * serialVersionUID declaration. 46 */ 47 private static final long serialVersionUID = 5262405026444050167L; 48 49 private final Comparator<? super T> comparator; 50 51 /** 52 * Construct a <code>IsGreaterThanOrEqual</code> {@link BinaryPredicate predicate} 53 * for {@link Comparable Comparable}s. 54 */ 55 @SuppressWarnings("unchecked") 56 public IsGreaterThanOrEqual() { 57 this(ComparableComparator.INSTANCE); 58 } 59 60 /** 61 * Construct a <code>IsGreaterThanOrEqual</code> {@link BinaryPredicate predicate} 62 * for the given {@link Comparator Comparator}. 63 * 64 * @param comparator the {@link Comparator Comparator}, when <code>null</code>, 65 * a <code>Comparator</code> for {@link Comparable Comparable}s will 66 * be used. 67 */ 68 public IsGreaterThanOrEqual(Comparator<? super T> comparator) { 69 if (comparator == null) { 70 throw new IllegalArgumentException("Comparator argument must not be null"); 71 } 72 this.comparator = comparator; 73 } 74 75 /** 76 * Return <code>true</code> iff the <i>left</i> parameter is 77 * greater than or equal to 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 public boolean equals(Object that) { 89 return that == this || (that instanceof IsGreaterThanOrEqual<?> && equals((IsGreaterThanOrEqual<?>) that)); 90 } 91 92 /** 93 * Learn whether another IsGreaterThanOrEqual is equal to this. 94 * @param that IsGreaterThanOrEqual to test 95 * @return boolean 96 */ 97 public boolean equals(IsGreaterThanOrEqual<?> that) { 98 if (null != that) { 99 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 }