Java Collection Empty isNotEmpty(T collection, String exceptionMessage)

Here you can find the source of isNotEmpty(T collection, String exceptionMessage)

Description

Validates that a collection is not empty.

License

Apache License

Parameter

Parameter Description
collection The collection to be validated.
exceptionMessage The message of the exception that is thrown if the collection does not contain an element.
T The type of the collection.

Return

The same collection that was validated.

Declaration

public static <T extends Collection<?>> T isNotEmpty(T collection, String exceptionMessage) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**// www.  j ava  2s .com
     * Validates that a collection is not empty.
     *
     * @param collection       The collection to be validated.
     * @param exceptionMessage The message of the exception that is thrown if the collection does not contain an element.
     * @param <T>              The type of the collection.
     * @return The same collection that was validated.
     */
    public static <T extends Collection<?>> T isNotEmpty(T collection, String exceptionMessage) {
        if (collection.size() == 0) {
            throw new IllegalArgumentException(exceptionMessage);
        }
        return collection;
    }
}

Related

  1. isNotEmpty(final Collection coll)
  2. isNotEmpty(final Collection collection)
  3. isNotEmpty(final Collection collection)
  4. isNotEmpty(final Collection collection)
  5. isNotEmpty(final Collection c)
  6. isNotEmptyCollection(Collection collection)
  7. isNotEmptyCollection(Object[] objs)
  8. isNotEmptyException(Collection collection)
  9. isNotEmptyOrNull(Collection collection)