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 less than or equal to the 029 * 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: 1166346 $ $Date: 2011-09-07 21:48:07 +0200 (Wed, 07 Sep 2011) $ 034 * @author Rodney Waldhoff 035 */ 036 public final class IsLessThanOrEqual<T> implements BinaryPredicate<T, T>, Serializable { 037 038 /** 039 * Basic IsLessThanOrEqual instance. 040 */ 041 public static final IsLessThanOrEqual<Comparable<?>> INSTANCE = IsLessThanOrEqual.<Comparable<?>>instance(); 042 043 /** 044 * serialVersionUID declaration. 045 */ 046 private static final long serialVersionUID = -7270189005780457145L; 047 048 private final Comparator<? super T> comparator; 049 050 /** 051 * Construct a <code>IsLessThanOrEqual</code> {@link BinaryPredicate predicate} 052 * for {@link Comparable Comparable}s. 053 */ 054 @SuppressWarnings("unchecked") 055 public IsLessThanOrEqual() { 056 this(ComparableComparator.INSTANCE); 057 } 058 059 /** 060 * Construct a <code>IsLessThanOrEqual</code> {@link BinaryPredicate predicate} 061 * for the given {@link Comparator Comparator}. 062 * 063 * @param comparator the {@link Comparator Comparator}, when <code>null</code>, 064 * a <code>Comparator</code> for {@link Comparable Comparable}s will 065 * be used. 066 */ 067 public IsLessThanOrEqual(Comparator<? super T> comparator) { 068 if (comparator == null) { 069 throw new IllegalArgumentException("Comparator argument must not be null"); 070 } 071 this.comparator = comparator; 072 } 073 074 /** 075 * {@inheritDoc} 076 * Return <code>true</code> iff the <i>left</i> parameter is 077 * less than or equal to the <i>right</i> parameter under my current 078 * {@link Comparator Comparator}. 079 */ 080 public boolean test(T left, T right) { 081 return comparator.compare(left, right) <= 0; 082 } 083 084 /** 085 * {@inheritDoc} 086 */ 087 public boolean equals(Object that) { 088 return that == this || (that instanceof IsLessThanOrEqual<?> && equals((IsLessThanOrEqual<?>) that)); 089 } 090 091 /** 092 * Learn whether another IsLessThanOrEqual is equal to this. 093 * @param that the IsLessThanOrEqual to test. 094 * @return boolean 095 */ 096 public boolean equals(IsLessThanOrEqual<?> that) { 097 if (null != that) { 098 if (null == comparator) { 099 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 }