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