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 Procedure Procedure}
26   * to the
27   * {@link BinaryProcedure BinaryProcedure} interface
28   * by ignoring the second binary argument.
29   *
30   * @param <L> the left argument type.
31   * @param <R> the right argument type.
32   * @version $Revision: 1537906 $ $Date: 2013-11-01 12:47:33 +0100 (Fr, 01 Nov 2013) $
33   */
34  public final class IgnoreRightProcedure<L, R> implements BinaryProcedure<L, R> {
35      /** The {@link Procedure Procedure} I'm wrapping. */
36      private final Procedure<? super L> procedure;
37  
38      /**
39       * Create a new IgnoreRightProcedure.
40       * @param procedure Procedure to adapt
41       */
42      public IgnoreRightProcedure(Procedure<? super L> procedure) {
43          this.procedure = Validate.notNull(procedure, "Procedure argument was null");
44      }
45  
46      /**
47       * {@inheritDoc}
48       */
49      public void run(L left, R right) {
50          procedure.run(left);
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      public boolean equals(Object obj) {
58          if (obj == this) {
59              return true;
60          }
61          if (!(obj instanceof IgnoreRightProcedure<?, ?>)) {
62              return false;
63          }
64          IgnoreRightProcedure<?, ?> that = (IgnoreRightProcedure<?, ?>) obj;
65          return this.procedure.equals(that.procedure);
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public int hashCode() {
73          int hash = "IgnoreRightProcedure".hashCode();
74          hash ^= procedure.hashCode();
75          return hash;
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public String toString() {
83          return "IgnoreRightProcedure<" + procedure + ">";
84      }
85  
86      /**
87       * Adapt a Procedure to the BinaryProcedure interface.
88       * @param <L> left type
89       * @param <R> right type
90       * @param procedure Procedure to adapt
91       * @return IgnoreRightProcedure
92       */
93      public static <L, R> IgnoreRightProcedure<L, R> adapt(Procedure<? super L> procedure) {
94          return null == procedure ? null : new IgnoreRightProcedure<L, R>(procedure);
95      }
96  
97  }