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.Procedure; 22 import org.apache.commons.functor.UnaryProcedure; 23 import org.apache.commons.lang3.Validate; 24 25 /** 26 * Adapts a 27 * {@link UnaryProcedure UnaryProcedure} 28 * to the 29 * {@link Procedure Procedure} interface 30 * using a constant unary argument. 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 objects are. Attempts to serialize 36 * an instance whose delegates are not 37 * <code>Serializable</code> will result in an exception. 38 * 39 * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $ 40 */ 41 public final class BoundProcedure implements Procedure, Serializable { 42 /** 43 * serialVersionUID declaration. 44 */ 45 private static final long serialVersionUID = -6010802933400471747L; 46 /** The {@link UnaryProcedure UnaryProcedure} I'm wrapping. */ 47 private final UnaryProcedure<Object> procedure; 48 /** The parameter to pass to {@code procedure}. */ 49 private final Object param; 50 51 /** 52 * Create a new BoundProcedure instance. 53 * @param <A> arg type 54 * @param procedure the procedure to adapt 55 * @param arg the constant argument to use 56 */ 57 @SuppressWarnings("unchecked") 58 public <A> BoundProcedure(UnaryProcedure<? super A> procedure, A arg) { 59 this.procedure = 60 (UnaryProcedure<Object>) Validate.notNull(procedure, 61 "UnaryProcedure argument was null"); 62 this.param = arg; 63 } 64 65 /** 66 * {@inheritDoc} 67 */ 68 public void run() { 69 procedure.run(param); 70 } 71 72 /** 73 * {@inheritDoc} 74 */ 75 @Override 76 public boolean equals(Object that) { 77 return that == this || (that instanceof BoundProcedure && equals((BoundProcedure) that)); 78 } 79 80 /** 81 * Learn whether a given BoundProcedure is equal to this. 82 * @param that the BoundProcedure to test 83 * @return boolean 84 */ 85 public boolean equals(BoundProcedure that) { 86 return null != that 87 && procedure.equals(that.procedure) 88 && (null == param ? null == that.param : param.equals(that.param)); 89 } 90 91 /** 92 * {@inheritDoc} 93 */ 94 @Override 95 public int hashCode() { 96 int hash = "BoundProcedure".hashCode(); 97 hash <<= 2; 98 hash ^= procedure.hashCode(); 99 if (null != param) { 100 hash <<= 2; 101 hash ^= param.hashCode(); 102 } 103 return hash; 104 } 105 106 /** 107 * {@inheritDoc} 108 */ 109 @Override 110 public String toString() { 111 return "BoundProcedure<" + procedure + "(" + param + ")>"; 112 } 113 114 /** 115 * Adapt the given, possibly-<code>null</code>, 116 * {@link UnaryProcedure UnaryProcedure} to the 117 * {@link Procedure Procedure} interface by binding 118 * the specified <code>Object</code> as a constant 119 * argument. 120 * When the given <code>UnaryProcedure</code> is <code>null</code>, 121 * returns <code>null</code>. 122 * 123 * @param <A> arg type 124 * @param procedure the possibly-<code>null</code> 125 * {@link UnaryProcedure UnaryProcedure} to adapt 126 * @param arg the object to bind as a constant argument 127 * @return a <code>BoundProcedure</code> wrapping the given 128 * {@link UnaryProcedure UnaryProcedure}, or <code>null</code> 129 * if the given <code>UnaryProcedure</code> is <code>null</code> 130 */ 131 public static <A> BoundProcedure bind(UnaryProcedure<? super A> procedure, A arg) { 132 return null == procedure ? null : new BoundProcedure(procedure, arg); 133 } 134 135 }