Java tutorial
//package com.java2s; import java.lang.reflect.Array; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.Map; public class Main { /** * Checks if the specified collection/array/iterator is empty. * <p> * This method can handles objects as follows * <ul> * <li>Collection - via collection isEmpty * <li>Map - via map isEmpty * <li>Array - using array size * <li>Iterator - via hasNext * <li>Enumeration - via hasMoreElements * </ul> * <p> * Note: This method is named to avoid clashing with * {@link #isEmpty(Collection)}. * * @param object * the object to get the size of, not null * @return true if empty * @throws IllegalArgumentException * thrown if object is not recognised or null */ public static boolean sizeIsEmpty(Object object) { if (object instanceof Collection) { return ((Collection<?>) object).isEmpty(); } else if (object instanceof Map) { return ((Map<?, ?>) object).isEmpty(); } else if (object instanceof Object[]) { return ((Object[]) object).length == 0; } else if (object instanceof Iterator) { return !((Iterator<?>) object).hasNext(); } else if (object instanceof Enumeration) { return !((Enumeration<?>) object).hasMoreElements(); } else if (object == null) { throw new IllegalArgumentException("Unsupported object type: null"); } else { try { return Array.getLength(object) == 0; } catch (IllegalArgumentException ex) { throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName()); } } } public static boolean isEmpty(Map<?, ?> map) { return (map == null) || (map.size() == 0); } public static boolean isEmpty(Collection<?> collection) { return (collection == null) || (collection.size() == 0); } /** * Gets the size of the collection/iterator specified. * <p> * This method can handles objects as follows * <ul> * <li>Collection - the collection size * <li>Map - the map size * <li>Array - the array size * <li>Iterator - the number of elements remaining in the iterator * <li>Enumeration - the number of elements remaining in the enumeration * </ul> * * @param object * the object to get the size of * @return the size of the specified collection * @throws IllegalArgumentException * thrown if object is not recognised or null * @since Commons Collections 3.1 */ public static int size(Object object) { int total = 0; if (object instanceof Map) { total = ((Map<?, ?>) object).size(); } else if (object instanceof Collection) { total = ((Collection<?>) object).size(); } else if (object instanceof Object[]) { total = ((Object[]) object).length; } else if (object instanceof Iterator) { Iterator<?> it = (Iterator<?>) object; while (it.hasNext()) { total++; it.next(); } } else if (object instanceof Enumeration) { Enumeration<?> it = (Enumeration<?>) object; while (it.hasMoreElements()) { total++; it.nextElement(); } } else if (object == null) { throw new IllegalArgumentException("Unsupported object type: null"); } else { try { total = Array.getLength(object); } catch (IllegalArgumentException ex) { throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName()); } } return total; } }