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.BinaryPredicate;
22  import org.apache.commons.functor.UnaryPredicate;
23  import org.apache.commons.lang3.Validate;
24  
25  /**
26   * Adapts a
27   * {@link UnaryPredicate UnaryPredicate}
28   * to the
29   * {@link BinaryPredicate BinaryPredicate} interface
30   * by ignoring the second binary 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 functor is.  Attempts to serialize
36   * an instance whose delegate is not
37   * <code>Serializable</code> will result in an exception.
38   *
39   * @param <L> the left argument type.
40   * @param <R> the right argument type.
41   * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $
42   */
43  public final class IgnoreRightPredicate<L, R> implements BinaryPredicate<L, R>, Serializable {
44      /**
45       * serialVersionUID declaration.
46       */
47      private static final long serialVersionUID = -4236624667788627722L;
48      /** The {@link UnaryPredicate UnaryPredicate} I'm wrapping. */
49      private final UnaryPredicate<? super L> predicate;
50  
51      /**
52       * Create a new IgnoreRightPredicate.
53       * @param predicate left
54       */
55      public IgnoreRightPredicate(UnaryPredicate<? super L> predicate) {
56          this.predicate = Validate.notNull(predicate, "UnaryPredicate argument was null");
57      }
58  
59      /**
60       * {@inheritDoc}
61       */
62      public boolean test(L left, R right) {
63          return predicate.test(left);
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public boolean equals(Object that) {
71          return that == this || (that instanceof IgnoreRightPredicate<?, ?>
72                                      && equals((IgnoreRightPredicate<?, ?>) that));
73      }
74  
75      /**
76       * Learn whether a given IgnoreRightPredicate is equal to this.
77       * @param that IgnoreRightPredicate to test
78       * @return boolean
79       */
80      public boolean equals(IgnoreRightPredicate<?, ?> that) {
81          return null != that &&  predicate.equals(that.predicate);
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public int hashCode() {
89          int hash = "IgnoreRightPredicate".hashCode();
90          hash ^= predicate.hashCode();
91          return hash;
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public String toString() {
99          return "IgnoreRightPredicate<" + predicate + ">";
100     }
101 
102     /**
103      * Adapt a UnaryPredicate as an IgnoreRightPredicate.
104      * @param <L> left type
105      * @param <R> right type
106      * @param predicate to adapt
107      * @return IgnoreRightPredicate<L, R>
108      */
109     public static <L, R> IgnoreRightPredicate<L, R> adapt(UnaryPredicate<? super L> predicate) {
110         return null == predicate ? null : new IgnoreRightPredicate<L, R>(predicate);
111     }
112 
113 }