001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.functor.adapter;
018    
019    import java.io.Serializable;
020    
021    import org.apache.commons.functor.BinaryPredicate;
022    import org.apache.commons.functor.Predicate;
023    
024    /**
025     * Adapts a
026     * {@link BinaryPredicate BinaryPredicate}
027     * to the
028     * {@link org.apache.commons.functor.UnaryPredicate UnaryPredicate} interface
029     * using a constant left-side argument.
030     * <p/>
031     * Note that although this class implements
032     * {@link Serializable}, a given instance will
033     * only be truly <code>Serializable</code> if the
034     * underlying objects are.  Attempts to serialize
035     * an instance whose delegates are not
036     * <code>Serializable</code> will result in an exception.
037     *
038     * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $
039     * @author Matt Benson
040     */
041    public final class FullyBoundPredicate implements Predicate, Serializable {
042    
043        /**
044         * serialVersionUID declaration.
045         */
046        private static final long serialVersionUID = 7676235030688391839L;
047        /** The {@link BinaryPredicate BinaryPredicate} I'm wrapping. */
048        private final BinaryPredicate<Object, Object> predicate;
049        /** The left parameter to pass to {@code predicate}. */
050        private final Object left;
051        /** The right parameter to pass to {@code predicate}. */
052        private final Object right;
053    
054        /**
055         * Create a new FullyBoundPredicate instance.
056         * @param <L> left type
057         * @param <R> right type
058         * @param predicate the predicate to adapt
059         * @param left the left argument to use
060         * @param right the right argument to use
061         */
062        @SuppressWarnings("unchecked")
063        public <L, R> FullyBoundPredicate(BinaryPredicate<? super L, ? super R> predicate, L left, R right) {
064            if (predicate == null) {
065                throw new IllegalArgumentException("BinaryPredicate argument was null");
066            }
067            this.predicate = (BinaryPredicate<Object, Object>) predicate;
068            this.left = left;
069            this.right = right;
070        }
071    
072        /**
073         * {@inheritDoc}
074         */
075        public boolean test() {
076            return predicate.test(left, right);
077        }
078    
079        /**
080         * {@inheritDoc}
081         */
082        public boolean equals(Object that) {
083            return that == this || (that instanceof FullyBoundPredicate && equals((FullyBoundPredicate) that));
084        }
085    
086        /**
087         * Learn whether another FullyBoundPredicate is equal to this.
088         * @param that FullyBoundPredicate to test
089         * @return boolean
090         */
091        public boolean equals(FullyBoundPredicate that) {
092            return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate))
093                    && (null == left ? null == that.left : left.equals(that.left))
094                    && (null == right ? null == that.right : right.equals(that.right));
095        }
096    
097        /**
098         * {@inheritDoc}
099         */
100        public int hashCode() {
101            int hash = "FullyBoundPredicate".hashCode();
102            if (null != predicate) {
103                hash <<= 2;
104                hash ^= predicate.hashCode();
105            }
106            hash <<= 2;
107            if (null != left) {
108                hash ^= left.hashCode();
109            }
110            hash <<= 2;
111            if (null != right) {
112                hash ^= right.hashCode();
113            }
114            return hash;
115        }
116    
117        /**
118         * {@inheritDoc}
119         */
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    }