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.Function;
22  import org.apache.commons.functor.UnaryFunction;
23  
24  /**
25   * Adapts a
26   * {@link Function Function}
27   * to the
28   * {@link UnaryFunction UnaryFunction} interface
29   * by ignoring the unary argument.
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 functor is.  Attempts to serialize
35   * an instance whose delegate is not
36   * <code>Serializable</code> will result in an exception.
37   *
38   * @param <A> the argument type.
39   * @param <T> the returned value type.
40   * @version $Revision: 1157609 $ $Date: 2011-08-14 21:42:21 +0200 (Sun, 14 Aug 2011) $
41   * @author Rodney Waldhoff
42   */
43  public final class FunctionUnaryFunction<A, T> implements UnaryFunction<A, T>, Serializable {
44      /**
45       * serialVersionUID declaration.
46       */
47      private static final long serialVersionUID = 1993899041200996524L;
48      /** The {@link Function Function} I'm wrapping. */
49      private final Function<? extends T> function;
50  
51      /**
52       * Create a new FunctionUnaryFunction.
53       * @param function to adapt
54       */
55      public FunctionUnaryFunction(Function<? extends T> function) {
56          if (function == null) {
57              throw new IllegalArgumentException("Function argument was null");
58          }
59          this.function = function;
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      public T evaluate(A obj) {
66          return function.evaluate();
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      public boolean equals(Object that) {
73          return that == this || (that instanceof FunctionUnaryFunction<?, ?>
74                  && equals((FunctionUnaryFunction<?, ?>) that));
75      }
76  
77      /**
78       * Learn whether another FunctionUnaryFunction is equal to this.
79       * @param that FunctionUnaryFunction to test
80       * @return boolean
81       */
82      public boolean equals(FunctionUnaryFunction<?, ?> that) {
83          return null != that && (null == function ? null == that.function : function.equals(that.function));
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      public int hashCode() {
90          int hash = "FunctionUnaryFunction".hashCode();
91          if (null != function) {
92              hash ^= function.hashCode();
93          }
94          return hash;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     public String toString() {
101         return "FunctionUnaryFunction<" + function + ">";
102     }
103 
104     /**
105      * Adapt a Function to the UnaryFunction interface.
106      * @param <A> arg type
107      * @param <T> result type
108      * @param function to adapt
109      * @return FunctionUnaryFunction
110      */
111     public static <A, T> FunctionUnaryFunction<A, T> adapt(Function<? extends T> function) {
112         return null == function ? null : new FunctionUnaryFunction<A, T>(function);
113     }
114 
115 }