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.UnaryFunction; 22 import org.apache.commons.functor.UnaryPredicate; 23 import org.apache.commons.lang3.Validate; 24 25 /** 26 * Adapts a <code>Boolean</code>-valued 27 * {@link UnaryFunction UnaryFunction} 28 * to the {@link UnaryPredicate UnaryPredicate} 29 * interface. 30 * <p/> 31 * Note that although this class implements 32 * {@link Serializable}, a given instance will 33 * only be truly <code>Serializable</code> if the 34 * underlying function is. Attempts to serialize 35 * an instance whose delegate is not 36 * <code>Serializable</code> will result in an exception. 37 * 38 * @param <A> the argument type. 39 * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $ 40 */ 41 public final class UnaryFunctionUnaryPredicate<A> implements UnaryPredicate<A>, Serializable { 42 /** 43 * serialVersionUID declaration. 44 */ 45 private static final long serialVersionUID = -9211927278252224707L; 46 /** The {@link UnaryFunction UnaryFunction} I'm wrapping. */ 47 private final UnaryFunction<? super A, Boolean> function; 48 49 /** 50 * Create an {@link UnaryPredicate UnaryPredicate} wrapping 51 * the given {@link UnaryFunction UnaryFunction}. 52 * @param function the {@link UnaryFunction UnaryFunction} to wrap 53 */ 54 public UnaryFunctionUnaryPredicate(UnaryFunction<? super A, Boolean> function) { 55 this.function = Validate.notNull(function, "UnaryFunction argument was null"); 56 } 57 58 /** 59 * {@inheritDoc} 60 * Returns the <code>boolean</code> value of the non-<code>null</code> 61 * <code>Boolean</code> returned by the {@link UnaryFunction#evaluate evaluate} 62 * method of my underlying function. 63 */ 64 public boolean test(A obj) { 65 return function.evaluate(obj).booleanValue(); 66 } 67 68 /** 69 * {@inheritDoc} 70 */ 71 @Override 72 public boolean equals(Object that) { 73 return that == this 74 || (that instanceof UnaryFunctionUnaryPredicate<?> && equals((UnaryFunctionUnaryPredicate<?>) that)); 75 } 76 77 /** 78 * Learn whether another UnaryFunctionUnaryPredicate is equal to this. 79 * @param that UnaryFunctionUnaryPredicate to test 80 * @return boolean 81 */ 82 public boolean equals(UnaryFunctionUnaryPredicate<?> that) { 83 return null != that && function.equals(that.function); 84 } 85 86 /** 87 * {@inheritDoc} 88 */ 89 @Override 90 public int hashCode() { 91 int hash = "UnaryFunctionUnaryPredicate".hashCode(); 92 hash ^= function.hashCode(); 93 return hash; 94 } 95 96 /** 97 * {@inheritDoc} 98 */ 99 @Override 100 public String toString() { 101 return "UnaryFunctionUnaryPredicate<" + function + ">"; 102 } 103 104 /** 105 * Adapt the given, possibly-<code>null</code>, 106 * {@link UnaryFunction UnaryFunction} to the 107 * {@link UnaryPredicate UnaryPredicate} interface. 108 * When the given <code>UnaryFunction</code> is <code>null</code>, 109 * returns <code>null</code>. 110 * 111 * @param <A> the argument type. 112 * @param function the possibly-<code>null</code> 113 * {@link UnaryFunction UnaryFunction} to adapt 114 * @return a {@link UnaryPredicate UnaryPredicate} wrapping the given 115 * {@link UnaryFunction UnaryFunction}, or <code>null</code> 116 * if the given <code>UnaryFunction</code> is <code>null</code> 117 */ 118 public static <A> UnaryFunctionUnaryPredicate<A> adapt(UnaryFunction<? super A, Boolean> function) { 119 return null == function ? null : new UnaryFunctionUnaryPredicate<A>(function); 120 } 121 122 }