Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.List;

public class Main {
    public static <T> T getSingle(List<T> collection) {
        checkIsNull(collection);

        if (collection.size() != 1) {
            throw new IllegalArgumentException("The supplied collection must contain exactly one item.");
        }

        return getFirst(collection);
    }

    private static <T> void checkIsNull(List<T> collection) {
        if (collection == null) {
            throw new NullPointerException("The supplied collection is null.");
        }
    }

    public static <T> T getFirst(List<T> collection) {
        checkIsNull(collection);

        if (collection.size() < 1) {
            throw new IndexOutOfBoundsException("The supplied collection is null or empty.");
        }

        return collection.get(0);
    }
}