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