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.core.composite;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.functor.BinaryFunction;
22  import org.apache.commons.lang3.Validate;
23  
24  /**
25   * Transposes (swaps) the arguments to some other
26   * {@link BinaryFunction function}.
27   * For example, given a function <i>f</i>
28   * and the ordered pair of arguments <i>a</i>,
29   * <i>b</i>.
30   * {@link #evaluate evaluates} to
31   * <code>f.evaluate(b,a)</code>.
32   * <p>
33   * Note that although this class implements
34   * {@link Serializable}, a given instance will
35   * only be truly <code>Serializable</code> if the
36   * underlying functor is.  Attempts to serialize
37   * an instance whose delegate is not
38   * <code>Serializable</code> will result in an exception.
39   * </p>
40   *
41   * @param <L> the left argument type.
42   * @param <R> the right argument type.
43   * @param <T> the returned value type.
44   * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
45   */
46  public class TransposedFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable {
47      /**
48       * serialVersionUID declaration.
49       */
50      private static final long serialVersionUID = -5824252875453493940L;
51      // attributes
52      // ------------------------------------------------------------------------
53      /**
54       * The adapted function.
55       */
56      private final BinaryFunction<? super R, ? super L, ? extends T> function;
57  
58      // constructor
59      // ------------------------------------------------------------------------
60      /**
61       * Create a new TransposedFunction.
62       * @param function BinaryFunction to transpose.
63       */
64      public TransposedFunction(BinaryFunction<? super R, ? super L, ? extends T> function) {
65          this.function = Validate.notNull(function, "BinaryFunction argument was null");
66      }
67  
68      // functor interface
69      // ------------------------------------------------------------------------
70      /**
71       * {@inheritDoc}
72       */
73      public final T evaluate(L left, R right) {
74          return function.evaluate(right, left);
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public final boolean equals(Object that) {
82          return that == this || (that instanceof TransposedFunction<?, ?, ?>
83                                      && equals((TransposedFunction<?, ?, ?>) that));
84      }
85  
86      /**
87       * Learn whether another TransposedFunction is equal to this.
88       * @param that TransposedFunction to test
89       * @return boolean
90       */
91      public final boolean equals(TransposedFunction<?, ?, ?> that) {
92          return null != that && function.equals(that.function);
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public int hashCode() {
100         int hash = "TransposedFunction".hashCode();
101         hash ^= function.hashCode();
102         return hash;
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public String toString() {
110         return "TransposedFunction<" + function + ">";
111     }
112 
113     // static
114     // ------------------------------------------------------------------------
115     /**
116      * Transpose a BinaryFunction.
117      * @param <L> the left argument type.
118      * @param <R> the right argument type.
119      * @param <T> the returned value type.
120      * @param f BinaryFunction to transpose
121      * @return TransposedFunction
122      */
123     public static <L, R, T> TransposedFunction<R, L, T> transpose(BinaryFunction<? super L, ? super R, ? extends T> f) {
124         return null == f ? null : new TransposedFunction<R, L, T>(f);
125     }
126 
127 }