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.UnaryProcedure;
23  import org.apache.commons.lang3.Validate;
24  
25  /**
26   * Adapts a
27   * {@link BinaryProcedure BinaryProcedure}
28   * to the
29   * {@link UnaryProcedure UnaryProcedure} 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   * @param <A> the argument type.
40   * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $
41   */
42  public final class LeftBoundProcedure<A> implements UnaryProcedure<A>, Serializable {
43      /**
44       * serialVersionUID declaration.
45       */
46      private static final long serialVersionUID = 7650992440581629785L;
47      /** The {@link BinaryProcedure BinaryProcedure} I'm wrapping. */
48      private final BinaryProcedure<Object, ? super A> procedure;
49      /** The parameter to pass to {@code procedure}. */
50      private final Object param;
51  
52      /**
53       * Create a new LeftBoundProcedure.
54       * @param <L> the left argument type.
55       * @param procedure the procedure to adapt
56       * @param arg the constant argument to use
57       */
58      @SuppressWarnings("unchecked")
59      public <L> LeftBoundProcedure(BinaryProcedure<? super L, ? super A> procedure, L arg) {
60          this.procedure =
61              (BinaryProcedure<Object, ? super A>) Validate.notNull(procedure,
62                  "BinaryProcedure argument was null");
63          this.param = arg;
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      public void run(A obj) {
70          procedure.run(param, obj);
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public boolean equals(Object that) {
78          return that == this || (that instanceof LeftBoundProcedure<?> && equals((LeftBoundProcedure<?>) that));
79      }
80  
81      /**
82       * Learn whether another LeftBoundProcedure is equal to this.
83       * @param that LeftBoundProcedure to test
84       * @return boolean
85       */
86      public boolean equals(LeftBoundProcedure<?> that) {
87          return null != that
88                  && procedure.equals(that.procedure)
89                  && (null == param ? null == that.param : param.equals(that.param));
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public int hashCode() {
97          int hash = "LeftBoundProcedure".hashCode();
98          hash <<= 2;
99          hash ^= procedure.hashCode();
100         if (null != param) {
101             hash <<= 2;
102             hash ^= param.hashCode();
103         }
104         return hash;
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public String toString() {
112         return "LeftBoundProcedure<" + procedure + "(" + param + ",?)>";
113     }
114 
115     /**
116      * Get a UnaryProcedure from <code>procedure</code>.
117      * @param <L> left type
118      * @param <R> right type
119      * @param procedure to adapt
120      * @param arg left side argument
121      * @return LeftBoundProcedure<R>
122      */
123     public static <L, R> LeftBoundProcedure<R> bind(BinaryProcedure<? super L, ? super R> procedure, L arg) {
124         return null == procedure ? null : new LeftBoundProcedure<R>(procedure, arg);
125     }
126 
127 }