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 java.io.Serializable; 020 021import org.apache.commons.functor.BinaryFunction; 022import org.apache.commons.functor.BinaryProcedure; 023import org.apache.commons.lang3.Validate; 024 025/** 026 * Adapts a {@link BinaryFunction BinaryFunction} 027 * to the {@link BinaryProcedure BinaryProcedure} 028 * interface by ignoring the value returned 029 * by the function. 030 * <p/> 031 * Note that although this class implements 032 * {@link Serializable}, a given instance will 033 * only be truly <code>Serializable</code> if the 034 * underlying function is. Attempts to serialize 035 * an instance whose delegate is not 036 * <code>Serializable</code> will result in an exception. 037 * 038 * @param <L> the left argument type. 039 * @param <R> the right argument type. 040 * @version $Revision: 1365377 $ $Date: 2012-07-24 20:59:23 -0400 (Tue, 24 Jul 2012) $ 041 */ 042public final class BinaryFunctionBinaryProcedure<L, R> implements BinaryProcedure<L, R>, Serializable { 043 044 /** 045 * serialVersionUID declaration. 046 */ 047 private static final long serialVersionUID = 4498276997127058865L; 048 /** The {@link BinaryFunction BinaryFunction} I'm wrapping. */ 049 private final BinaryFunction<? super L, ? super R, ?> function; 050 051 /** 052 * Create an {@link BinaryProcedure BinaryProcedure} wrapping 053 * the given {@link BinaryFunction BinaryFunction}. 054 * @param function the {@link BinaryFunction BinaryFunction} to wrap 055 */ 056 public BinaryFunctionBinaryProcedure(BinaryFunction<? super L, ? super R, ?> function) { 057 this.function = Validate.notNull(function, "BinaryFunction argument was null"); 058 } 059 060 /** 061 * {@link BinaryFunction#evaluate Evaluate} my function, but 062 * ignore its returned value. 063 * {@inheritDoc} 064 */ 065 public void run(L left, R right) { 066 function.evaluate(left, right); 067 } 068 069 /** 070 * {@inheritDoc} 071 */ 072 @Override 073 public boolean equals(Object that) { 074 return that == this 075 || (that instanceof BinaryFunctionBinaryProcedure<?, ?> 076 && equals((BinaryFunctionBinaryProcedure<?, ?>) that)); 077 } 078 079 /** 080 * Learn whether a given BinaryFunctionBinaryPredicate is equal to this. 081 * @param that BinaryFunctionBinaryPredicate to compare 082 * @return boolean 083 */ 084 public boolean equals(BinaryFunctionBinaryProcedure<?, ?> that) { 085 return null != that && function.equals(that.function); 086 } 087 088 /** 089 * {@inheritDoc} 090 */ 091 @Override 092 public int hashCode() { 093 int hash = "BinaryFunctionBinaryProcedure".hashCode(); 094 hash ^= function.hashCode(); 095 return hash; 096 } 097 098 /** 099 * {@inheritDoc} 100 */ 101 @Override 102 public String toString() { 103 return "BinaryFunctionBinaryProcedure<" + function + ">"; 104 } 105 106 /** 107 * Adapt the given, possibly-<code>null</code>, 108 * {@link BinaryFunction BinaryFunction} to the 109 * {@link BinaryProcedure BinaryProcedure} interface. 110 * When the given <code>BinaryFunction</code> is <code>null</code>, 111 * returns <code>null</code>. 112 * 113 * @param <L> left type 114 * @param <R> right type 115 * @param function the possibly-<code>null</code> 116 * {@link BinaryFunction BinaryFunction} to adapt 117 * @return a <code>BinaryFunctionBinaryProcedure</code> wrapping the given 118 * {@link BinaryFunction BinaryFunction}, or <code>null</code> 119 * if the given <code>BinaryFunction</code> is <code>null</code> 120 */ 121 public static <L, R> BinaryFunctionBinaryProcedure<L, R> adapt(BinaryFunction<? super L, ? super R, ?> function) { 122 return null == function ? null : new BinaryFunctionBinaryProcedure<L, R>(function); 123 } 124 125}