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 org.apache.commons.functor.NullaryProcedure; 020import org.apache.commons.functor.Procedure; 021import org.apache.commons.lang3.Validate; 022 023/** 024 * Adapts a 025 * {@link Procedure Procedure} 026 * to the 027 * {@link NullaryProcedure NullaryProcedure} interface 028 * using a constant unary argument. 029 * 030 * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $ 031 */ 032public final class BoundNullaryProcedure implements NullaryProcedure { 033 /** The {@link Procedure Procedure} I'm wrapping. */ 034 private final Procedure<Object> procedure; 035 /** The parameter to pass to {@code procedure}. */ 036 private final Object param; 037 038 /** 039 * Create a new BoundNullaryProcedure instance. 040 * @param <A> arg type 041 * @param procedure the procedure to adapt 042 * @param arg the constant argument to use 043 */ 044 @SuppressWarnings("unchecked") 045 public <A> BoundNullaryProcedure(Procedure<? super A> procedure, A arg) { 046 this.procedure = 047 (Procedure<Object>) Validate.notNull(procedure, 048 "Procedure argument was null"); 049 this.param = arg; 050 } 051 052 /** 053 * {@inheritDoc} 054 */ 055 public void run() { 056 procedure.run(param); 057 } 058 059 /** 060 * {@inheritDoc} 061 */ 062 @Override 063 public boolean equals(Object obj) { 064 if (obj == this) { 065 return true; 066 } 067 if (!(obj instanceof BoundNullaryProcedure)) { 068 return false; 069 } 070 BoundNullaryProcedure that = (BoundNullaryProcedure) obj; 071 return procedure.equals(that.procedure) 072 && (null == param ? null == that.param : param.equals(that.param)); 073 } 074 075 /** 076 * {@inheritDoc} 077 */ 078 @Override 079 public int hashCode() { 080 int hash = "BoundNullaryProcedure".hashCode(); 081 hash <<= 2; 082 hash ^= procedure.hashCode(); 083 if (null != param) { 084 hash <<= 2; 085 hash ^= param.hashCode(); 086 } 087 return hash; 088 } 089 090 /** 091 * {@inheritDoc} 092 */ 093 @Override 094 public String toString() { 095 return "BoundNullaryProcedure<" + procedure + "(" + param + ")>"; 096 } 097 098 /** 099 * Adapt the given, possibly-<code>null</code>, 100 * {@link Procedure Procedure} to the 101 * {@link NullaryProcedure NullaryProcedure} interface by binding 102 * the specified <code>Object</code> as a constant 103 * argument. 104 * When the given <code>Procedure</code> is <code>null</code>, 105 * returns <code>null</code>. 106 * 107 * @param <A> arg type 108 * @param procedure the possibly-<code>null</code> 109 * {@link Procedure Procedure} to adapt 110 * @param arg the object to bind as a constant argument 111 * @return a <code>BoundNullaryProcedure</code> wrapping the given 112 * {@link Procedure Procedure}, or <code>null</code> 113 * if the given <code>Procedure</code> is <code>null</code> 114 */ 115 public static <A> BoundNullaryProcedure bind(Procedure<? super A> procedure, A arg) { 116 return null == procedure ? null : new BoundNullaryProcedure(procedure, arg); 117 } 118 119}