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.Predicate;
23  import org.apache.commons.lang3.Validate;
24  
25  /**
26   * Adapts a
27   * {@link BinaryPredicate BinaryPredicate}
28   * to the
29   * {@link org.apache.commons.functor.UnaryPredicate UnaryPredicate} interface
30   * using a constant left-side 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 objects are.  Attempts to serialize
36   * an instance whose delegates are not
37   * <code>Serializable</code> will result in an exception.
38   *
39   * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $
40   */
41  public final class FullyBoundPredicate implements Predicate, Serializable {
42  
43      /**
44       * serialVersionUID declaration.
45       */
46      private static final long serialVersionUID = 7676235030688391839L;
47      /** The {@link BinaryPredicate BinaryPredicate} I'm wrapping. */
48      private final BinaryPredicate<Object, Object> predicate;
49      /** The left parameter to pass to {@code predicate}. */
50      private final Object left;
51      /** The right parameter to pass to {@code predicate}. */
52      private final Object right;
53  
54      /**
55       * Create a new FullyBoundPredicate instance.
56       * @param <L> left type
57       * @param <R> right type
58       * @param predicate the predicate to adapt
59       * @param left the left argument to use
60       * @param right the right argument to use
61       */
62      @SuppressWarnings("unchecked")
63      public <L, R> FullyBoundPredicate(BinaryPredicate<? super L, ? super R> predicate, L left, R right) {
64          this.predicate =
65              (BinaryPredicate<Object, Object>) Validate.notNull(predicate,
66                  "BinaryPredicate argument was null");
67          this.left = left;
68          this.right = right;
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      public boolean test() {
75          return predicate.test(left, right);
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public boolean equals(Object that) {
83          return that == this || (that instanceof FullyBoundPredicate && equals((FullyBoundPredicate) that));
84      }
85  
86      /**
87       * Learn whether another FullyBoundPredicate is equal to this.
88       * @param that FullyBoundPredicate to test
89       * @return boolean
90       */
91      public boolean equals(FullyBoundPredicate that) {
92          return null != that && predicate.equals(that.predicate)
93                  && (null == left ? null == that.left : left.equals(that.left))
94                  && (null == right ? null == that.right : right.equals(that.right));
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public int hashCode() {
102         int hash = "FullyBoundPredicate".hashCode();
103         hash <<= 2;
104         hash ^= predicate.hashCode();
105         hash <<= 2;
106         if (null != left) {
107             hash ^= left.hashCode();
108         }
109         hash <<= 2;
110         if (null != right) {
111             hash ^= right.hashCode();
112         }
113         return hash;
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public String toString() {
121         return "FullyBoundPredicate<" + predicate + "(" + left + ", " + right + ")>";
122     }
123 
124     /**
125      * Adapt a BinaryPredicate to the Predicate interface.
126      * @param predicate to adapt
127      * @param <L> left type
128      * @param <R> right type
129      * @param left L argument to always send as the left operand to the wrapped function
130      * @param right R argument to always send as the right operand to the wrapped function
131      * @return FullyBoundPredicate
132      */
133     public static <L, R> FullyBoundPredicate bind(
134             BinaryPredicate<? super L, ? super R> predicate, L left, R right) {
135         return null == predicate ? null : new FullyBoundPredicate(predicate, left, right);
136     }
137 }