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.core.composite;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.functor.BinaryFunction;
22  import org.apache.commons.functor.UnaryFunction;
23  
24  /**
25   * A BinaryFunction whose result is then run through a UnaryFunction.
26   * @version $Revision: 1171154 $ $Date: 2011-09-15 17:58:38 +0200 (Thu, 15 Sep 2011) $
27   * @author Matt Benson
28   */
29  public class TransformedBinaryFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable {
30      /**
31       * serialVersionUID declaration.
32       */
33      private static final long serialVersionUID = 3312781645741807814L;
34  
35      /**
36       * Type-remembering helper
37       * @param <X>
38       */
39      private static final class Helper<X, L, R, T> implements BinaryFunction<L, R, T>, Serializable {
40          /**
41           * serialVersionUID declaration.
42           */
43          private static final long serialVersionUID = 8141488776884860650L;
44          private BinaryFunction<? super L, ? super R, ? extends X> preceding;
45          private UnaryFunction<? super X, ? extends T> following;
46  
47          /**
48           * Create a new Helper.
49           * @param preceding BinaryFunction
50           * @param following UnaryFunction
51           */
52          private Helper(BinaryFunction<? super L, ? super R, ? extends X> preceding,
53                  UnaryFunction<? super X, ? extends T> following) {
54              this.preceding = preceding;
55              this.following = following;
56          }
57  
58          /**
59           * {@inheritDoc}
60           */
61          public T evaluate(L left, R right) {
62              return following.evaluate(preceding.evaluate(left, right));
63          }
64      }
65  
66      private final Helper<?, L, R, T> helper;
67  
68      /**
69       * Create a new TransformedBinaryFunction.
70       * @param <X>
71       * @param preceding BinaryFunction
72       * @param following UnaryFunction
73       */
74      public <X> TransformedBinaryFunction(BinaryFunction<? super L, ? super R, ? extends X> preceding,
75              UnaryFunction<? super X, ? extends T> following) {
76          this.helper = new Helper<X, L, R, T>(preceding, following);
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      public final T evaluate(L left, R right) {
83          return helper.evaluate(left, right);
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public final boolean equals(Object obj) {
91          return obj == this || obj instanceof TransformedBinaryFunction<?, ?, ?>
92                  && equals((TransformedBinaryFunction<?, ?, ?>) obj);
93      }
94  
95      /**
96       * Learn whether another TransformedBinaryFunction is equal to <code>this</code>.
97       * @param that instance to test
98       * @return whether equal
99       */
100     public final boolean equals(TransformedBinaryFunction<?, ?, ?> that) {
101         return that != null && that.helper.preceding.equals(this.helper.preceding)
102                 && that.helper.following.equals(this.helper.following);
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public int hashCode() {
110         int result = "TransformedBinaryFunction".hashCode();
111         result <<= 2;
112         result |= helper.following.hashCode();
113         result <<= 2;
114         result |= helper.preceding.hashCode();
115         return result;
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public String toString() {
123         return "TransformedBinaryFunction<" + helper.preceding + "; " + helper.following + ">";
124     }
125 }