    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.collections4.functors;
    018
    019import java.io.Serializable;
    020import java.util.Objects;
    021
    022import org.apache.commons.collections4.Predicate;
    023
    024/**
    025 * Predicate implementation that returns the opposite of the decorated predicate.
    026 *
    027 * @since 3.0
    028 */
    029public final class NotPredicate<T> implements PredicateDecorator<T>, Serializable {
    030
    031    /** Serial version UID */
    032    private static final long serialVersionUID = -2654603322338049674L;
    033
    034    /**
    035     * Factory to create the not predicate.
    036     *
    037     * @param <T> the type that the predicate queries
    038     * @param predicate  the predicate to decorate, not null
    039     * @return the predicate
    040     * @throws NullPointerException if the predicate is null
    041     */
    042    public static <T> Predicate<T> notPredicate(final Predicate<? super T> predicate) {
    043        return new NotPredicate<>(Objects.requireNonNull(predicate, "predicate"));
    044    }
    045
    046    /** The predicate to decorate */
    047    private final Predicate<? super T> iPredicate;
    048
    049    /**
    050     * Constructor that performs no validation.
    051     * Use {@code notPredicate} if you want that.
    052     *
    053     * @param predicate  the predicate to call after the null check
    054     */
    055    public NotPredicate(final Predicate<? super T> predicate) {
    056        iPredicate = predicate;
    057    }
    058
    059    /**
    060     * Evaluates the predicate returning the opposite to the stored predicate.
    061     *
    062     * @param object  the input object
    063     * @return true if predicate returns false
    064     */
    065    @Override
    066    public boolean evaluate(final T object) {
    067        return !iPredicate.evaluate(object);
    068    }
    069
    070    /**
    071     * Gets the predicate being decorated.
    072     *
    073     * @return the predicate as the only element in an array
    074     * @since 3.1
    075     */
    076    @Override
    077    @SuppressWarnings("unchecked")
    078    public Predicate<? super T>[] getPredicates() {
    079        return new Predicate[] {iPredicate};
    080    }
    081
    082}