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