| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| PredicateFunction |
|
| 2.142857142857143;2.143 |
| 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 java.io.Serializable; | |
| 20 | ||
| 21 | import org.apache.commons.functor.Function; | |
| 22 | import org.apache.commons.functor.Predicate; | |
| 23 | ||
| 24 | /** | |
| 25 | * Adapts a | |
| 26 | * {@link Predicate Predicate} | |
| 27 | * to the | |
| 28 | * {@link Function Function} interface. | |
| 29 | * <p/> | |
| 30 | * Note that although this class implements | |
| 31 | * {@link Serializable}, a given instance will | |
| 32 | * only be truly <code>Serializable</code> if the | |
| 33 | * underlying predicate is. Attempts to serialize | |
| 34 | * an instance whose delegate is not | |
| 35 | * <code>Serializable</code> will result in an exception. | |
| 36 | * | |
| 37 | * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $ | |
| 38 | * @author Rodney Waldhoff | |
| 39 | */ | |
| 40 | 4 | public final class PredicateFunction implements Function<Boolean>, Serializable { |
| 41 | /** | |
| 42 | * serialVersionUID declaration. | |
| 43 | */ | |
| 44 | private static final long serialVersionUID = -8858981355549412629L; | |
| 45 | /** The {@link Predicate Predicate} I'm wrapping. */ | |
| 46 | private final Predicate predicate; | |
| 47 | ||
| 48 | /** | |
| 49 | * Create a new PredicateFunction. | |
| 50 | * @param predicate to adapt | |
| 51 | */ | |
| 52 | 20 | public PredicateFunction(Predicate predicate) { |
| 53 | 20 | if (predicate == null) { |
| 54 | 0 | throw new IllegalArgumentException("Predicate argument was null"); |
| 55 | } | |
| 56 | 20 | this.predicate = predicate; |
| 57 | 20 | } |
| 58 | ||
| 59 | /** | |
| 60 | * {@inheritDoc} | |
| 61 | * Returns <code>Boolean.TRUE</code> (<code>Boolean.FALSE</code>) | |
| 62 | * when the {@link Predicate#test test} method of my underlying | |
| 63 | * predicate returns <code>true</code> (<code>false</code>). | |
| 64 | * | |
| 65 | * @return a non-<code>null</code> <code>Boolean</code> instance | |
| 66 | */ | |
| 67 | public Boolean evaluate() { | |
| 68 | 4 | return predicate.test(); |
| 69 | } | |
| 70 | ||
| 71 | /** | |
| 72 | * {@inheritDoc} | |
| 73 | */ | |
| 74 | public boolean equals(Object that) { | |
| 75 | 22 | return that == this || (that instanceof PredicateFunction && equals((PredicateFunction) that)); |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * Learn whether another PredicateFunction is equal to this. | |
| 80 | * @param that PredicateFunction to test | |
| 81 | * @return boolean | |
| 82 | */ | |
| 83 | public boolean equals(PredicateFunction that) { | |
| 84 | 14 | return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate)); |
| 85 | } | |
| 86 | ||
| 87 | /** | |
| 88 | * {@inheritDoc} | |
| 89 | */ | |
| 90 | public int hashCode() { | |
| 91 | 24 | int hash = "PredicateFunction".hashCode(); |
| 92 | 24 | if (null != predicate) { |
| 93 | 24 | hash ^= predicate.hashCode(); |
| 94 | } | |
| 95 | 24 | return hash; |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * {@inheritDoc} | |
| 100 | */ | |
| 101 | public String toString() { | |
| 102 | 16 | return "PredicateFunction<" + predicate + ">"; |
| 103 | } | |
| 104 | ||
| 105 | /** | |
| 106 | * Adapt a Predicate to the Function interface. | |
| 107 | * @param predicate to adapt | |
| 108 | * @return PredicateFunction | |
| 109 | */ | |
| 110 | public static PredicateFunction adapt(Predicate predicate) { | |
| 111 | 2 | return null == predicate ? null : new PredicateFunction(predicate); |
| 112 | } | |
| 113 | ||
| 114 | } |