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

com.speedment.common.logger.internal.util.NullUtil Maven / Gradle / Ivy

Go to download

A Speedment bundle that shades all dependencies into one jar. This is useful when deploying an application on a server.

The newest version!
/*
 *
 * Copyright (c) 2006-2019, Speedment, Inc. 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.speedment.common.logger.internal.util;

import java.util.Arrays;
import java.util.Collection;

import static com.speedment.common.logger.internal.util.StaticClassUtil.instanceNotAllowed;
import static java.util.Objects.requireNonNull;

/**
 *
 * @author  Emil Forslund
 * @author  Per Minborg
 * @since   2.1
 */
public final class NullUtil {

    private static final String PARAMETER_0_IS_NULL = "Parameter 0 is null.";

    private NullUtil() {}

    /**
     * Checks if this set is non null and also that all members are non-null.
     * If a null is detected a NullPointerException is thrown.
     *
     * @param          the item type
     * @param          the collection type
     * @param collection  to check
     * @return            the same array instance as provided
     * 
     * @throws NullPointerException  if a null is found in the set or if the
     *                               set itself is null
     */
    public static > C requireNonNullElements(C collection) {
        requireNonNull(collection, "The provided collection is null.");
        
        collection.forEach(t -> {
            if (t == null) {
                throw new NullPointerException(
                    "An item in the collection is null."
                );
            }
        });
        
        return collection;
    }
    
    /**
     * Checks if this array is non null and also that all members are non-null.
     * If a null is detected a NullPointerException is thrown.
     *
     * @param     array type
     * @param array  to check
     * @return       the same array instance as provided
     * 
     * @throws NullPointerException  if a null is found in the array or if the
     *                               array itself is null
     */
    public static  T[] requireNonNullElements(T[] array) {
        requireNonNull(array, "The provided array is null.");
        int len = array.length;
        for (int i = 0; i < len; i++) {
            if (array[i] == null) {
                throw new NullPointerException("Item " + i + " in the array " + Arrays.toString(array) + " is null");
            }
        }
        return array;
    }
    
    /**
     * Checks if all parameters are non-null. If a null is detected a 
     * {@code NullPointerException} is thrown.
     *
     * @param   array type
     * @param t0   to check
     * 
     * @throws NullPointerException  if a null is found in the parameters
     */
    public static  void requireNonNulls(T t0) {
        requireNonNull(t0, PARAMETER_0_IS_NULL);
    }
    
    /**
     * Checks if all parameters are non-null. If a null is detected a 
     * {@code NullPointerException} is thrown.
     *
     * @param   array type
     * @param t0   to check
     * @param t1   to check
     * 
     * @throws NullPointerException  if a null is found in the parameters
     */
    public static  void requireNonNulls(T t0, T t1) {
        requireNonNull(t0, PARAMETER_0_IS_NULL);
        requireNonNull(t1, "Parameter 1 is null.");
    }
    
    /**
     * Checks if all parameters are non-null. If a null is detected a 
     * {@code NullPointerException} is thrown.
     *
     * @param   array type
     * @param t0   to check
     * @param t1   to check
     * @param t2   to check
     * 
     * @throws NullPointerException  if a null is found in the parameters
     */
    public static  void requireNonNulls(T t0, T t1, T t2) {
        requireNonNull(t0, PARAMETER_0_IS_NULL);
        requireNonNull(t1, "Parameter 1 is null.");
        requireNonNull(t2, "Parameter 2 is null.");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy