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.UnaryFunction; 022 import org.apache.commons.functor.UnaryProcedure; 023 024 /** 025 * Adapts a {@link UnaryFunction UnaryFunction} 026 * to the {@link UnaryProcedure UnaryProcedure} 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 * @param <A> the argument type. 038 * @version $Revision: 1157651 $ $Date: 2011-08-14 22:24:05 +0200 (Sun, 14 Aug 2011) $ 039 * @author Rodney Waldhoff 040 */ 041 public final class UnaryFunctionUnaryProcedure<A> implements UnaryProcedure<A>, Serializable { 042 043 /** 044 * serialVersionUID declaration. 045 */ 046 private static final long serialVersionUID = -3578673875995684811L; 047 /** The {@link UnaryFunction UnaryFunction} I'm wrapping. */ 048 private final UnaryFunction<? super A, ?> function; 049 050 /** 051 * Create an {@link UnaryProcedure UnaryProcedure} wrapping 052 * the given {@link UnaryFunction UnaryFunction}. 053 * @param function the {@link UnaryFunction UnaryFunction} to wrap 054 */ 055 public UnaryFunctionUnaryProcedure(UnaryFunction<? super A, ?> function) { 056 if (function == null) { 057 throw new IllegalArgumentException("UnaryFunction argument was null"); 058 } 059 this.function = function; 060 } 061 062 /** 063 * {@link UnaryFunction#evaluate Evaluate} my function, but 064 * ignore its returned value. 065 * {@inheritDoc} 066 */ 067 public void run(A obj) { 068 function.evaluate(obj); 069 } 070 071 /** 072 * {@inheritDoc} 073 */ 074 public boolean equals(Object that) { 075 return that == this 076 || (that instanceof UnaryFunctionUnaryProcedure<?> && equals((UnaryFunctionUnaryProcedure<?>) that)); 077 } 078 079 /** 080 * Learn whether a specified UnaryFunctionUnaryPredicate is equal to this. 081 * @param that the UnaryFunctionUnaryPredicate to test 082 * @return boolean 083 */ 084 public boolean equals(UnaryFunctionUnaryProcedure<?> that) { 085 return null != that && (null == function ? null == that.function : function.equals(that.function)); 086 } 087 088 /** 089 * {@inheritDoc} 090 */ 091 public int hashCode() { 092 int hash = "UnaryFunctionUnaryProcedure".hashCode(); 093 if (null != function) { 094 hash ^= function.hashCode(); 095 } 096 return hash; 097 } 098 099 /** 100 * {@inheritDoc} 101 */ 102 public String toString() { 103 return "UnaryFunctionUnaryProcedure<" + function + ">"; 104 } 105 106 /** 107 * Adapt the given, possibly-<code>null</code>, 108 * {@link UnaryFunction UnaryFunction} to the 109 * {@link UnaryProcedure UnaryProcedure} interface. 110 * When the given <code>UnaryFunction</code> is <code>null</code>, 111 * returns <code>null</code>. 112 * 113 * @param <A> the argument type. 114 * @param function the possibly-<code>null</code> 115 * {@link UnaryFunction UnaryFunction} to adapt 116 * @return a {@link UnaryProcedure UnaryProcedure} wrapping the given 117 * {@link UnaryFunction UnaryFunction}, or <code>null</code> 118 * if the given <code>UnaryFunction</code> is <code>null</code> 119 */ 120 public static <A> UnaryFunctionUnaryProcedure<A> adapt(UnaryFunction<? super A, ?> function) { 121 return null == function ? null : new UnaryFunctionUnaryProcedure<A>(function); 122 } 123 124 }