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