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.Function;
22  import org.apache.commons.functor.Procedure;
23  import org.apache.commons.lang3.Validate;
24  
25  /**
26   * Adapts a {@link Function Function}
27   * to the {@link Procedure Procedure}
28   * interface by ignoring the value returned
29   * by the function.
30   * <p/>
31   * Note that although this class implements
32   * {@link Serializable}, a given instance will
33   * only be truly <code>Serializable</code> if the
34   * underlying function is.  Attempts to serialize
35   * an instance whose delegate is not
36   * <code>Serializable</code> will result in an exception.
37   *
38   * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $
39   */
40  public final class FunctionProcedure implements Procedure, Serializable {
41      /**
42       * serialVersionUID declaration.
43       */
44      private static final long serialVersionUID = -7300031015086684901L;
45      /** The {@link Function Function} I'm wrapping. */
46      private final Function<?> function;
47  
48      /**
49       * Create an {@link Procedure Procedure} wrapping
50       * the given {@link Function Function}.
51       * @param function the {@link Function Function} to wrap
52       */
53      public FunctionProcedure(Function<?> function) {
54          this.function = Validate.notNull(function, "Function argument was null");
55      }
56  
57      /**
58       * {@inheritDoc}
59       * {@link Function#evaluate Evaluate} my function,
60       * but ignore its returned value.
61       */
62      public void run() {
63          function.evaluate();
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public boolean equals(Object that) {
71          return that == this || (that instanceof FunctionProcedure && equals((FunctionProcedure) that));
72      }
73  
74      /**
75       * Learn whether another FunctionProcedure is equal to this.
76       * @param that FunctionProcedure to test
77       * @return boolean
78       */
79      public boolean equals(FunctionProcedure that) {
80          return null != that && function.equals(that.function);
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public int hashCode() {
88          int hash = "FunctionProcedure".hashCode();
89          hash ^= function.hashCode();
90          return hash;
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public String toString() {
98          return "FunctionProcedure<" + function + ">";
99      }
100 
101     /**
102      * Adapt the given, possibly-<code>null</code>,
103      * {@link Function Function} to the
104      * {@link Procedure Procedure} interface.
105      * When the given <code>Function</code> is <code>null</code>,
106      * returns <code>null</code>.
107      *
108      * @param function the possibly-<code>null</code>
109      *        {@link Function Function} to adapt
110      * @return a {@link Procedure Procedure} wrapping the given
111      *         {@link Function Function}, or <code>null</code>
112      *         if the given <code>Function</code> is <code>null</code>
113      */
114     public static FunctionProcedure adapt(Function<?> function) {
115         return null == function ? null : new FunctionProcedure(function);
116     }
117 
118 }