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