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

se.softhouse.common.guavaextensions.Preconditions2 Maven / Gradle / Ivy

/* Copyright 2013 Jonatan Jönsson
 *
 *    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 se.softhouse.common.guavaextensions;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

/**
 * Additional {@link Preconditions pre-condition} checks
 */
@Immutable
public final class Preconditions2
{
	private Preconditions2()
	{
	}

	/**
	 * 
	 * Returns {@code items} as a modifiable {@link List} that's guaranteed not to contain
	 * any nulls.
	 * Use {@link ImmutableList#copyOf(Iterable)} if the returned list doesn't have to be
	 * modifiable and a {@code message} isn't needed.
	 * 
	 * @throws NullPointerException with {@code message} (plus the index of the first
	 *             null element) if any element in {@code items} is null
	 * 
*/ @Nonnull public static List checkNulls(Iterable items, String message) { checkNotNull(message, "a message describing what it is that is containing the elements must be given"); List nullCheckedList = Lists.newArrayList(items); int index = 0; for(T element : nullCheckedList) { if(element == null) throw new NullPointerException(message + " (discovered one at index " + index + ")"); index++; } return nullCheckedList; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy