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