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 */ 017 package org.apache.commons.functor.adapter; 018 019 import java.io.Serializable; 020 021 import org.apache.commons.functor.Function; 022 import org.apache.commons.functor.Procedure; 023 024 /** 025 * Adapts a {@link Function Function} 026 * to the {@link Procedure Procedure} 027 * interface by ignoring the value returned 028 * by the function. 029 * <p/> 030 * Note that although this class implements 031 * {@link Serializable}, a given instance will 032 * only be truly <code>Serializable</code> if the 033 * underlying function is. Attempts to serialize 034 * an instance whose delegate is not 035 * <code>Serializable</code> will result in an exception. 036 * 037 * @version $Revision: 1156778 $ $Date: 2011-08-11 21:50:51 +0200 (Thu, 11 Aug 2011) $ 038 * @author Rodney Waldhoff 039 */ 040 public final class FunctionProcedure implements Procedure, Serializable { 041 /** 042 * serialVersionUID declaration. 043 */ 044 private static final long serialVersionUID = -7300031015086684901L; 045 /** The {@link Function Function} I'm wrapping. */ 046 private final Function<?> function; 047 048 /** 049 * Create an {@link Procedure Procedure} wrapping 050 * the given {@link Function Function}. 051 * @param function the {@link Function Function} to wrap 052 */ 053 public FunctionProcedure(Function<?> function) { 054 if (function == null) { 055 throw new IllegalArgumentException("Function argument was null"); 056 } 057 this.function = function; 058 } 059 060 /** 061 * {@inheritDoc} 062 * {@link Function#evaluate Evaluate} my function, 063 * but ignore its returned value. 064 */ 065 public void run() { 066 function.evaluate(); 067 } 068 069 /** 070 * {@inheritDoc} 071 */ 072 public boolean equals(Object that) { 073 return that == this || (that instanceof FunctionProcedure && equals((FunctionProcedure) that)); 074 } 075 076 /** 077 * Learn whether another FunctionProcedure is equal to this. 078 * @param that FunctionProcedure to test 079 * @return boolean 080 */ 081 public boolean equals(FunctionProcedure that) { 082 return null != that && (null == function ? null == that.function : function.equals(that.function)); 083 } 084 085 /** 086 * {@inheritDoc} 087 */ 088 public int hashCode() { 089 int hash = "FunctionProcedure".hashCode(); 090 if (null != function) { 091 hash ^= function.hashCode(); 092 } 093 return hash; 094 } 095 096 /** 097 * {@inheritDoc} 098 */ 099 public String toString() { 100 return "FunctionProcedure<" + function + ">"; 101 } 102 103 /** 104 * Adapt the given, possibly-<code>null</code>, 105 * {@link Function Function} to the 106 * {@link Procedure Procedure} interface. 107 * When the given <code>Function</code> is <code>null</code>, 108 * returns <code>null</code>. 109 * 110 * @param function the possibly-<code>null</code> 111 * {@link Function Function} to adapt 112 * @return a {@link Procedure Procedure} wrapping the given 113 * {@link Function Function}, or <code>null</code> 114 * if the given <code>Function</code> is <code>null</code> 115 */ 116 public static FunctionProcedure adapt(Function<?> function) { 117 return null == function ? null : new FunctionProcedure(function); 118 } 119 120 }