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.Predicate;
22  import org.apache.commons.functor.UnaryPredicate;
23  
24  /**
25   * Adapts a
26   * {@link UnaryPredicate UnaryPredicate}
27   * to the
28   * {@link Predicate Predicate} interface
29   * using a constant unary 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 objects are.  Attempts to serialize
35   * an instance whose delegates are not
36   * <code>Serializable</code> will result in an exception.
37   *
38   * @version $Revision: 666186 $ $Date: 2008-06-10 11:34:42 -0500 (Tue, 10 Jun 2008) $
39   * @author Rodney Waldhoff
40   */
41  public final class BoundPredicate implements Predicate, Serializable {
42      /** The {@link UnaryPredicate UnaryPredicate} I'm wrapping. */
43      private UnaryPredicate<Object> predicate;
44      /** The parameter to pass to that predicate. */
45      private Object param;
46  
47      /**
48       * Create a new BoundPredicate.
49       * @param predicate the predicate to adapt
50       * @param arg the constant argument to use
51       */
52      @SuppressWarnings("unchecked")
53      public <A> BoundPredicate(UnaryPredicate<? super A> predicate, A arg) {
54          if (predicate == null) {
55              throw new IllegalArgumentException("UnaryPredicate argument was null");
56          }
57          this.predicate = (UnaryPredicate<Object>) predicate;
58          this.param = arg;
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      public boolean test() {
65          return predicate.test(param);
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      public boolean equals(Object that) {
72          return that == this || (that instanceof BoundPredicate && equals((BoundPredicate) that));
73      }
74  
75      /**
76       * Learn whether another BoundPredicate is equal to this.
77       * @param that BoundPredicate to test
78       * @return boolean
79       */
80      public boolean equals(BoundPredicate that) {
81          return null != that
82                  && (null == predicate ? null == that.predicate : predicate.equals(that.predicate))
83                  && (null == param ? null == that.param : param.equals(that.param));
84  
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      public int hashCode() {
91          int hash = "BoundPredicate".hashCode();
92          if (null != predicate) {
93              hash <<= 2;
94              hash ^= predicate.hashCode();
95          }
96          if (null != param) {
97              hash <<= 2;
98              hash ^= param.hashCode();
99          }
100         return hash;
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     public String toString() {
107         return "BoundPredicate<" + predicate + "(" + param + ")>";
108     }
109 
110     /**
111      * Adapt the given, possibly-<code>null</code>,
112      * {@link UnaryPredicate UnaryPredicate} to the
113      * {@link Predicate Predicate} interface by binding
114      * the specified <code>Object</code> as a constant
115      * argument.
116      * When the given <code>UnaryPredicate</code> is <code>null</code>,
117      * returns <code>null</code>.
118      *
119      * @param predicate the possibly-<code>null</code>
120      *        {@link UnaryPredicate UnaryPredicate} to adapt
121      * @param arg the object to bind as a constant argument
122      * @return a <code>BoundPredicate</code> wrapping the given
123      *         {@link UnaryPredicate UnaryPredicate}, or <code>null</code>
124      *         if the given <code>UnaryPredicate</code> is <code>null</code>
125      */
126     public static <A> BoundPredicate bind(UnaryPredicate<? super A> predicate, A arg) {
127         return null == predicate ? null : new BoundPredicate(predicate, arg);
128     }
129 
130 }