Skip to content

Commit 761a43e

Browse files
committed
Add chain and object methods support for the size() and isEmpty().
1 parent d449f6a commit 761a43e

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,10 @@ public static int size(final Iterable<?> iterable) {
635635
return size;
636636
}
637637

638+
public int size() {
639+
return size(iterable);
640+
}
641+
638642
public static <E> List<List<E>> partition(final Iterable<E> iterable, final Predicate<E> pred) {
639643
final List<E> retVal1 = newArrayList();
640644
final List<E> retVal2 = newArrayList();
@@ -1438,6 +1442,10 @@ public static <T> boolean isEmpty(final Iterable<T> iterable) {
14381442
return iterable == null || size(iterable) == 0;
14391443
}
14401444

1445+
public boolean isEmpty() {
1446+
return iterable == null || size(iterable) == 0;
1447+
}
1448+
14411449
public static boolean isArray(final Object object) {
14421450
return object != null && object.getClass().isArray();
14431451
}
@@ -1776,6 +1784,14 @@ public <K, V> Chain<Map<K, V>> toMap() {
17761784
return new Chain<Map<K, V>>($.toMap((Iterable<Map.Entry<K, V>>) list));
17771785
}
17781786

1787+
public boolean isEmpty() {
1788+
return $.isEmpty(list);
1789+
}
1790+
1791+
public int size() {
1792+
return $.size(list);
1793+
}
1794+
17791795
public T item() {
17801796
return item;
17811797
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,10 @@ public void toMap() {
11401140
public void size() {
11411141
final int result = $.size(asList(1, 2, 3, 4));
11421142
assertEquals(4, result);
1143+
final int resultObj = new $(asList(1, 2, 3, 4)).size();
1144+
assertEquals(4, resultObj);
1145+
final int resultChain = $.chain(asList(1, 2, 3, 4)).size();
1146+
assertEquals(4, resultChain);
11431147
}
11441148

11451149
/*

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ public void isEqual() {
227227
public void isEmpty() {
228228
assertTrue($.isEmpty((List) null));
229229
assertTrue($.isEmpty(new ArrayList<String>()));
230+
assertTrue(new $((List) null).isEmpty());
231+
assertTrue(new $(new ArrayList<String>()).isEmpty());
232+
assertTrue($.chain((List) null).isEmpty());
233+
assertTrue($.chain(new ArrayList<String>()).isEmpty());
230234
assertFalse($.isEmpty(asList("")));
231235
assertTrue($.isEmpty((Map) null));
232236
assertTrue($.isEmpty(new HashMap<String, String>()));

0 commit comments

Comments
 (0)