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