/** * Attempt to determine if a iterable-like is empty, preferably without iterating. * @param obj the iterable-like to check for emptiness * @return emptiness to fill your heart */ public static boolean isEmpty(final Object obj) { if (obj == null) { throw new IllegalArgumentException("cannot determine emptiness of null"); } if (obj instanceof Collection) { return ((Collection<?>) obj).isEmpty(); } if (obj instanceof Iterable) { return !((Iterable<?>) obj).iterator().hasNext(); } if (obj.getClass().isArray()) { return Array.getLength(obj) == 0; } throw new IllegalArgumentException(getTypeWarning(obj.getClass())); }
/** * Given an iterable object (which may be a iterator, iterable, primitive * or reference array), return an iterator over its (possibly boxed) elements. * * @param iterable the iterable-like * @return an iterator of the given array's elements */ @SuppressWarnings("unchecked") public static Iterator<Object> of(Object iterable) { if (iterable == null) { throw new IllegalArgumentException("can't iterate null"); } if (iterable instanceof Iterator<?>) { return (Iterator<Object>) iterable; } else if (iterable instanceof Iterable<?>) { return ((Iterable<Object>) iterable).iterator(); } Class<?> klass = iterable.getClass(); if (!klass.isArray()) { throw new IllegalArgumentException(getTypeWarning(klass)); } if (klass.getComponentType().isPrimitive()) { return new PrimitiveArrayIterator(iterable); } return Arrays.asList((Object[]) iterable).iterator(); }
/** * Attempt to determine if a iterable-like is empty, preferably without iterating. * @param obj the iterable-like to check for emptiness * @return emptiness to fill your heart */ public static boolean isEmpty(final Object obj) { if (obj == null) { throw new IllegalArgumentException("cannot determine emptiness of null"); } if (obj instanceof Collection) { return ((Collection<?>) obj).isEmpty(); } if (obj instanceof Iterable) { return !((Iterable<?>) obj).iterator().hasNext(); } if (obj.getClass().isArray()) { return Array.getLength(obj) == 0; } throw new IllegalArgumentException(getTypeWarning(obj.getClass())); }
/** * Given an iterable object (which may be a iterator, iterable, primitive * or reference array), return an iterator over its (possibly boxed) elements. * * @return an iterator of the given array's elements */ @SuppressWarnings("unchecked") public static Iterator<Object> of(Object iterable) { if (iterable == null) { throw new IllegalArgumentException("can't iterate null"); } if (iterable instanceof Iterator<?>) { return (Iterator<Object>) iterable; } else if (iterable instanceof Iterable<?>) { return ((Iterable<Object>) iterable).iterator(); } Class<? extends Object> klass = iterable.getClass(); if(!klass.isArray()) { throw new IllegalArgumentException(getTypeWarning(klass)); } if (klass.getComponentType().isPrimitive()) { return new PrimitiveArrayIterator(iterable); } return Arrays.asList((Object[])iterable).iterator(); }