Coverage Report - jaggregate.Comparables
 
Classes in this File Line Coverage Branch Coverage Complexity
Comparables
100%
9/9
N/A
0
Comparables$1
N/A
N/A
0
Comparables$Between
100%
8/8
100%
1/1
0
Comparables$GreaterThan
100%
3/3
100%
1/1
0
Comparables$GreaterThanOrEqualTo
100%
3/3
100%
1/1
0
Comparables$LessThan
100%
3/3
100%
1/1
0
Comparables$LessThanOrEqualTo
100%
3/3
100%
1/1
0
Comparables$Max
100%
3/3
100%
1/1
0
Comparables$Min
100%
3/3
100%
1/1
0
Comparables$NaturalOrderingPredicate
100%
3/3
N/A
0
 
 1  
 /*
 2  
  Copyright 2004-2008 Paul R. Holser, Jr.  All rights reserved.
 3  
  Licensed under the Academic Free License version 3.0
 4  
  */
 5  
 
 6  
 package jaggregate;
 7  
 
 8  
 import java.util.Comparator;
 9  
 import static java.lang.String.*;
 10  
 
 11  
 /**
 12  
  * Utility class that offers useful operations on {@linkplain Comparable Comparables}.
 13  
  *
 14  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 15  
  * @version $Id: Comparables.java,v 1.7 2008/08/09 04:35:29 pholser Exp $
 16  
  */
 17  
 public class Comparables {
 18  
     /**
 19  
      * Discourages instantiation.
 20  
      *
 21  
      * @throws UnsupportedOperationException always
 22  
      */
 23  1
     protected Comparables() {
 24  1
         throw new UnsupportedOperationException();
 25  
     }
 26  
 
 27  
     /**
 28  
      * Answers a predicate which will answer {@code true} if its argument compares
 29  
      * "less than" the given comparand.
 30  
      * <p/>
 31  
      * For purposes of this comparison, {@code null} always compares "less than"
 32  
      * non-{@code null}.
 33  
      *
 34  
      * @param <E> a constraint on the type of the comparand
 35  
      * @param comparand the comparand to use with the returned predicate
 36  
      * @return a predicate that can be used to compare arguments with {@code comparand}
 37  
      */
 38  
     public static <E extends Comparable<? super E>> UnaryPredicate<E> lessThan(
 39  
         E comparand ) {
 40  
 
 41  11
         return new LessThan<E>().bindSecond( comparand );
 42  
     }
 43  
 
 44  
     /**
 45  
      * Answers a predicate which will answer {@code true} if its argument compares
 46  
      * "less than" or "equal to" the given comparand.
 47  
      * <p/>
 48  
      * For purposes of this comparison, {@code null} always compares "less than"
 49  
      * non-{@code null}.
 50  
      *
 51  
      * @param <E> a constraint on the type of the comparand
 52  
      * @param comparand the comparand to use with the returned predicate
 53  
      * @return a predicate that can be used to compare arguments with {@code comparand}
 54  
      */
 55  
     public static <E extends Comparable<? super E>> UnaryPredicate<E> lessThanOrEqualTo(
 56  
         E comparand ) {
 57  
 
 58  7
         return new LessThanOrEqualTo<E>().bindSecond( comparand );
 59  
     }
 60  
 
 61  
     /**
 62  
      * Answers a predicate which will answer {@code true} if its argument compares
 63  
      * "greater than" the given comparand.
 64  
      * <p/>
 65  
      * For purposes of this comparison, {@code null} always compares "less than"
 66  
      * non-{@code null}.
 67  
      *
 68  
      * @param <E> a constraint on the type of the comparand
 69  
      * @param comparand the comparand to use with the returned predicate
 70  
      * @return a predicate that can be used to compare arguments with {@code comparand}
 71  
      */
 72  
     public static <E extends Comparable<? super E>> UnaryPredicate<E> greaterThan(
 73  
         E comparand ) {
 74  
 
 75  7
         return new GreaterThan<E>().bindSecond( comparand );
 76  
     }
 77  
 
 78  
     /**
 79  
      * Answers a predicate which will answer {@code true} if its argument compares
 80  
      * "greater than" or "equal to" the given comparand.
 81  
      * <p/>
 82  
      * For purposes of this comparison, {@code null} always compares "less than"
 83  
      * non-{@code null}.
 84  
      *
 85  
      * @param <E> a constraint on the type of the comparand
 86  
      * @param comparand the comparand to use with the returned predicate
 87  
      * @return a predicate that can be used to compare arguments with {@code comparand}
 88  
      */
 89  
     public static <E extends Comparable<? super E>>
 90  
     UnaryPredicate<E> greaterThanOrEqualTo( E comparand ) {
 91  7
         return new GreaterThanOrEqualTo<E>().bindSecond( comparand );
 92  
     }
 93  
 
 94  
     /**
 95  
      * Answers a predicate which will answer {@code true} if its argument compares
 96  
      * "between" the given minimum and maximum, inclusive.
 97  
      * <p/>
 98  
      * For purposes of this comparison, {@code null} always compares "less than"
 99  
      * non-{@code null}.
 100  
      *
 101  
      * @param <E> a constraint on the type of the bounds
 102  
      * @param minimum the lower bound
 103  
      * @param maximum the upper bound
 104  
      * @return a predicate that can be used to compare arguments with {@code minimum}
 105  
      *         and {@code maximum}
 106  
      */
 107  
     public static <E extends Comparable<? super E>> UnaryPredicate<E> between( E minimum,
 108  
         E maximum ) {
 109  
 
 110  6
         return new Between<E>( minimum, maximum );
 111  
     }
 112  
 
 113  
     /**
 114  
      * Answers a function which will give the greater of its two arguments.
 115  
      * <p/>
 116  
      * For purposes of this comparison, {@code null} always compares "less than"
 117  
      * non-{@code null}.
 118  
      *
 119  
      * @param <E> a constraint on the types of the function's arguments
 120  
      * @return function which will give the greater of its two arguments
 121  
      */
 122  
     public static <E extends Comparable<? super E>> BinaryFunction<E, E, E> max() {
 123  4
         return new Max<E>();
 124  
     }
 125  
 
 126  
     /**
 127  
      * Answers a function which will give the lesser of its two arguments.
 128  
      * <p/>
 129  
      * For purposes of this comparison, {@code null} always compares "less than"
 130  
      * non-{@code null}.
 131  
      *
 132  
      * @param <E> a constraint on the types of the function's arguments
 133  
      * @return function which will give the lesser of its two arguments
 134  
      */
 135  
     public static <E extends Comparable<? super E>> BinaryFunction<E, E, E> min() {
 136  4
         return new Min<E>();
 137  
     }
 138  
 
 139  
     private abstract static
 140  
     class NaturalOrderingPredicate<E extends Comparable<? super E>>
 141  
         extends BinaryPredicate<E, E> {
 142  
 
 143  
         protected static final String LESS_THAN_FORMAT = "a value less than <%1$s>";
 144  
         protected static final String GREATER_THAN_FORMAT = "a value greater than <%1$s>";
 145  
         protected static final String LESS_THAN_OR_EQUAL_TO_FORMAT =
 146  
             "a value less than or equal to <%1$s>";
 147  
         protected static final String GREATER_THAN_OR_EQUAL_TO_FORMAT =
 148  
             "a value greater than or equal to <%1$s>";
 149  
 
 150  
         protected final Comparator<E> comparator;
 151  
 
 152  32
         protected NaturalOrderingPredicate() {
 153  32
             comparator = Comparing.<E>byNaturalOrdering();
 154  32
         }
 155  
     }
 156  
 
 157  40
     private static class LessThan<E extends Comparable<? super E>>
 158  
         extends NaturalOrderingPredicate<E> {
 159  
 
 160  
         public boolean matches( E first, E second ) {
 161  18
             return comparator.compare( first, second ) < 0;
 162  
         }
 163  
 
 164  
         @Override
 165  
         protected String getSecondBoundDescription() {
 166  1
             return LESS_THAN_FORMAT;
 167  
         }
 168  
     }
 169  
 
 170  20
     private static class LessThanOrEqualTo<E extends Comparable<? super E>>
 171  
         extends NaturalOrderingPredicate<E> {
 172  
 
 173  
         public boolean matches( E first, E second ) {
 174  6
             return comparator.compare( first, second ) <= 0;
 175  
         }
 176  
 
 177  
         @Override
 178  
         protected String getSecondBoundDescription() {
 179  1
             return LESS_THAN_OR_EQUAL_TO_FORMAT;
 180  
         }
 181  
     }
 182  
 
 183  20
     private static class GreaterThan<E extends Comparable<? super E>>
 184  
         extends NaturalOrderingPredicate<E> {
 185  
 
 186  
         public boolean matches( E first, E second ) {
 187  6
             return comparator.compare( first, second ) > 0;
 188  
         }
 189  
 
 190  
         @Override
 191  
         protected String getSecondBoundDescription() {
 192  1
             return GREATER_THAN_FORMAT;
 193  
         }
 194  
     }
 195  
 
 196  20
     private static class GreaterThanOrEqualTo<E extends Comparable<? super E>>
 197  
         extends NaturalOrderingPredicate<E> {
 198  
 
 199  
         public boolean matches( E first, E second ) {
 200  6
             return comparator.compare( first, second ) >= 0;
 201  
         }
 202  
 
 203  
         @Override
 204  
         protected String getSecondBoundDescription() {
 205  1
             return GREATER_THAN_OR_EQUAL_TO_FORMAT;
 206  
         }
 207  
     }
 208  
 
 209  8
     private static class Between<E extends Comparable<? super E>>
 210  
         extends UnaryPredicate<E> {
 211  
 
 212  
         private final E first;
 213  
         private final E second;
 214  
 
 215  6
         Between( E first, E second ) {
 216  6
             this.first = first;
 217  6
             this.second = second;
 218  6
         }
 219  
 
 220  
         public boolean matches( E target ) {
 221  8
             Comparator<E> comparator = Comparing.<E>byNaturalOrdering();
 222  8
             return comparator.compare( first, target ) <= 0
 223  
                 && comparator.compare( target, second ) <= 0;
 224  
         }
 225  
 
 226  
         @Override
 227  
         public String describe() {
 228  1
             return format( "a value between <%1$s> and <%2$s>", first, second );
 229  
         }
 230  
     }
 231  
 
 232  12
     private static class Max<E extends Comparable<? super E>>
 233  
         extends BinaryFunction<E, E, E> {
 234  
 
 235  4
         private final Comparator<E> comparator = Comparing.<E>byNaturalOrdering();
 236  
 
 237  
         public E evaluate( E first, E second ) {
 238  4
             return comparator.compare( first, second ) >= 0 ? first : second;
 239  
         }
 240  
     }
 241  
 
 242  12
     private static class Min<E extends Comparable<? super E>>
 243  
         extends BinaryFunction<E, E, E> {
 244  
 
 245  4
         private final Comparator<E> comparator = Comparing.<E>byNaturalOrdering();
 246  
 
 247  
         public E evaluate( E first, E second ) {
 248  4
             return comparator.compare( first, second ) <= 0 ? first : second;
 249  
         }
 250  
     }
 251  
 }