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.BinaryFunction;
22  import org.apache.commons.functor.Function;
23  import org.apache.commons.lang3.Validate;
24  
25  /**
26   * Adapts a
27   * {@link BinaryFunction BinaryFunction}
28   * to the
29   * {@link Function Function} interface
30   * using constant arguments.
31   * <p/>
32   * Note that although this class implements
33   * {@link Serializable}, a given instance will
34   * only be truly <code>Serializable</code> if the
35   * underlying objects are.  Attempts to serialize
36   * an instance whose delegates are not
37   * <code>Serializable</code> will result in an exception.
38   *
39   * @param <T> the returned value type.
40   * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $
41   */
42  public final class FullyBoundFunction<T> implements Function<T>, Serializable {
43      /**
44       * serialVersionUID declaration.
45       */
46      private static final long serialVersionUID = -8588331452137525985L;
47      /** The {@link BinaryFunction BinaryFunction} I'm wrapping. */
48      private final BinaryFunction<Object, Object, ? extends T> function;
49      /** The left parameter to pass to {@code function}. */
50      private final Object left;
51      /** The right parameter to pass to {@code function}. */
52      private final Object right;
53  
54      /**
55       * Create a new FullyBoundFunction.
56       * @param <L> the left argument type.
57       * @param <R> the right argument type.
58       * @param function the function to adapt
59       * @param left the left side argument to use
60       * @param right the right side argument to use
61       */
62      @SuppressWarnings("unchecked")
63      public <L, R> FullyBoundFunction(BinaryFunction<? super L, ? super R, ? extends T> function, L left, R right) {
64          this.function =
65              (BinaryFunction<Object, Object, T>) Validate.notNull(function,
66                  "BinaryFunction argument was null");
67          this.left = left;
68          this.right = right;
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      public T evaluate() {
75          return function.evaluate(left, right);
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public boolean equals(Object that) {
83          return that == this || (that instanceof FullyBoundFunction<?> && equals((FullyBoundFunction<?>) that));
84      }
85  
86      /**
87       * Learn whether another FullyBoundFunction is equal to this.
88       * @param that FullyBoundFunction to test
89       * @return boolean
90       */
91      public boolean equals(FullyBoundFunction<?> that) {
92          return null != that && function.equals(that.function)
93                  && (null == left ? null == that.left : left.equals(that.left))
94                  && (null == right ? null == that.right : right.equals(that.right));
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public int hashCode() {
102         int hash = "FullyBoundFunction".hashCode();
103         hash <<= 2;
104         hash ^= function.hashCode();
105         hash <<= 2;
106         if (null != left) {
107             hash ^= left.hashCode();
108         }
109         hash <<= 2;
110         if (null != right) {
111             hash ^= right.hashCode();
112         }
113         return hash;
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public String toString() {
121         return "FullyBoundFunction<" + function + "(" + left + ", " + right + ")>";
122     }
123 
124     /**
125      * Adapt a BinaryFunction as a Function.
126      * @param <L> left type
127      * @param <R> right type
128      * @param <T> result type
129      * @param function to adapt
130      * @param left left side argument
131      * @param right right side argument
132      * @return FullyBoundFunction
133      */
134     public static <L, R, T> FullyBoundFunction<T> bind(
135             BinaryFunction<? super L, ? super R, ? extends T> function, L left, R right) {
136         return null == function ? null : new FullyBoundFunction<T>(function, left, right);
137     }
138 
139 }