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.BinaryFunction; 022 import org.apache.commons.functor.BinaryPredicate; 023 024 /** 025 * Adapts a 026 * {@link BinaryPredicate BinaryPredicate} 027 * to the 028 * {@link BinaryFunction BinaryFunction} 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 <L> the left argument type. 038 * @param <R> the right argument type. 039 * @version $Revision: 1156767 $ $Date: 2011-08-11 21:38:12 +0200 (Thu, 11 Aug 2011) $ 040 * @author Rodney Waldhoff 041 */ 042 public final class BinaryPredicateBinaryFunction<L, R> implements BinaryFunction<L, R, Boolean>, Serializable { 043 /** 044 * serialVersionUID declaration. 045 */ 046 private static final long serialVersionUID = 207209665276797678L; 047 /** The {@link BinaryPredicate BinaryPredicate} I'm wrapping. */ 048 private final BinaryPredicate<? super L, ? super R> predicate; 049 050 /** 051 * Create a new BinaryPredicateBinaryFunction. 052 * @param predicate to adapt 053 */ 054 public BinaryPredicateBinaryFunction(BinaryPredicate<? super L, ? super R> predicate) { 055 this.predicate = predicate; 056 } 057 058 /** 059 * {@inheritDoc} 060 * Returns <code>Boolean.TRUE</code> (<code>Boolean.FALSE</code>) 061 * when the {@link BinaryPredicate#test test} method of my underlying 062 * predicate returns <code>true</code> (<code>false</code>). 063 * 064 * @return a non-<code>null</code> <code>Boolean</code> instance 065 */ 066 public Boolean evaluate(L left, R right) { 067 return predicate.test(left, right) ? Boolean.TRUE : Boolean.FALSE; 068 } 069 070 /** 071 * {@inheritDoc} 072 */ 073 public boolean equals(Object that) { 074 return that == this 075 || (that instanceof BinaryPredicateBinaryFunction<?, ?> 076 && equals((BinaryPredicateBinaryFunction<?, ?>) that)); 077 } 078 079 /** 080 * Learn whether another BinaryPredicateBinaryFunction is equal to this. 081 * @param that BinaryPredicateBinaryFunction to test 082 * @return boolean 083 */ 084 public boolean equals(BinaryPredicateBinaryFunction<?, ?> that) { 085 return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate)); 086 } 087 088 /** 089 * {@inheritDoc} 090 */ 091 public int hashCode() { 092 int hash = "BinaryPredicateBinaryFunction".hashCode(); 093 if (null != predicate) { 094 hash ^= predicate.hashCode(); 095 } 096 return hash; 097 } 098 099 /** 100 * {@inheritDoc} 101 */ 102 public String toString() { 103 return "BinaryPredicateBinaryFunction<" + predicate + ">"; 104 } 105 106 /** 107 * Adapt the given, possibly-<code>null</code>, 108 * {@link BinaryPredicate BinaryPredicate} to the 109 * {@link BinaryFunction BinaryFunction} interface. 110 * When the given <code>BinaryPredicate</code> is <code>null</code>, 111 * returns <code>null</code>. 112 * 113 * @param <L> left type 114 * @param <R> right type 115 * @param predicate the possibly-<code>null</code> 116 * {@link BinaryPredicate BinaryPredicate} to adapt 117 * @return a <code>BinaryPredicateBinaryFunction</code> wrapping the given 118 * {@link BinaryPredicate BinaryPredicate}, or <code>null</code> 119 * if the given <code>BinaryPredicate</code> is <code>null</code> 120 */ 121 public static <L, R> BinaryPredicateBinaryFunction<L, R> adapt(BinaryPredicate<? super L, ? super R> predicate) { 122 return null == predicate ? null : new BinaryPredicateBinaryFunction<L, R>(predicate); 123 } 124 125 }