All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ait.tooling.common.api.java.util.Predicates Maven / Gradle / Ivy

/*
   Copyright (c) 2014,2015,2016 Ahome' Innovation Technologies. All rights reserved.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package com.ait.tooling.common.api.java.util;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.ait.tooling.common.api.java.util.function.Predicate;

public final class Predicates implements Serializable
{
    private static final long serialVersionUID = 5253333459696388521L;

    protected Predicates()
    {
    }

    public static final  Predicate notNull()
    {
        return new Predicate()
        {
            @Override
            public final boolean test(final T value)
            {
                return (null != value);
            }
        };
    }

    public static final  Predicate negate(final Predicate predicate)
    {
        Objects.requireNonNull(predicate);

        return new Predicate()
        {
            @Override
            public final boolean test(final T value)
            {
                return (false == predicate.test(value));
            }
        };
    }

    @SafeVarargs
    public static final  Predicate and(final Predicate... predicates)
    {
        return and(Arrays.asList(Objects.requireNonNull(predicates)));
    }

    public static final  Predicate and(final List> list)
    {
        Objects.requireNonNull(list);

        return new Predicate()
        {
            @Override
            public final boolean test(final T value)
            {
                for (Predicate pred : list)
                {
                    if (false == pred.test(value))
                    {
                        return false;
                    }
                }
                return true;
            }
        };
    }

    @SafeVarargs
    public static final  Predicate or(final Predicate... predicates)
    {
        return or(Arrays.asList(Objects.requireNonNull(predicates)));
    }

    public static final  Predicate or(final List> list)
    {
        Objects.requireNonNull(list);

        return new Predicate()
        {
            @Override
            public final boolean test(final T value)
            {
                for (Predicate pred : list)
                {
                    if (pred.test(value))
                    {
                        return true;
                    }
                }
                return false;
            }
        };
    }

    public static final  Predicate isEqual(final T object)
    {
        return new Predicate()
        {
            @Override
            public final boolean test(final T value)
            {
                if (value == object)
                {
                    return true;
                }
                if (null != value)
                {
                    return value.equals(object);
                }
                if (null != object)
                {
                    return object.equals(value);
                }
                return false;
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy