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