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