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 org.apache.commons.functor.BinaryFunction;
20  import org.apache.commons.functor.UnaryFunction;
21  
22  /**
23   * Adapts a BinaryFunction as a UnaryFunction by sending the same argument to both sides of the BinaryFunction.
24   * It sounds nonsensical, but using Composite functions, can be made to do something useful.
25   * @param <A> the argument type.
26   * @param <T> the returned value type.
27   * @version $Revision: 1156765 $ $Date: 2011-08-11 21:37:08 +0200 (Thu, 11 Aug 2011) $
28   * @author Rodney Waldhoff
29   */
30  public final class BinaryFunctionUnaryFunction<A, T> implements UnaryFunction<A, T> {
31      /**
32       * The adapted function.
33       */
34      private final BinaryFunction<? super A, ? super A, ? extends T> function;
35  
36      /**
37       * Create a new BinaryFunctionUnaryFunction.
38       * @param function to adapt
39       */
40      public BinaryFunctionUnaryFunction(BinaryFunction<? super A, ? super A, ? extends T> function) {
41          if (null == function) {
42              throw new IllegalArgumentException("BinaryFunction argument was null");
43          }
44          this.function = function;
45      }
46  
47      /**
48       * {@inheritDoc}
49       */
50      public T evaluate(A obj) {
51          return function.evaluate(obj, obj);
52      }
53  
54      /**
55       * {@inheritDoc}
56       */
57      @Override
58      public boolean equals(Object obj) {
59          return obj == this || obj instanceof BinaryFunctionUnaryFunction<?, ?>
60                  && equals((BinaryFunctionUnaryFunction<?, ?>) obj);
61      }
62  
63      /**
64       * Learn whether another BinaryFunctionUnaryFunction is equal to <code>this</code>.
65       * @param that BinaryFunctionUnaryFunction to check
66       * @return whether equal
67       */
68      public boolean equals(BinaryFunctionUnaryFunction<?, ?> that) {
69          return that != null && that.function.equals(this.function);
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public int hashCode() {
77          return ("BinaryFunctionUnaryFunction".hashCode() << 2) | function.hashCode();
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public String toString() {
85          return "BinaryFunctionUnaryFunction<" + function + ">";
86      }
87  
88      /**
89       * Adapt a BinaryFunction as a UnaryFunction.
90       * @param <A> input type
91       * @param <T> result type
92       * @param function to adapt
93       * @return UnaryFunction<A, T>
94       */
95      public static <A, T> UnaryFunction<A, T> adapt(BinaryFunction<? super A, ? super A, ? extends T> function) {
96          return null == function ? null : new BinaryFunctionUnaryFunction<A, T>(function);
97      }
98  
99  }