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 import org.apache.commons.lang3.Validate; 24 25 /** 26 * Adapts a 27 * {@link BinaryProcedure BinaryProcedure} 28 * to the 29 * {@link BinaryFunction BinaryFunction} interface 30 * by always returning <code>null</code>. 31 * <p/> 32 * Note that although this class implements 33 * {@link Serializable}, a given instance will 34 * only be truly <code>Serializable</code> if the 35 * underlying procedure is. Attempts to serialize 36 * an instance whose delegate is not 37 * <code>Serializable</code> will result in an exception. 38 * 39 * @param <L> the left argument type. 40 * @param <R> the right argument type. 41 * @param <T> the returned value type. 42 * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $ 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 this.procedure = Validate.notNull(procedure, "BinaryProcedure argument was null"); 58 } 59 60 /** 61 * {@inheritDoc} 62 */ 63 public T evaluate(L left, R right) { 64 procedure.run(left, right); 65 return null; 66 } 67 68 /** 69 * {@inheritDoc} 70 */ 71 @Override 72 public boolean equals(Object that) { 73 return that == this || (that instanceof BinaryProcedureBinaryFunction<?, ?, ?> 74 && equals((BinaryProcedureBinaryFunction<?, ?, ?>) that)); 75 } 76 77 /** 78 * Learn whether another BinaryProcedureBinaryFunction is equal to this. 79 * @param that the BinaryProcedureBinaryFunction to test 80 * @return boolean 81 */ 82 public boolean equals(BinaryProcedureBinaryFunction<?, ?, ?> that) { 83 return null != that && procedure.equals(that.procedure); 84 } 85 86 /** 87 * {@inheritDoc} 88 */ 89 @Override 90 public int hashCode() { 91 int hash = "BinaryProcedureBinaryFunction".hashCode(); 92 hash ^= procedure.hashCode(); 93 return hash; 94 } 95 96 /** 97 * {@inheritDoc} 98 */ 99 @Override 100 public String toString() { 101 return "BinaryProcedureBinaryFunction<" + procedure + ">"; 102 } 103 104 /** 105 * Adapt the given, possibly-<code>null</code>, 106 * {@link BinaryProcedure BinaryProcedure} to the 107 * {@link BinaryFunction BinaryFunction} interface. 108 * When the given <code>BinaryProcedure</code> is <code>null</code>, 109 * returns <code>null</code>. 110 * 111 * @param <L> the left argument type. 112 * @param <R> the right argument type. 113 * @param <T> the returned value type. 114 * @param procedure the possibly-<code>null</code> 115 * {@link BinaryFunction BinaryFunction} to adapt 116 * @return a <code>BinaryProcedureBinaryFunction</code> wrapping the given 117 * {@link BinaryFunction BinaryFunction}, or <code>null</code> 118 * if the given <code>BinaryFunction</code> is <code>null</code> 119 */ 120 public static <L, R, T> BinaryProcedureBinaryFunction<L, R, T> adapt( 121 BinaryProcedure<? super L, ? super R> procedure) { 122 return null == procedure ? null : new BinaryProcedureBinaryFunction<L, R, T>(procedure); 123 } 124 125 }