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.adapter;
18  
19  import org.apache.commons.functor.BinaryProcedure;
20  import org.apache.commons.functor.Procedure;
21  import org.apache.commons.lang3.Validate;
22  
23  /**
24   * Adapts a
25   * {@link BinaryProcedure BinaryProcedure}
26   * to the
27   * {@link Procedure Procedure} interface
28   * using a constant left-side argument.
29   *
30   * @param <A> the argument type.
31   * @version $Revision: 1537906 $ $Date: 2013-11-01 12:47:33 +0100 (Fr, 01 Nov 2013) $
32   */
33  public final class RightBoundProcedure<A> implements Procedure<A> {
34      /** The {@link BinaryProcedure BinaryProcedure} I'm wrapping. */
35      private final BinaryProcedure<? super A, Object> procedure;
36      /** The parameter to pass to {@code procedure}. */
37      private final Object param;
38  
39      /**
40       * Create a new RightBoundProcedure.
41       * @param <R> bound arg type
42       * @param procedure the procedure to adapt
43       * @param arg the constant argument to use
44       */
45      @SuppressWarnings("unchecked")
46      public <R> RightBoundProcedure(BinaryProcedure<? super A, ? super R> procedure, R arg) {
47          this.procedure =
48              (BinaryProcedure<? super A, Object>) Validate.notNull(procedure,
49                  "BinaryProcedure argument was null");
50          this.param = arg;
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      public void run(A obj) {
57          procedure.run(obj, param);
58      }
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public boolean equals(Object obj) {
65          if (obj == this) {
66              return true;
67          }
68          if (!(obj instanceof RightBoundProcedure<?>)) {
69              return false;
70          }
71          RightBoundProcedure<?> that = (RightBoundProcedure<?>) obj;
72          return this.procedure.equals(that.procedure)
73                  && (null == param ? null == that.param : param.equals(that.param));
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public int hashCode() {
81          int hash = "RightBoundProcedure".hashCode();
82          hash <<= 2;
83          hash ^= procedure.hashCode();
84          if (null != param) {
85              hash <<= 2;
86              hash ^= param.hashCode();
87          }
88          return hash;
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public String toString() {
96          return "RightBoundProcedure<" + procedure + "(?," + param + ")>";
97      }
98  
99      /**
100      * Get a Procedure from <code>procedure</code>.
101      * @param <L> the left argument type.
102      * @param <R> the right argument type.
103      * @param procedure to adapt
104      * @param arg right side argument
105      * @return RightBoundProcedure
106      */
107     public static <L, R> RightBoundProcedure<L> bind(BinaryProcedure<? super L, ? super R> procedure, R arg) {
108         return null == procedure ? null : new RightBoundProcedure<L>(procedure, arg);
109     }
110 
111 }