Assert/Check whether the collection is null or empty. - Java java.lang

Java examples for java.lang:Assert

Description

Assert/Check whether the collection is null or empty.

Demo Code

/*//from   w  ww. jav  a 2  s .c  o m
 * oxCore is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */
//package com.java2s;
import java.util.Collection;

public class Main {
    public static void main(String[] argv) throws Exception {
        Collection list = java.util.Arrays.asList("asdf", "java2s.com");
        String message = "java2s.com";
        assertNotEmpty(list, message);
    }

    /**
     * Check whether the collection is null or empty. If it is, throw an exception and display the message.
     * 
     * @param list the collecion to check.
     * @param message the message to display if the object is null.
     */
    public static void assertNotEmpty(final Collection<?> list,
            final String message) {
        assertNotNull(list, message);
        if (list.isEmpty()) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * Check whether the object is null or not. If it is, throw an exception and display the message.
     * 
     * @param object the object to check.
     * @param message the message to display if the object is null.
     */
    public static void assertNotNull(final Object object,
            final String message) {
        if (object == null) {
            throw new IllegalArgumentException(message);
        }
    }
}

Related Tutorials