| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| IsLessThan |
|
| 1.8888888888888888;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: 1166345 $ $Date: 2011-09-07 21:47:15 +0200 (Wed, 07 Sep 2011) $ | |
| 34 | * @author Rodney Waldhoff | |
| 35 | */ | |
| 36 | public final class IsLessThan<T> implements BinaryPredicate<T, T>, Serializable { | |
| 37 | ||
| 38 | /** | |
| 39 | * Basic IsLessThan instance. | |
| 40 | */ | |
| 41 | 2 | public static final IsLessThan<Comparable<?>> INSTANCE = IsLessThan.<Comparable<?>>instance(); |
| 42 | ||
| 43 | /** | |
| 44 | * serialVersionUID declaration. | |
| 45 | */ | |
| 46 | private static final long serialVersionUID = 1809153179639613842L; | |
| 47 | ||
| 48 | private final Comparator<? super T> comparator; | |
| 49 | ||
| 50 | /** | |
| 51 | * Construct a <code>IsLessThan</code> {@link BinaryPredicate predicate} | |
| 52 | * for {@link Comparable Comparable}s. | |
| 53 | */ | |
| 54 | @SuppressWarnings("unchecked") | |
| 55 | public IsLessThan() { | |
| 56 | 30 | this(ComparableComparator.INSTANCE); |
| 57 | 30 | } |
| 58 | ||
| 59 | /** | |
| 60 | * Construct a <code>IsLessThan</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 | 32 | public IsLessThan(Comparator<? super T> comparator) { |
| 68 | 32 | if (comparator == null) { |
| 69 | 0 | throw new IllegalArgumentException("Comparator argument must not be null"); |
| 70 | } | |
| 71 | 32 | this.comparator = comparator; |
| 72 | 32 | } |
| 73 | ||
| 74 | /** | |
| 75 | * Return <code>true</code> iff the <i>left</i> parameter is | |
| 76 | * less 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 | 122 | 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 IsLessThan<?> && equals((IsLessThan<?>) that)); |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * Learn whether a given IsLessThan is equal to this. | |
| 93 | * @param that IsLessThan to test | |
| 94 | * @return boolean | |
| 95 | */ | |
| 96 | public boolean equals(IsLessThan<?> 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 = "IsLessThan".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 "IsLessThan<" + comparator + ">"; |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * Get a typed IsLessThan instance. | |
| 125 | * @param <T> | |
| 126 | * @return IsLessThan<T> | |
| 127 | */ | |
| 128 | public static <T extends Comparable<?>> IsLessThan<T> instance() { | |
| 129 | 8 | return new IsLessThan<T>(); |
| 130 | } | |
| 131 | ||
| 132 | /** | |
| 133 | * Get an IsLessThan 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 | 4 | return RightBoundPredicate.bind(new IsLessThan<T>(), right); |
| 139 | } | |
| 140 | ||
| 141 | } |