Skip to content

Commit 479ad14

Browse files
committed
Add U.count(iterable, predicate).
1 parent 5e25088 commit 479ad14

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,20 @@ public boolean any(final Predicate<T> pred) {
552552
return some(iterable, pred);
553553
}
554554

555+
public static <E> int count(final Iterable<E> iterable, final Predicate<E> pred) {
556+
int result = 0;
557+
for (E item : iterable) {
558+
if (pred.test(item)) {
559+
result += 1;
560+
}
561+
}
562+
return result;
563+
}
564+
565+
public int count(final Predicate<T> pred) {
566+
return count(iterable, pred);
567+
}
568+
555569
public static <E> boolean contains(final Iterable<E> iterable, final E elem) {
556570
return some(iterable, new Predicate<E>() {
557571
@Override
@@ -2324,6 +2338,10 @@ public Chain<Boolean> some(final Predicate<T> pred) {
23242338
return new Chain<Boolean>(U.some(list, pred));
23252339
}
23262340

2341+
public Chain<Integer> count(final Predicate<T> pred) {
2342+
return new Chain<Integer>(U.count(list, pred));
2343+
}
2344+
23272345
public Chain<Boolean> contains(final T elem) {
23282346
return new Chain<Boolean>(U.contains(list, elem));
23292347
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ public Chain<Boolean> some(final Predicate<T> pred) {
299299
return new Chain<Boolean>(U.some(value(), pred));
300300
}
301301

302+
public Chain<Integer> count(final Predicate<T> pred) {
303+
return new Chain<Integer>(U.count(value(), pred));
304+
}
305+
302306
public Chain<Boolean> contains(final T elem) {
303307
return new Chain<Boolean>(U.contains(value(), elem));
304308
}

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,61 @@ public void include() {
950950
assertTrue(result);
951951
}
952952

953+
/*
954+
_.count([1, 2, 3, 4], function(num) { return num % 2 === 0; }); // 2
955+
_.count([1, 2, 3, 4], function(num) { return num < 5; }); // 4
956+
*/
957+
@Test
958+
@SuppressWarnings("unchecked")
959+
public void count() {
960+
final int result1 = U.count(asList(1, 2, 3, 4),
961+
new Predicate<Integer>() {
962+
public boolean test(Integer item) {
963+
return item % 2 == 0;
964+
}
965+
});
966+
final int result1obj = new U(asList(1, 2, 3, 4))
967+
.count(
968+
new Predicate<Integer>() {
969+
public boolean test(Integer item) {
970+
return item % 2 == 0;
971+
}
972+
});
973+
final int result1chain = U.chain(asList(1, 2, 3, 4))
974+
.count(
975+
new Predicate<Integer>() {
976+
public boolean test(Integer item) {
977+
return item % 2 == 0;
978+
}
979+
}).item();
980+
final int result2 = U.count(asList(1, 2, 3, 4),
981+
new Predicate<Integer>() {
982+
public boolean test(Integer item) {
983+
return item < 5;
984+
}
985+
});
986+
final int result2obj = new U(asList(1, 2, 3, 4))
987+
.count(
988+
new Predicate<Integer>() {
989+
public boolean test(Integer item) {
990+
return item < 5;
991+
}
992+
});
993+
final int result2chain = U.chain(asList(1, 2, 3, 4))
994+
.count(
995+
new Predicate<Integer>() {
996+
public boolean test(Integer item) {
997+
return item < 5;
998+
}
999+
}).item();
1000+
assertEquals(2, result1);
1001+
assertEquals(2, result1obj);
1002+
assertEquals(2, result1chain);
1003+
assertEquals(4, result2);
1004+
assertEquals(4, result2obj);
1005+
assertEquals(4, result2chain);
1006+
}
1007+
9531008
/*
9541009
_.contains([1, 2, 3], 3);
9551010
=> true

src/test/java/com/github/underscore/lodash/LodashTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,8 @@ public void accept(String str) {
752752
public boolean test(String str) { return true; } });
753753
U.chain(new String[] {""}).some(new Predicate<String>() {
754754
public boolean test(String str) { return true; } });
755+
U.chain(new String[] {""}).count(new Predicate<String>() {
756+
public boolean test(String str) { return true; } });
755757
U.chain(new String[] {""}).contains("");
756758
U.chain(new String[] {""}).invoke("toString", Collections.emptyList());
757759
U.chain(new String[] {""}).invoke("toString");

0 commit comments

Comments
 (0)