Skip to content

Commit 005055f

Browse files
authored
Add U.isNotEmpty(interable).
1 parent f8c0c1c commit 005055f

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/main/java/com/github/underscore/U.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,11 +2093,23 @@ public static <K, V> boolean isEmpty(final Map<K, V> object) {
20932093
* Documented, #isEmpty
20942094
*/
20952095
public static <T> boolean isEmpty(final Iterable<T> iterable) {
2096-
return iterable == null || size(iterable) == 0;
2096+
return iterable == null || !iterable.iterator().hasNext();
20972097
}
20982098

20992099
public boolean isEmpty() {
2100-
return iterable == null || size(iterable) == 0;
2100+
return iterable == null || !iterable.iterator().hasNext();
2101+
}
2102+
2103+
public static <K, V> boolean isNotEmpty(final Map<K, V> object) {
2104+
return object != null && !object.isEmpty();
2105+
}
2106+
2107+
public static <T> boolean isNotEmpty(final Iterable<T> iterable) {
2108+
return iterable != null && iterable.iterator().hasNext();
2109+
}
2110+
2111+
public boolean isNotEmpty() {
2112+
return iterable != null && iterable.iterator().hasNext();
21012113
}
21022114

21032115
/*
@@ -2706,6 +2718,10 @@ public boolean isEmpty() {
27062718
return U.isEmpty(list);
27072719
}
27082720

2721+
public boolean isNotEmpty() {
2722+
return U.isNotEmpty(list);
2723+
}
2724+
27092725
public int size() {
27102726
return U.size(list);
27112727
}

src/test/java/com/github/underscore/ObjectsTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,23 @@ public void isEmpty() {
244244
assertTrue(U.isEmpty(new HashMap<String, String>()));
245245
assertFalse(U.isEmpty(new HashMap<String, String>() { { put("", ""); } }));
246246
}
247+
248+
@Test
249+
@SuppressWarnings("unchecked")
250+
public void isNotEmpty() {
251+
assertFalse(U.isNotEmpty((List) null));
252+
assertFalse(U.isNotEmpty(new ArrayList<String>()));
253+
assertFalse(new U((List) null).isNotEmpty());
254+
assertFalse(new U(new ArrayList<String>()).isNotEmpty());
255+
assertFalse(U.chain((List) null).isNotEmpty());
256+
assertFalse(U.chain(new ArrayList<String>()).isNotEmpty());
257+
assertTrue(U.isNotEmpty(asList("")));
258+
assertTrue(new U(asList("")).isNotEmpty());
259+
assertTrue(U.chain(asList("")).isNotEmpty());
260+
assertFalse(U.isNotEmpty((Map) null));
261+
assertFalse(U.isNotEmpty(new HashMap<String, String>()));
262+
assertTrue(U.isNotEmpty(new HashMap<String, String>() { { put("", ""); } }));
263+
}
247264
/*
248265
_.isObject({});
249266
=> true

0 commit comments

Comments
 (0)