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

com.speedment.util.NullUtil Maven / Gradle / Ivy

Go to download

An object-oriented code generator for Java that is built using the Model-View-Controller (MVC) design philosophy.

There is a newer version: 2.4.5
Show newest version
/**
 *
 * Copyright (c) 2006-2016, 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.util;

import com.speedment.annotation.Api;
import java.util.Arrays;
import java.util.Collection;
import static java.util.Objects.requireNonNull;
import static com.speedment.util.StaticClassUtil.instanceNotAllowed;

/**
 *
 * @author  Emil Forslund
 * @author  Per Minborg
 * @since   2.1
 */
@Api(version = "2.3")
public final class 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.");
    }
    
    /**
     * Utility classes should not be instantiated.
     */
    private NullUtil() { instanceNotAllowed(getClass()); }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy