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