View Javadoc

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.UnaryFunction;
22  import org.apache.commons.functor.UnaryProcedure;
23  
24  /**
25   * Adapts a {@link UnaryFunction UnaryFunction}
26   * to the {@link UnaryProcedure UnaryProcedure}
27   * interface by ignoring the value returned
28   * by the function.
29   * <p/>
30   * Note that although this class implements
31   * {@link Serializable}, a given instance will
32   * only be truly <code>Serializable</code> if the
33   * underlying function is.  Attempts to serialize
34   * an instance whose delegate is not
35   * <code>Serializable</code> will result in an exception.
36   *
37   * @param <A> the argument type.
38   * @version $Revision: 1157651 $ $Date: 2011-08-14 22:24:05 +0200 (Sun, 14 Aug 2011) $
39   * @author Rodney Waldhoff
40   */
41  public final class UnaryFunctionUnaryProcedure<A> implements UnaryProcedure<A>, Serializable {
42  
43      /**
44       * serialVersionUID declaration.
45       */
46      private static final long serialVersionUID = -3578673875995684811L;
47      /** The {@link UnaryFunction UnaryFunction} I'm wrapping. */
48      private final UnaryFunction<? super A, ?> function;
49  
50      /**
51       * Create an {@link UnaryProcedure UnaryProcedure} wrapping
52       * the given {@link UnaryFunction UnaryFunction}.
53       * @param function the {@link UnaryFunction UnaryFunction} to wrap
54       */
55      public UnaryFunctionUnaryProcedure(UnaryFunction<? super A, ?> function) {
56          if (function == null) {
57              throw new IllegalArgumentException("UnaryFunction argument was null");
58          }
59          this.function = function;
60      }
61  
62      /**
63       * {@link UnaryFunction#evaluate Evaluate} my function, but
64       * ignore its returned value.
65       * {@inheritDoc}
66       */
67      public void run(A obj) {
68          function.evaluate(obj);
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      public boolean equals(Object that) {
75          return that == this
76                  || (that instanceof UnaryFunctionUnaryProcedure<?> && equals((UnaryFunctionUnaryProcedure<?>) that));
77      }
78  
79      /**
80       * Learn whether a specified UnaryFunctionUnaryPredicate is equal to this.
81       * @param that the UnaryFunctionUnaryPredicate to test
82       * @return boolean
83       */
84      public boolean equals(UnaryFunctionUnaryProcedure<?> that) {
85          return null != that && (null == function ? null == that.function : function.equals(that.function));
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      public int hashCode() {
92          int hash = "UnaryFunctionUnaryProcedure".hashCode();
93          if (null != function) {
94              hash ^= function.hashCode();
95          }
96          return hash;
97      }
98  
99      /**
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 }