Add Assert.requireNonEmpty() and stop supporting Iterable

Some Iterable<T> classes do not support returning an iterator more than once!


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/f6a856d0
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/f6a856d0
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/f6a856d0

Branch: refs/heads/master
Commit: f6a856d08545fd58cfca1ead2acd43ccb96923a6
Parents: 16bfee4
Author: Matt Sicker <[email protected]>
Authored: Fri Jan 6 23:05:37 2017 -0600
Committer: Matt Sicker <[email protected]>
Committed: Fri Jan 6 23:05:37 2017 -0600

----------------------------------------------------------------------
 .../apache/logging/log4j/core/util/Assert.java  | 33 ++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f6a856d0/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Assert.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Assert.java 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Assert.java
index d46c06a..da7f45a 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Assert.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Assert.java
@@ -16,6 +16,7 @@
  */
 package org.apache.logging.log4j.core.util;
 
+import java.util.Collection;
 import java.util.Map;
 
 /**
@@ -49,8 +50,8 @@ public final class Assert {
         if (o.getClass().isArray()) {
             return ((Object[]) o).length == 0;
         }
-        if (o instanceof Iterable) {
-            return !((Iterable<?>) o).iterator().hasNext();
+        if (o instanceof Collection) {
+            return ((Collection<?>) o).isEmpty();
         }
         if (o instanceof Map) {
             return ((Map<?, ?>) o).isEmpty();
@@ -69,6 +70,34 @@ public final class Assert {
         return !isEmpty(o);
     }
 
+    /**
+     * Checks a value for emptiness and throws an IllegalArgumentException if 
it's empty.
+     *
+     * @param value value to check for emptiness
+     * @param <T>   type of value
+     * @return the provided value if non-empty
+     * @since 2.8
+     */
+    public static <T> T requireNonEmpty(final T value) {
+        return requireNonEmpty(value, "");
+    }
+
+    /**
+     * Checks a value for emptiness and throws an IllegalArgumentException if 
it's empty.
+     *
+     * @param value   value to check for emptiness
+     * @param message message to provide in exception
+     * @param <T>     type of value
+     * @return the provided value if non-empty
+     * @since 2.8
+     */
+    public static <T> T requireNonEmpty(final T value, final String message) {
+        if (isEmpty(value)) {
+            throw new IllegalArgumentException(message);
+        }
+        return value;
+    }
+
     public static int valueIsAtLeast(final int value, final int minValue) {
         if (value < minValue) {
             throw new IllegalArgumentException("Value should be at least " + 
minValue + " but was " + value);

Reply via email to