Skip to content

Commit 06b8c7a

Browse files
committed
Add support for the chain methods every() and some().
1 parent f79c7c3 commit 06b8c7a

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,21 +1664,11 @@ public <F> Chain<F> reduceRight(final FunctionAccum<F, T> func, final F zeroElem
16641664
}
16651665

16661666
public Chain<Optional<T>> find(final Predicate<T> pred) {
1667-
for (final T element : list) {
1668-
if (pred.apply(element)) {
1669-
return new Chain<Optional<T>>(Optional.of(element));
1670-
}
1671-
}
1672-
return new Chain<Optional<T>>(Optional.<T>absent());
1667+
return new Chain<Optional<T>>($.find(list, pred));
16731668
}
16741669

16751670
public Chain<Optional<T>> findLast(final Predicate<T> pred) {
1676-
for (int index = list.size() - 1; index >= 0; index--) {
1677-
if (pred.apply(list.get(index))) {
1678-
return new Chain<Optional<T>>(Optional.of(list.get(index)));
1679-
}
1680-
}
1681-
return new Chain<Optional<T>>(Optional.<T>absent());
1671+
return new Chain<Optional<T>>($.findLast(list, pred));
16821672
}
16831673

16841674
public Chain<Comparable> max() {
@@ -1738,6 +1728,14 @@ public Chain<T> tap(final Block<T> func) {
17381728
return new Chain<T>(list);
17391729
}
17401730

1731+
public Chain<Boolean> every(final Predicate<T> pred) {
1732+
return new Chain<Boolean>($.every(list, pred));
1733+
}
1734+
1735+
public Chain<Boolean> some(final Predicate<T> pred) {
1736+
return new Chain<Boolean>($.some(list, pred));
1737+
}
1738+
17411739
public Chain<Boolean> contains(final T elem) {
17421740
return new Chain<Boolean>($.contains(list, elem));
17431741
}
@@ -1750,8 +1748,8 @@ public <F> Chain<F> uniq(final Function1<T, F> func) {
17501748
return new Chain<F>($.newArrayList((Iterable<F>) $.uniq(list, func)));
17511749
}
17521750

1753-
public <T> Chain<T> concat(final List<T> second) {
1754-
return new Chain<T>((List<T>) Arrays.asList($.concat(list.toArray(), second.toArray())));
1751+
public Chain<T> concat(final List<T> second) {
1752+
return new Chain<T>($.concat(list, second));
17551753
}
17561754

17571755
public <T> Chain<T> slice(final int start) {

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,13 @@ public Boolean apply(Integer item) {
459459
return item % 2 == 0;
460460
}
461461
});
462+
final boolean result1chain = (Boolean) $.chain(asList(1, 2, 3, 4))
463+
.every(
464+
new Predicate<Integer>() {
465+
public Boolean apply(Integer item) {
466+
return item % 2 == 0;
467+
}
468+
}).item();
462469
final boolean result2 = $.every(asList(1, 2, 3, 4),
463470
new Predicate<Integer>() {
464471
public Boolean apply(Integer item) {
@@ -472,10 +479,19 @@ public Boolean apply(Integer item) {
472479
return item < 5;
473480
}
474481
});
482+
final boolean result2chain = (Boolean) $.chain(asList(1, 2, 3, 4))
483+
.every(
484+
new Predicate<Integer>() {
485+
public Boolean apply(Integer item) {
486+
return item < 5;
487+
}
488+
}).item();
475489
assertFalse(result1);
476490
assertFalse(result1obj);
491+
assertFalse(result1chain);
477492
assertTrue(result2);
478493
assertTrue(result2obj);
494+
assertTrue(result2chain);
479495
}
480496

481497
/*
@@ -573,6 +589,13 @@ public Boolean apply(Integer item) {
573589
return item % 2 == 0;
574590
}
575591
});
592+
final boolean result1chain = (Boolean) $.chain(asList(1, 2, 3, 4))
593+
.some(
594+
new Predicate<Integer>() {
595+
public Boolean apply(Integer item) {
596+
return item % 2 == 0;
597+
}
598+
}).item();
576599
final boolean result2 = $.some(asList(1, 2, 3, 4),
577600
new Predicate<Integer>() {
578601
public Boolean apply(Integer item) {
@@ -586,10 +609,19 @@ public Boolean apply(Integer item) {
586609
return item == 5;
587610
}
588611
});
612+
final boolean result2chain = (Boolean) $.chain(asList(1, 2, 3, 4))
613+
.some(
614+
new Predicate<Integer>() {
615+
public Boolean apply(Integer item) {
616+
return item == 5;
617+
}
618+
}).item();
589619
assertTrue(result1);
590620
assertTrue(result1obj);
621+
assertTrue(result1chain);
591622
assertFalse(result2);
592623
assertFalse(result2obj);
624+
assertFalse(result2chain);
593625
}
594626

595627
/*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public Void apply() {
113113
assertEquals("incr was debounced", 1, counter[0]);
114114
return null;
115115
}
116-
}, 48);
116+
}, 60);
117117
Thread.sleep(120);
118118
}
119119

0 commit comments

Comments
 (0)