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

net.dongliu.commons.Predicates Maven / Gradle / Ivy

There is a newer version: 12.0.2
Show newest version
package net.dongliu.commons;

import javax.annotation.Nullable;
import java.util.Collection;

/**
 * @author Liu Dong
 */
public class Predicates {

    /**
     * Require all value to be non-null
     *
     * @return the values
     */
    @SafeVarargs
    public static  T[] requireValuesNonNull(@Nullable T... values) {
        if (values == null) {
            throw new NullPointerException();
        }
        for (T value : values) {
            if (value == null) {
                throw new NullPointerException();
            }
        }
        return values;
    }

    /**
     * Require all value to be non-null
     *
     * @return the values
     */
    public static  T[] requireValuesNonNull(@Nullable T[] values, String msg) {
        if (values == null) {
            throw new NullPointerException(msg);
        }
        for (T value : values) {
            if (value == null) {
                throw new NullPointerException(msg);
            }
        }
        return values;
    }

    /**
     * Require all value to be non-null
     *
     * @return the values
     */
    public static , T> S requireValuesNonNull(@Nullable S values, String msg) {
        if (values == null) {
            throw new NullPointerException(msg);
        }
        for (T value : values) {
            if (value == null) {
                throw new NullPointerException(msg);
            }
        }
        return values;
    }

    /**
     * Require all value to be non-null
     *
     * @return the values
     */
    public static , T> S requireValuesNonNull(@Nullable S values) {
        if (values == null) {
            throw new NullPointerException();
        }
        for (T value : values) {
            if (value == null) {
                throw new NullPointerException();
            }
        }
        return values;
    }

    /**
     * Require argument to satisfy condition, or throw IllegalArgument exception
     */
    public static void requireArgument(boolean condition, String msg) {
        if (!condition) {
            throw new IllegalArgumentException(msg);
        }
    }

    /**
     * Require argument to satisfy condition, or throw IllegalArgument exception
     */
    public static void requireArgument(boolean condition) {
        if (!condition) {
            throw new IllegalArgumentException();
        }
    }

}