001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.functor.adapter; 018 019 import java.io.Serializable; 020 021 import org.apache.commons.functor.UnaryFunction; 022 import org.apache.commons.functor.UnaryPredicate; 023 024 /** 025 * Adapts a 026 * {@link UnaryPredicate UnaryPredicate} 027 * to the 028 * {@link UnaryFunction UnaryFunction} interface. 029 * <p/> 030 * Note that although this class implements 031 * {@link Serializable}, a given instance will 032 * only be truly <code>Serializable</code> if the 033 * underlying predicate is. Attempts to serialize 034 * an instance whose delegate is not 035 * <code>Serializable</code> will result in an exception. 036 * 037 * @param <A> the argument type. 038 * @version $Revision: 1157655 $ $Date: 2011-08-14 22:25:24 +0200 (Sun, 14 Aug 2011) $ 039 * @author Rodney Waldhoff 040 */ 041 public final class UnaryPredicateUnaryFunction<A> implements UnaryFunction<A, Boolean>, Serializable { 042 /** 043 * serialVersionUID declaration. 044 */ 045 private static final long serialVersionUID = 5660724725036398625L; 046 /** The {@link UnaryPredicate UnaryPredicate} I'm wrapping. */ 047 private final UnaryPredicate<? super A> predicate; 048 049 /** 050 * Create a new UnaryPredicateUnaryFunction. 051 * @param predicate to adapt 052 */ 053 public UnaryPredicateUnaryFunction(UnaryPredicate<? super A> predicate) { 054 if (predicate == null) { 055 throw new IllegalArgumentException("UnaryPredicate argument was null"); 056 } 057 this.predicate = predicate; 058 } 059 060 /** 061 * {@inheritDoc} 062 * Returns <code>Boolean.TRUE</code> (<code>Boolean.FALSE</code>) 063 * when the {@link UnaryPredicate#test test} method of my underlying 064 * predicate returns <code>true</code> (<code>false</code>). 065 * 066 * @return a non-<code>null</code> <code>Boolean</code> instance 067 */ 068 public Boolean evaluate(A obj) { 069 return predicate.test(obj); 070 } 071 072 /** 073 * {@inheritDoc} 074 */ 075 public boolean equals(Object that) { 076 return that == this 077 || (that instanceof UnaryPredicateUnaryFunction<?> && equals((UnaryPredicateUnaryFunction<?>) that)); 078 } 079 080 /** 081 * Learn whether another UnaryPredicateUnaryFunction is equal to this. 082 * @param that UnaryPredicateUnaryFunction to test 083 * @return boolean 084 */ 085 public boolean equals(UnaryPredicateUnaryFunction<?> that) { 086 return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate)); 087 } 088 089 /** 090 * {@inheritDoc} 091 */ 092 public int hashCode() { 093 int hash = "UnaryPredicateUnaryFunction".hashCode(); 094 if (null != predicate) { 095 hash ^= predicate.hashCode(); 096 } 097 return hash; 098 } 099 100 /** 101 * {@inheritDoc} 102 */ 103 public String toString() { 104 return "UnaryPredicateUnaryFunction<" + predicate + ">"; 105 } 106 107 /** 108 * Adapt a UnaryPredicate to the UnaryFunction interface. 109 * @param <A> the argument type. 110 * @param predicate to adapt 111 * @return UnaryPredicateUnaryFunction 112 */ 113 public static <A> UnaryPredicateUnaryFunction<A> adapt(UnaryPredicate<? super A> predicate) { 114 return null == predicate ? null : new UnaryPredicateUnaryFunction<A>(predicate); 115 } 116 117 }