Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Collection;

public class Main {

    public static Number getNumber(Collection<?> list) {
        Object obj = getUnique(list);
        if (obj instanceof Number)
            return ((Number) obj);
        throw new IllegalArgumentException("list result is not instanceof Number");
    }

    public static <T> T getUnique(Collection<T> list) {
        if (list == null)
            throw new IllegalArgumentException(" collection is null! collection must be not null");
        else if (list.size() != 1)
            throw new IllegalArgumentException(
                    list.size() + " collection must be unique : " + list.iterator().next().toString());
        else
            return list.iterator().next();
    }
}