Examine a collection and determines if it is null, empty, or contains only null values - Java java.util

Java examples for java.util:Collection Size

Description

Examine a collection and determines if it is null, empty, or contains only null values

Demo Code

/*/*w  ww.j  a v a 2  s .c  o m*/
 * Copyright (C) 2013
 *
 * 52?North Initiative for Geospatial Open Source Software GmbH
 * Contact: Andreas Wytzisk
 * Martin-Luther-King-Weg 24
 * 48155 Muenster, Germany
 * info@52north.org
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
//package com.java2s;
import java.util.ArrayList;

import java.util.Collection;
import java.util.Collections;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] argv) {
        Collection collection = java.util.Arrays.asList("asdf",
                "java2s.com");
        System.out.println(nullEmptyOrContainsOnlyNulls(collection));
    }

    /**
     * Examine a collection and determines if it is null, empty, or contains only null values
     * 
     * @param collection Collection to examine
     * @return whether the collection is null, empty, or contains only nulls
     */
    public static boolean nullEmptyOrContainsOnlyNulls(
            final Collection<? extends Object> collection) {
        if (isNotEmpty(collection)) {
            for (final Object obj : collection) {
                if (obj != null) {
                    return false;
                }
            }
        }
        return true;
    }

    public static <T> List<T> asList(final Iterable<? extends T> iterable) {
        return (iterable instanceof Collection) ? new LinkedList<T>(
                (Collection<? extends T>) iterable) : new LinkedList<T>() {
            private static final long serialVersionUID = 3109256773218160485L;
            {
                if (iterable != null) {
                    for (final T t : iterable) {
                        add(t);
                    }
                }
            }
        };
    }

    public static <T> List<T> asList(final T t, final T... ts) {
        final ArrayList<T> list = new ArrayList<T>(ts.length + 1);
        list.add(t);
        Collections.addAll(list, ts);
        return list;
    }

    /**
     * Check if collection is not null and not empty
     * 
     * @param collection
     *            Collection to check
     *            
     * @return empty or not
     */
    public static <T> boolean isNotEmpty(final Collection<T> collection) {
        return collection != null && !collection.isEmpty();
    }

    /**
     * Check if collection is not null and not empty
     * 
     * 
     * @param map
     *          Map to check
     * 
     * @return <tt>false</tt>, if map is <tt>null</tt> or empty, else <tt>true</tt>. 
     */
    public static <K, V> boolean isNotEmpty(final Map<K, V> map) {
        return map != null && !map.isEmpty();
    }

    /**
     * Check if collection is not <tt>null</tt> and empty
     * 
     * @param collection
     *          Collection to check
     * 
     * @return <tt>true</tt>, if collection is not null and empty, else <tt>false</tt>
     */
    public static <T> boolean isEmpty(final Collection<T> collection) {
        return collection != null && collection.isEmpty();
    }

    /**
     * Check if map is not <tt>null</tt> and empty
     * 
     * @param map
     *          map to check
     * 
     * @return <tt>true</tt>, if map is not null and empty, else <tt>false</tt>
     */
    public static <K, V> boolean isEmpty(final Map<K, V> map) {
        return map != null && map.isEmpty();
    }
}

Related Tutorials