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