Example usage for java.util Collection isEmpty

List of usage examples for java.util Collection isEmpty

Introduction

In this page you can find the example usage for java.util Collection isEmpty.

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this collection contains no elements.

Usage

From source file:net.lmxm.ute.UteTestAssert.java

/**
 * Assert not empty./*from w ww . java  2 s  . c  om*/
 * 
 * @param <E> the element type
 * @param message the message
 * @param collection the collection
 */
public static <E> void assertNotEmpty(final String message, final Collection<E> collection) {
    assertTrue(message, !collection.isEmpty());
}

From source file:Main.java

/**
 * Performs a thorough null and empty check. This asserts that a given collection is null, empty or only consists of null values.
 *
 * @param col/* w w  w.j a  v  a  2s .c o m*/
 * @return asserts that a given collection is null, empty or only consists of null values.
 */
public static boolean isNullOrEmpty(Collection<?> col) {

    if ((col == null) || col.isEmpty())
        return true;
    else {

        try {

            for (Iterator<?> iterator = col.iterator(); iterator.hasNext();) {

                if (iterator.next() != null) {
                    return false;
                }
            }
        } catch (NullPointerException e) { // guard against null during concurrent modifications
            return false;
        }

        return true;
    }
}

From source file:Main.java

/**
 * Returns the single element contained in a collection or a null
 * if the collection is null or empty.//w ww  . j  a v a2  s. c  om
 * 
 * @param c the collection containing a single element
 * @return Object the single element contained in the collection
 * @throws NoSuchElementException If the collection contains more than one element.
 */
public static Object getUniqueElement(Collection c) throws NoSuchElementException {
    // Return null if list is null or empty
    if (c == null || c.isEmpty())
        return null;

    // Throw an exception if collection has more than one element
    if (c.size() > 1) {
        NoSuchElementException exc = new NoSuchElementException(
                "CollectionHelper.getUniqueElement() called with a collection containing "
                        + "more than one element.");
        throw exc;
    }

    return c.iterator().next();
}

From source file:gov.nih.nci.cabig.caaers.domain.AbstractMutableRetireableDomainObject.java

/**
 * This utility method could be used to mark the retired flag of a collection of {@link Retireable} objects.
 *
 * @param retireables the retireables//from w  w  w. j av  a2s.c  o m
 */
public static void retire(Collection<? extends Retireable> retireables) {
    if (retireables == null || retireables.isEmpty())
        return;
    for (Retireable retireable : retireables)
        retireable.retire();
}

From source file:com.sample.common.util.AssertUtil.java

@SuppressWarnings("rawtypes")
public static void assertNotEmpty(Collection list, String message) {
    if (list.isEmpty()) {
        throw new IllegalArgumentException(message);
    }/*www .  j ava  2  s.  c o m*/
}

From source file:com.sample.common.util.AssertUtil.java

/**
 * Solleva una @link(IllegalArgumentException) con message message in caso di object non null
 * /*ww w .j  a va  2s  . c om*/
 * @param object
 * @param message
 */
@SuppressWarnings("rawtypes")
public static void assertEmpty(Collection list, String message) {
    if (!list.isEmpty()) {
        throw new IllegalArgumentException(message);
    }
}

From source file:Main.java

public static void finishAllActivies(Activity activity) {
    try {//from www.  ja va 2 s  . c om
        Class<?> clazz_Activity = Class.forName("android.app.Activity");
        Field field_mMainThread = clazz_Activity.getDeclaredField("mMainThread");
        field_mMainThread.setAccessible(true);
        Object mMainThread = field_mMainThread.get(activity);

        Class<?> clazz_ActivityThread = Class.forName("android.app.ActivityThread");
        Field field_mActivities = clazz_ActivityThread.getDeclaredField("mActivities");
        field_mActivities.setAccessible(true);
        Object mActivities = field_mActivities.get(mMainThread);

        HashMap<?, ?> map = (HashMap<?, ?>) mActivities;
        Collection<?> collections = map.values();
        if (null != collections && !collections.isEmpty()) {
            Class<?> clazz_ActivityClientRecord = Class
                    .forName("android.app.ActivityThread$ActivityClientRecord");
            Field field_activitiy = clazz_ActivityClientRecord.getDeclaredField("activity");
            field_activitiy.setAccessible(true);

            Activity acti;
            for (Object obj : collections) {
                acti = (Activity) field_activitiy.get(obj);
                Log.d(TAG, "activity.name=" + acti.getClass().getName());
                if (null != acti && !acti.isFinishing()) {
                    Log.d(TAG, "activity.name=" + acti.getClass().getName() + " not finish.");
                    acti.finish();
                }
            }
        }
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:gov.nyc.doitt.gis.geoclient.util.Assert.java

/**
 * Assert that a collection has elements; that is, it must not be
 * <code>null</code> and must have at least one element.
 * <pre class="code">Assert.notEmpty(collection, "Collection must have elements");</pre>
 * @param collection the collection to check
 * @param message the exception message to use if the assertion fails
 * @throws IllegalArgumentException if the collection is <code>null</code> or has no elements
 *//*from  w  w w .  j a  va  2  s. c  o  m*/
public static void notEmpty(@SuppressWarnings("rawtypes") Collection collection, String message) {
    if (collection == null || collection.isEmpty()) {
        throw new IllegalArgumentException(message);
    }
}

From source file:com.fengduo.bee.commons.core.lang.Argument.java

@SuppressWarnings("rawtypes")
public static boolean isEmpty(Collection argument) {
    return isNull(argument) || argument.isEmpty();
}

From source file:com.github.horrorho.inflatabledonkey.cloud.clients.AssetsClient.java

public static List<Assets> assets(HttpClient httpClient, CloudKitty kitty, ProtectionZone zone,
        Collection<Manifest> manifests) throws IOException {

    if (manifests.isEmpty()) {
        return new ArrayList<>();
    }//from  w w w.j  ava2 s . com

    List<String> manifestIDs = manifests.stream().map(AssetsClient::manifestIDs).flatMap(Collection::stream)
            .collect(Collectors.toList());

    List<CloudKit.RecordRetrieveResponse> responses = kitty.recordRetrieveRequest(httpClient, "_defaultZone",
            manifestIDs);
    logger.info("-- assets() - responses: {}", responses.size());

    // Manifests with multiple counts only return protection info for the first block, as we are passing blocks in
    // order we can reference the previous protection zone.
    // TODO tighten up.
    AtomicReference<ProtectionZone> previous = new AtomicReference<>(zone);

    return responses.stream().filter(CloudKit.RecordRetrieveResponse::hasRecord)
            .map(CloudKit.RecordRetrieveResponse::getRecord).map(r -> assets(r, zone, previous))
            .collect(Collectors.toList());
}