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 */ 017package org.apache.commons.functor.adapter; 018 019import org.apache.commons.functor.BinaryFunction; 020import org.apache.commons.functor.BinaryPredicate; 021 022/** 023 * Adapts a 024 * {@link BinaryPredicate BinaryPredicate} 025 * to the 026 * {@link BinaryFunction BinaryFunction} interface. 027 * 028 * @param <L> the left argument type. 029 * @param <R> the right argument type. 030 * @version $Revision: 1537906 $ $Date: 2013-11-01 12:47:33 +0100 (Fr, 01 Nov 2013) $ 031 */ 032public final class BinaryPredicateBinaryFunction<L, R> implements BinaryFunction<L, R, Boolean> { 033 /** The {@link BinaryPredicate BinaryPredicate} I'm wrapping. */ 034 private final BinaryPredicate<? super L, ? super R> predicate; 035 036 /** 037 * Create a new BinaryPredicateBinaryFunction. 038 * @param predicate to adapt 039 */ 040 public BinaryPredicateBinaryFunction(BinaryPredicate<? super L, ? super R> predicate) { 041 this.predicate = predicate; 042 } 043 044 /** 045 * {@inheritDoc} 046 * Returns <code>Boolean.TRUE</code> (<code>Boolean.FALSE</code>) 047 * when the {@link BinaryPredicate#test test} method of my underlying 048 * predicate returns <code>true</code> (<code>false</code>). 049 * 050 * @return a non-<code>null</code> <code>Boolean</code> instance 051 */ 052 public Boolean evaluate(L left, R right) { 053 return predicate.test(left, right) ? Boolean.TRUE : Boolean.FALSE; 054 } 055 056 /** 057 * {@inheritDoc} 058 */ 059 @Override 060 public boolean equals(Object obj) { 061 if (obj == this) { 062 return true; 063 } 064 if (!(obj instanceof BinaryPredicateBinaryFunction<?, ?>)) { 065 return false; 066 } 067 BinaryPredicateBinaryFunction<?, ?> that = (BinaryPredicateBinaryFunction<?, ?>) obj; 068 return this.predicate.equals(that.predicate); 069 } 070 071 /** 072 * {@inheritDoc} 073 */ 074 @Override 075 public int hashCode() { 076 int hash = "BinaryPredicateBinaryFunction".hashCode(); 077 hash ^= predicate.hashCode(); 078 return hash; 079 } 080 081 /** 082 * {@inheritDoc} 083 */ 084 @Override 085 public String toString() { 086 return "BinaryPredicateBinaryFunction<" + predicate + ">"; 087 } 088 089 /** 090 * Adapt the given, possibly-<code>null</code>, 091 * {@link BinaryPredicate BinaryPredicate} to the 092 * {@link BinaryFunction BinaryFunction} interface. 093 * When the given <code>BinaryPredicate</code> is <code>null</code>, 094 * returns <code>null</code>. 095 * 096 * @param <L> left type 097 * @param <R> right type 098 * @param predicate the possibly-<code>null</code> 099 * {@link BinaryPredicate BinaryPredicate} to adapt 100 * @return a <code>BinaryPredicateBinaryFunction</code> wrapping the given 101 * {@link BinaryPredicate BinaryPredicate}, or <code>null</code> 102 * if the given <code>BinaryPredicate</code> is <code>null</code> 103 */ 104 public static <L, R> BinaryPredicateBinaryFunction<L, R> adapt(BinaryPredicate<? super L, ? super R> predicate) { 105 return null == predicate ? null : new BinaryPredicateBinaryFunction<L, R>(predicate); 106 } 107 108}