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 */
017package org.apache.commons.functor.adapter;
018
019import org.apache.commons.functor.NullaryPredicate;
020import org.apache.commons.functor.Predicate;
021import org.apache.commons.lang3.Validate;
022
023/**
024 * Adapts a
025 * {@link NullaryPredicate NullaryPredicate}
026 * to the
027 * {@link Predicate Predicate} interface
028 * by ignoring the given argument.
029 *
030 * @param <A> the argument type.
031 * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $
032 */
033public final class NullaryPredicatePredicate<A> implements Predicate<A> {
034    /** The {@link NullaryPredicate NullaryPredicate} I'm wrapping. */
035    private final NullaryPredicate predicate;
036
037    /**
038     * Create a new NullaryPredicatePredicate.
039     * @param predicate to adapt
040     */
041    public NullaryPredicatePredicate(NullaryPredicate predicate) {
042        this.predicate = Validate.notNull(predicate, "NullaryPredicate argument was null");
043    }
044
045    /**
046     * {@inheritDoc}
047     */
048    public boolean test(Object obj) {
049        return predicate.test();
050    }
051
052    /**
053     * {@inheritDoc}
054     */
055    @Override
056    public boolean equals(Object obj) {
057        if (obj == this) {
058            return true;
059        }
060        if (!(obj instanceof NullaryPredicatePredicate<?>)) {
061            return false;
062        }
063        NullaryPredicatePredicate<?> that = (NullaryPredicatePredicate<?>) obj;
064        return this.predicate.equals(that.predicate);
065    }
066
067    /**
068     * {@inheritDoc}
069     */
070    @Override
071    public int hashCode() {
072        int hash = "NullaryPredicatePredicate".hashCode();
073        hash ^= predicate.hashCode();
074        return hash;
075    }
076
077    /**
078     * {@inheritDoc}
079     */
080    @Override
081    public String toString() {
082        return "NullaryPredicatePredicate<" + predicate + ">";
083    }
084
085    /**
086     * Adapt a NullaryPredicate to the Predicate interface.
087     * @param <A> the argument type.
088     * @param predicate to adapt
089     * @return NullaryPredicatePredicate<A>
090     */
091    public static <A> NullaryPredicatePredicate<A> adapt(NullaryPredicate predicate) {
092        return null == predicate ? null : new NullaryPredicatePredicate<A>(predicate);
093    }
094
095}