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.BinaryProcedure; 021import org.apache.commons.lang3.Validate; 022 023/** 024 * Adapts a {@link BinaryFunction BinaryFunction} 025 * to the {@link BinaryProcedure BinaryProcedure} 026 * interface by ignoring the value returned 027 * by the function. 028 * 029 * @param <L> the left argument type. 030 * @param <R> the right argument type. 031 * @version $Revision: 1537906 $ $Date: 2013-11-01 12:47:33 +0100 (Fr, 01 Nov 2013) $ 032 */ 033public final class BinaryFunctionBinaryProcedure<L, R> implements BinaryProcedure<L, R> { 034 035 /** The {@link BinaryFunction BinaryFunction} I'm wrapping. */ 036 private final BinaryFunction<? super L, ? super R, ?> function; 037 038 /** 039 * Create an {@link BinaryProcedure BinaryProcedure} wrapping 040 * the given {@link BinaryFunction BinaryFunction}. 041 * @param function the {@link BinaryFunction BinaryFunction} to wrap 042 */ 043 public BinaryFunctionBinaryProcedure(BinaryFunction<? super L, ? super R, ?> function) { 044 this.function = Validate.notNull(function, "BinaryFunction argument was null"); 045 } 046 047 /** 048 * {@link BinaryFunction#evaluate Evaluate} my function, but 049 * ignore its returned value. 050 * {@inheritDoc} 051 */ 052 public void run(L left, R right) { 053 function.evaluate(left, right); 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 BinaryFunctionBinaryProcedure<?, ?>)) { 065 return false; 066 } 067 BinaryFunctionBinaryProcedure<?, ?> that = (BinaryFunctionBinaryProcedure<?, ?>) obj; 068 return this.function.equals(that.function); 069 } 070 071 /** 072 * {@inheritDoc} 073 */ 074 @Override 075 public int hashCode() { 076 int hash = "BinaryFunctionBinaryProcedure".hashCode(); 077 hash ^= function.hashCode(); 078 return hash; 079 } 080 081 /** 082 * {@inheritDoc} 083 */ 084 @Override 085 public String toString() { 086 return "BinaryFunctionBinaryProcedure<" + function + ">"; 087 } 088 089 /** 090 * Adapt the given, possibly-<code>null</code>, 091 * {@link BinaryFunction BinaryFunction} to the 092 * {@link BinaryProcedure BinaryProcedure} interface. 093 * When the given <code>BinaryFunction</code> is <code>null</code>, 094 * returns <code>null</code>. 095 * 096 * @param <L> left type 097 * @param <R> right type 098 * @param function the possibly-<code>null</code> 099 * {@link BinaryFunction BinaryFunction} to adapt 100 * @return a <code>BinaryFunctionBinaryProcedure</code> wrapping the given 101 * {@link BinaryFunction BinaryFunction}, or <code>null</code> 102 * if the given <code>BinaryFunction</code> is <code>null</code> 103 */ 104 public static <L, R> BinaryFunctionBinaryProcedure<L, R> adapt(BinaryFunction<? super L, ? super R, ?> function) { 105 return null == function ? null : new BinaryFunctionBinaryProcedure<L, R>(function); 106 } 107 108}