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.Predicate;
022    import org.apache.commons.functor.UnaryPredicate;
023    
024    /**
025     * Adapts a
026     * {@link Predicate Predicate}
027     * to the
028     * {@link UnaryPredicate UnaryPredicate} interface
029     * by ignoring the given 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 functor is.  Attempts to serialize
035     * an instance whose delegate is not
036     * <code>Serializable</code> will result in an exception.
037     *
038     * @param <A> the argument type.
039     * @version $Revision: 1157628 $ $Date: 2011-08-14 22:04:52 +0200 (Sun, 14 Aug 2011) $
040     * @author Rodney Waldhoff
041     */
042    public final class PredicateUnaryPredicate<A> implements UnaryPredicate<A>, Serializable {
043        /**
044         * serialVersionUID declaration.
045         */
046        private static final long serialVersionUID = -5168896606842881702L;
047        /** The {@link Predicate Predicate} I'm wrapping. */
048        private final Predicate predicate;
049    
050        /**
051         * Create a new PredicateUnaryPredicate.
052         * @param predicate to adapt
053         */
054        public PredicateUnaryPredicate(Predicate predicate) {
055            if (predicate == null) {
056                throw new IllegalArgumentException("Predicate argument was null");
057            }
058            this.predicate = predicate;
059        }
060    
061        /**
062         * {@inheritDoc}
063         */
064        public boolean test(Object obj) {
065            return predicate.test();
066        }
067    
068        /**
069         * {@inheritDoc}
070         */
071        public boolean equals(Object that) {
072            return that == this || (that instanceof PredicateUnaryPredicate<?>
073                                        && equals((PredicateUnaryPredicate<?>) that));
074        }
075    
076        /**
077         * Learn whether a given PredicateUnaryPredicate is equal to this.
078         * @param that PredicateUnaryPredicate to test
079         * @return boolean
080         */
081        public boolean equals(PredicateUnaryPredicate<?> that) {
082            return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate));
083        }
084    
085        /**
086         * {@inheritDoc}
087         */
088        public int hashCode() {
089            int hash = "PredicateUnaryPredicate".hashCode();
090            if (null != predicate) {
091                hash ^= predicate.hashCode();
092            }
093            return hash;
094        }
095    
096        /**
097         * {@inheritDoc}
098         */
099        public String toString() {
100            return "PredicateUnaryPredicate<" + predicate + ">";
101        }
102    
103        /**
104         * Adapt a Predicate to the UnaryPredicate interface.
105         * @param <A> the argument type.
106         * @param predicate to adapt
107         * @return PredicateUnaryPredicate<A>
108         */
109        public static <A> PredicateUnaryPredicate<A> adapt(Predicate predicate) {
110            return null == predicate ? null : new PredicateUnaryPredicate<A>(predicate);
111        }
112    
113    }