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.BinaryProcedure;
23  import org.apache.commons.lang3.Validate;
24  
25  /**
26   * Adapts a {@link BinaryFunction BinaryFunction}
27   * to the {@link BinaryProcedure BinaryProcedure}
28   * interface by ignoring the value returned
29   * by the function.
30   * <p/>
31   * Note that although this class implements
32   * {@link Serializable}, a given instance will
33   * only be truly <code>Serializable</code> if the
34   * underlying function is.  Attempts to serialize
35   * an instance whose delegate is not
36   * <code>Serializable</code> will result in an exception.
37   *
38   * @param <L> the left argument type.
39   * @param <R> the right argument type.
40   * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $
41   */
42  public final class BinaryFunctionBinaryProcedure<L, R> implements BinaryProcedure<L, R>, Serializable {
43  
44      /**
45       * serialVersionUID declaration.
46       */
47      private static final long serialVersionUID = 4498276997127058865L;
48      /** The {@link BinaryFunction BinaryFunction} I'm wrapping. */
49      private final BinaryFunction<? super L, ? super R, ?> function;
50  
51      /**
52       * Create an {@link BinaryProcedure BinaryProcedure} wrapping
53       * the given {@link BinaryFunction BinaryFunction}.
54       * @param function the {@link BinaryFunction BinaryFunction} to wrap
55       */
56      public BinaryFunctionBinaryProcedure(BinaryFunction<? super L, ? super R, ?> function) {
57          this.function = Validate.notNull(function, "BinaryFunction argument was null");
58      }
59  
60      /**
61       * {@link BinaryFunction#evaluate Evaluate} my function, but
62       * ignore its returned value.
63       * {@inheritDoc}
64       */
65      public void run(L left, R right) {
66          function.evaluate(left, right);
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public boolean equals(Object that) {
74          return that == this
75                  || (that instanceof BinaryFunctionBinaryProcedure<?, ?>
76                  && equals((BinaryFunctionBinaryProcedure<?, ?>) that));
77      }
78  
79      /**
80       * Learn whether a given BinaryFunctionBinaryPredicate is equal to this.
81       * @param that BinaryFunctionBinaryPredicate to compare
82       * @return boolean
83       */
84      public boolean equals(BinaryFunctionBinaryProcedure<?, ?> that) {
85          return null != that && function.equals(that.function);
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public int hashCode() {
93          int hash = "BinaryFunctionBinaryProcedure".hashCode();
94          hash ^= function.hashCode();
95          return hash;
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public String toString() {
103         return "BinaryFunctionBinaryProcedure<" + function + ">";
104     }
105 
106     /**
107      * Adapt the given, possibly-<code>null</code>,
108      * {@link BinaryFunction BinaryFunction} to the
109      * {@link BinaryProcedure BinaryProcedure} interface.
110      * When the given <code>BinaryFunction</code> is <code>null</code>,
111      * returns <code>null</code>.
112      *
113      * @param <L> left type
114      * @param <R> right type
115      * @param function the possibly-<code>null</code>
116      *        {@link BinaryFunction BinaryFunction} to adapt
117      * @return a <code>BinaryFunctionBinaryProcedure</code> wrapping the given
118      *         {@link BinaryFunction BinaryFunction}, or <code>null</code>
119      *         if the given <code>BinaryFunction</code> is <code>null</code>
120      */
121     public static <L, R> BinaryFunctionBinaryProcedure<L, R> adapt(BinaryFunction<? super L, ? super R, ?> function) {
122         return null == function ? null : new BinaryFunctionBinaryProcedure<L, R>(function);
123     }
124 
125 }