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