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.Predicate;
22  import org.apache.commons.functor.UnaryPredicate;
23  import org.apache.commons.lang3.Validate;
24  
25  /**
26   * Adapts a
27   * {@link Predicate Predicate}
28   * to the
29   * {@link UnaryPredicate UnaryPredicate} interface
30   * by ignoring the given 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 <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 PredicateUnaryPredicate<A> implements UnaryPredicate<A>, Serializable {
43      /**
44       * serialVersionUID declaration.
45       */
46      private static final long serialVersionUID = -5168896606842881702L;
47      /** The {@link Predicate Predicate} I'm wrapping. */
48      private final Predicate predicate;
49  
50      /**
51       * Create a new PredicateUnaryPredicate.
52       * @param predicate to adapt
53       */
54      public PredicateUnaryPredicate(Predicate predicate) {
55          this.predicate = Validate.notNull(predicate, "Predicate argument was null");
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      public boolean test(Object obj) {
62          return predicate.test();
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public boolean equals(Object that) {
70          return that == this || (that instanceof PredicateUnaryPredicate<?>
71                                      && equals((PredicateUnaryPredicate<?>) that));
72      }
73  
74      /**
75       * Learn whether a given PredicateUnaryPredicate is equal to this.
76       * @param that PredicateUnaryPredicate to test
77       * @return boolean
78       */
79      public boolean equals(PredicateUnaryPredicate<?> that) {
80          return null != that && predicate.equals(that.predicate);
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public int hashCode() {
88          int hash = "PredicateUnaryPredicate".hashCode();
89          hash ^= predicate.hashCode();
90          return hash;
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public String toString() {
98          return "PredicateUnaryPredicate<" + predicate + ">";
99      }
100 
101     /**
102      * Adapt a Predicate to the UnaryPredicate interface.
103      * @param <A> the argument type.
104      * @param predicate to adapt
105      * @return PredicateUnaryPredicate<A>
106      */
107     public static <A> PredicateUnaryPredicate<A> adapt(Predicate predicate) {
108         return null == predicate ? null : new PredicateUnaryPredicate<A>(predicate);
109     }
110 
111 }