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.BinaryProcedure; 023 024 /** 025 * Adapts a 026 * {@link BinaryProcedure BinaryProcedure} 027 * to the 028 * {@link BinaryFunction BinaryFunction} interface 029 * by always returning <code>null</code>. 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 procedure 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 * @param <T> the returned value type. 041 * @version $Revision: 1157600 $ $Date: 2011-08-14 21:22:50 +0200 (Sun, 14 Aug 2011) $ 042 * @author Rodney Waldhoff 043 */ 044 public final class BinaryProcedureBinaryFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable { 045 /** 046 * serialVersionUID declaration. 047 */ 048 private static final long serialVersionUID = 2099210273403668624L; 049 /** The {@link BinaryProcedure BinaryProcedure} I'm wrapping. */ 050 private final BinaryProcedure<? super L, ? super R> procedure; 051 052 /** 053 * Create a new BinaryProcedureBinaryFunction. 054 * @param procedure to adapt as a BinaryFunction 055 */ 056 public BinaryProcedureBinaryFunction(BinaryProcedure<? super L, ? super R> procedure) { 057 if (procedure == null) { 058 throw new IllegalArgumentException("BinaryProcedure argument was null"); 059 } 060 this.procedure = procedure; 061 } 062 063 /** 064 * {@inheritDoc} 065 */ 066 public T evaluate(L left, R right) { 067 procedure.run(left, right); 068 return null; 069 } 070 071 /** 072 * {@inheritDoc} 073 */ 074 public boolean equals(Object that) { 075 return that == this || (that instanceof BinaryProcedureBinaryFunction<?, ?, ?> 076 && equals((BinaryProcedureBinaryFunction<?, ?, ?>) that)); 077 } 078 079 /** 080 * Learn whether another BinaryProcedureBinaryFunction is equal to this. 081 * @param that the BinaryProcedureBinaryFunction to test 082 * @return boolean 083 */ 084 public boolean equals(BinaryProcedureBinaryFunction<?, ?, ?> that) { 085 return null != that && (null == procedure ? null == that.procedure : procedure.equals(that.procedure)); 086 } 087 088 /** 089 * {@inheritDoc} 090 */ 091 public int hashCode() { 092 int hash = "BinaryProcedureBinaryFunction".hashCode(); 093 if (null != procedure) { 094 hash ^= procedure.hashCode(); 095 } 096 return hash; 097 } 098 099 /** 100 * {@inheritDoc} 101 */ 102 public String toString() { 103 return "BinaryProcedureBinaryFunction<" + procedure + ">"; 104 } 105 106 /** 107 * Adapt the given, possibly-<code>null</code>, 108 * {@link BinaryProcedure BinaryProcedure} to the 109 * {@link BinaryFunction BinaryFunction} interface. 110 * When the given <code>BinaryProcedure</code> is <code>null</code>, 111 * returns <code>null</code>. 112 * 113 * @param <L> the left argument type. 114 * @param <R> the right argument type. 115 * @param <T> the returned value type. 116 * @param procedure the possibly-<code>null</code> 117 * {@link BinaryFunction BinaryFunction} to adapt 118 * @return a <code>BinaryProcedureBinaryFunction</code> wrapping the given 119 * {@link BinaryFunction BinaryFunction}, or <code>null</code> 120 * if the given <code>BinaryFunction</code> is <code>null</code> 121 */ 122 public static <L, R, T> BinaryProcedureBinaryFunction<L, R, T> adapt(BinaryProcedure<? super L, ? super R> procedure) { 123 return null == procedure ? null : new BinaryProcedureBinaryFunction<L, R, T>(procedure); 124 } 125 126 }