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