1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.functor.adapter; 18 19 import java.io.Serializable; 20 21 import org.apache.commons.functor.BinaryFunction; 22 import org.apache.commons.functor.BinaryProcedure; 23 24 /** 25 * Adapts a 26 * {@link BinaryProcedure BinaryProcedure} 27 * to the 28 * {@link BinaryFunction BinaryFunction} interface 29 * by always returning <code>null</code>. 30 * <p/> 31 * Note that although this class implements 32 * {@link Serializable}, a given instance will 33 * only be truly <code>Serializable</code> if the 34 * underlying procedure is. Attempts to serialize 35 * an instance whose delegate is not 36 * <code>Serializable</code> will result in an exception. 37 * 38 * @param <L> the left argument type. 39 * @param <R> the right argument type. 40 * @param <T> the returned value type. 41 * @version $Revision: 1157600 $ $Date: 2011-08-14 21:22:50 +0200 (Sun, 14 Aug 2011) $ 42 * @author Rodney Waldhoff 43 */ 44 public final class BinaryProcedureBinaryFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable { 45 /** 46 * serialVersionUID declaration. 47 */ 48 private static final long serialVersionUID = 2099210273403668624L; 49 /** The {@link BinaryProcedure BinaryProcedure} I'm wrapping. */ 50 private final BinaryProcedure<? super L, ? super R> procedure; 51 52 /** 53 * Create a new BinaryProcedureBinaryFunction. 54 * @param procedure to adapt as a BinaryFunction 55 */ 56 public BinaryProcedureBinaryFunction(BinaryProcedure<? super L, ? super R> procedure) { 57 if (procedure == null) { 58 throw new IllegalArgumentException("BinaryProcedure argument was null"); 59 } 60 this.procedure = procedure; 61 } 62 63 /** 64 * {@inheritDoc} 65 */ 66 public T evaluate(L left, R right) { 67 procedure.run(left, right); 68 return null; 69 } 70 71 /** 72 * {@inheritDoc} 73 */ 74 public boolean equals(Object that) { 75 return that == this || (that instanceof BinaryProcedureBinaryFunction<?, ?, ?> 76 && equals((BinaryProcedureBinaryFunction<?, ?, ?>) that)); 77 } 78 79 /** 80 * Learn whether another BinaryProcedureBinaryFunction is equal to this. 81 * @param that the BinaryProcedureBinaryFunction to test 82 * @return boolean 83 */ 84 public boolean equals(BinaryProcedureBinaryFunction<?, ?, ?> that) { 85 return null != that && (null == procedure ? null == that.procedure : procedure.equals(that.procedure)); 86 } 87 88 /** 89 * {@inheritDoc} 90 */ 91 public int hashCode() { 92 int hash = "BinaryProcedureBinaryFunction".hashCode(); 93 if (null != procedure) { 94 hash ^= procedure.hashCode(); 95 } 96 return hash; 97 } 98 99 /** 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 }