Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * File:                CollectionUtil.java
 * Authors:             Justin Basilico
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 *
 * Copyright March 25, 2008, Sandia Corporation.
 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
 * license for use of this work by or on behalf of the U.S. Government. Export
 * of this program may require a license from the United States Government.
 * See CopyrightHistory.txt for complete details.
 *
 */

import java.util.Collection;

import java.util.Iterator;

import java.util.List;

public class Main {
    /**
     * Gets the first element from an iterable. If the iterable is null or
     * empty, null is returned.
     *
     * @param   <T>
     *      The type of element.
     * @param   iterable
     *      The iterable to get the first element from.
     * @return
     *      The first element from the iterable, if one exists. Otherwise,
     *      null.
     */
    public static <T> T getFirst(final Iterable<? extends T> iterable) {
        if (iterable == null) {
            // No first element.
            return null;
        }

        final Iterator<? extends T> iterator = iterable.iterator();
        if (iterator.hasNext()) {
            return iterator.next();
        } else {
            // No first element.
            return null;
        }
    }

    /**
     * Gets the first element of the list. If the list is null or empty, null
     * is returned.
     *
     * @param   <T>
     *      The type of element in the list.
     * @param   list
     *      The list to get the first element from.
     * @return
     *      The first element from the list, if one exists. Otherwise, null.
     */
    public static <T> T getFirst(final List<? extends T> list) {
        if (list == null || list.isEmpty()) {
            return null;
        } else {
            return list.get(0);
        }
    }

    /**
     * Returns true if the given collection is null or empty.
     *
     * @param   collection
     *      The collection to determine if it is null or empty.
     * @return
     *      True if the given collection is null or empty.
     */
    public static boolean isEmpty(final Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * Returns true if the given iterable is null or empty.
     *
     * @param   iterable
     *      The iterable to determine if it is null or empty.
     * @return
     *      True if the given iterable is null or empty.
     */
    public static boolean isEmpty(final Iterable<?> iterable) {
        if (iterable == null) {
            // It is null, so it is empty.
            return true;
        } else if (iterable instanceof Collection) {
            return ((Collection<?>) iterable).isEmpty();
        } else {
            return !iterable.iterator().hasNext();
        }
    }
}