| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| BinaryPredicateUnaryPredicate |
|
| 1.8571428571428572;1.857 |
| 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.adapter; | |
| 18 | ||
| 19 | import org.apache.commons.functor.BinaryPredicate; | |
| 20 | import org.apache.commons.functor.UnaryPredicate; | |
| 21 | ||
| 22 | /** | |
| 23 | * Adapts a BinaryPredicate as a UnaryPredicate by sending the same argument to both sides of the BinaryPredicate. | |
| 24 | * @param <A> the argument type. | |
| 25 | * @version $Revision: 1156771 $ $Date: 2011-08-11 21:42:56 +0200 (Thu, 11 Aug 2011) $ | |
| 26 | * @author Matt Benson | |
| 27 | */ | |
| 28 | public final class BinaryPredicateUnaryPredicate<A> implements UnaryPredicate<A> { | |
| 29 | /** | |
| 30 | * The adapted {@link BinaryPredicate}. | |
| 31 | */ | |
| 32 | private final BinaryPredicate<? super A, ? super A> predicate; | |
| 33 | ||
| 34 | /** | |
| 35 | * Create a new BinaryPredicateUnaryPredicate. | |
| 36 | * @param predicate to adapt | |
| 37 | */ | |
| 38 | 20 | public BinaryPredicateUnaryPredicate(BinaryPredicate<? super A, ? super A> predicate) { |
| 39 | 20 | if (null == predicate) { |
| 40 | 0 | throw new IllegalArgumentException("BinaryPredicate argument was null"); |
| 41 | } | |
| 42 | 20 | this.predicate = predicate; |
| 43 | 20 | } |
| 44 | ||
| 45 | /** | |
| 46 | * {@inheritDoc} | |
| 47 | */ | |
| 48 | public boolean test(A obj) { | |
| 49 | 4 | return predicate.test(obj, obj); |
| 50 | } | |
| 51 | ||
| 52 | /** | |
| 53 | * {@inheritDoc} | |
| 54 | */ | |
| 55 | @Override | |
| 56 | public boolean equals(Object obj) { | |
| 57 | 20 | return obj == this || obj instanceof BinaryPredicateUnaryPredicate<?> |
| 58 | && equals((BinaryPredicateUnaryPredicate<?>) obj); | |
| 59 | } | |
| 60 | ||
| 61 | /** | |
| 62 | * Learn whether another BinaryPredicateUnaryPredicate is equal to | |
| 63 | * <code>this</code>. | |
| 64 | * | |
| 65 | * @param that BinaryPredicateUnaryPredicate to check | |
| 66 | * | |
| 67 | * @return whether equal | |
| 68 | */ | |
| 69 | public boolean equals(BinaryPredicateUnaryPredicate<?> that) { | |
| 70 | 12 | return that != null && that.predicate.equals(this.predicate); |
| 71 | } | |
| 72 | ||
| 73 | /** | |
| 74 | * {@inheritDoc} | |
| 75 | */ | |
| 76 | @Override | |
| 77 | public int hashCode() { | |
| 78 | 20 | return ("BinaryPredicateUnaryPredicate".hashCode() << 2) | predicate.hashCode(); |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * {@inheritDoc} | |
| 83 | */ | |
| 84 | @Override | |
| 85 | public String toString() { | |
| 86 | 16 | return "BinaryPredicateUnaryPredicate<" + predicate + ">"; |
| 87 | } | |
| 88 | ||
| 89 | /** | |
| 90 | * Adapt a BinaryFunction as a UnaryFunction. | |
| 91 | * @param <A> the argument type. | |
| 92 | * @param predicate BinaryPredicate to adapt | |
| 93 | * @return UnaryPredicate<A> | |
| 94 | */ | |
| 95 | public static <A> UnaryPredicate<A> adapt(BinaryPredicate<? super A, ? super A> predicate) { | |
| 96 | 4 | return null == predicate ? null : new BinaryPredicateUnaryPredicate<A>(predicate); |
| 97 | } | |
| 98 | ||
| 99 | } |