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.Function; 020import org.apache.commons.functor.Procedure; 021import org.apache.commons.lang3.Validate; 022 023/** 024 * Adapts a {@link Function Function} 025 * to the {@link Procedure Procedure} 026 * interface by ignoring the value returned 027 * by the function. 028 * 029 * @param <A> the argument type. 030 * @version $Revision: 1537906 $ $Date: 2013-11-01 12:47:33 +0100 (Fr, 01 Nov 2013) $ 031 */ 032public final class FunctionProcedure<A> implements Procedure<A> { 033 034 /** The {@link Function Function} I'm wrapping. */ 035 private final Function<? super A, ?> function; 036 037 /** 038 * Create an {@link Procedure Procedure} wrapping 039 * the given {@link Function Function}. 040 * @param function the {@link Function Function} to wrap 041 */ 042 public FunctionProcedure(Function<? super A, ?> function) { 043 this.function = Validate.notNull(function, "Function argument was null"); 044 } 045 046 /** 047 * {@link Function#evaluate Evaluate} my function, but 048 * ignore its returned value. 049 * {@inheritDoc} 050 */ 051 public void run(A obj) { 052 function.evaluate(obj); 053 } 054 055 /** 056 * {@inheritDoc} 057 */ 058 @Override 059 public boolean equals(Object obj) { 060 if (obj == this) { 061 return true; 062 } 063 if (!(obj instanceof FunctionProcedure<?>)) { 064 return false; 065 } 066 FunctionProcedure<?> that = (FunctionProcedure<?>) obj; 067 return this.function.equals(that.function); 068 } 069 070 /** 071 * {@inheritDoc} 072 */ 073 @Override 074 public int hashCode() { 075 int hash = "FunctionProcedure".hashCode(); 076 hash ^= function.hashCode(); 077 return hash; 078 } 079 080 /** 081 * {@inheritDoc} 082 */ 083 @Override 084 public String toString() { 085 return "FunctionProcedure<" + function + ">"; 086 } 087 088 /** 089 * Adapt the given, possibly-<code>null</code>, 090 * {@link Function Function} to the 091 * {@link Procedure Procedure} interface. 092 * When the given <code>Function</code> is <code>null</code>, 093 * returns <code>null</code>. 094 * 095 * @param <A> the argument type. 096 * @param function the possibly-<code>null</code> 097 * {@link Function Function} to adapt 098 * @return a {@link Procedure Procedure} wrapping the given 099 * {@link Function Function}, or <code>null</code> 100 * if the given <code>Function</code> is <code>null</code> 101 */ 102 public static <A> FunctionProcedure<A> adapt(Function<? super A, ?> function) { 103 return null == function ? null : new FunctionProcedure<A>(function); 104 } 105 106}