001package co.codewizards.cloudstore.core.util; 002 003import static java.util.Objects.*; 004 005import java.util.Collection; 006 007/** 008 * @author Sebastian Schefczyk 009 * 010 */ 011public final class AssertUtil { 012 013 private AssertUtil() { } 014 015 public static final <T> T assertNotNull(final T object, final String name, final String additionalInfoTemplate, final Object ... additionalInfoArgs) { 016 if (additionalInfoTemplate == null) 017 return requireNonNull(object, name); 018 019 if (object == null) 020 throw new IllegalArgumentException(String.format("%s == null :: ", name) + String.format(additionalInfoTemplate, additionalInfoArgs)); 021 022 return object; 023 } 024 025 public static final <T> T[] assertNotNullAndNoNullElement(final T[] array, final String name) { 026 requireNonNull(array, name); 027 for (int i = 0; i < array.length; i++) { 028 if (array[i] == null) 029 throw new IllegalArgumentException(String.format("%s[%s] == null", name, i)); 030 } 031 return array; 032 } 033 034 public static final <E, T extends Collection<E>> T assertNotNullAndNoNullElement(final T collection, final String name) { 035 requireNonNull(collection, name); 036 int i = -1; 037 for (final E element : collection) { 038 ++i; 039 if (element == null) 040 throw new IllegalArgumentException(String.format("%s[%s] == null", name, i)); 041 } 042 return collection; 043 } 044 045 public static final <E, T extends Collection<E>> T assertNotEmpty(final T collection, final String name) { 046 requireNonNull(collection, name); 047 if (collection.isEmpty()) 048 throw new IllegalArgumentException(String.format("%s is empty", name)); 049 050 return collection; 051 } 052}