Skip to content

Commit ef72e29

Browse files
committed
Add object methods support every(), all(), some() and any().
1 parent 7decdab commit ef72e29

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,18 +334,34 @@ public Boolean apply(E arg) {
334334
}).isPresent();
335335
}
336336

337+
public boolean every(final Predicate<T> pred) {
338+
return every(iterable, pred);
339+
}
340+
337341
public static <E> boolean all(final Iterable<E> iterable, final Predicate<E> pred) {
338342
return every(iterable, pred);
339343
}
340344

345+
public boolean all(final Predicate<T> pred) {
346+
return every(iterable, pred);
347+
}
348+
341349
public static <E> boolean some(final Iterable<E> iterable, final Predicate<E> pred) {
342350
return find(iterable, pred).isPresent();
343351
}
344352

353+
public boolean some(final Predicate<T> pred) {
354+
return some(iterable, pred);
355+
}
356+
345357
public static <E> boolean any(final Iterable<E> iterable, final Predicate<E> pred) {
346358
return some(iterable, pred);
347359
}
348360

361+
public boolean any(final Predicate<T> pred) {
362+
return some(iterable, pred);
363+
}
364+
349365
public static <E> boolean contains(final Iterable<E> iterable, final E elem) {
350366
return some(iterable, new Predicate<E>() {
351367
@Override

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

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,26 +440,80 @@ public Boolean apply(Integer item) {
440440
assertEquals("[1, 3, 5]", resultSet.toString());
441441
}
442442

443+
/*
444+
_.every([1, 2, 3, 4], function(num) { return num % 2 === 0; }); // false
445+
_.every([1, 2, 3, 4], function(num) { return num < 5; }); // true
446+
*/
447+
@Test
448+
public void every() {
449+
final boolean result1 = $.every(asList(1, 2, 3, 4),
450+
new Predicate<Integer>() {
451+
public Boolean apply(Integer item) {
452+
return item % 2 == 0;
453+
}
454+
});
455+
final boolean result1obj = new $(asList(1, 2, 3, 4))
456+
.every(
457+
new Predicate<Integer>() {
458+
public Boolean apply(Integer item) {
459+
return item % 2 == 0;
460+
}
461+
});
462+
final boolean result2 = $.every(asList(1, 2, 3, 4),
463+
new Predicate<Integer>() {
464+
public Boolean apply(Integer item) {
465+
return item < 5;
466+
}
467+
});
468+
final boolean result2obj = new $(asList(1, 2, 3, 4))
469+
.every(
470+
new Predicate<Integer>() {
471+
public Boolean apply(Integer item) {
472+
return item < 5;
473+
}
474+
});
475+
assertFalse(result1);
476+
assertFalse(result1obj);
477+
assertTrue(result2);
478+
assertTrue(result2obj);
479+
}
480+
443481
/*
444482
_.all([1, 2, 3, 4], function(num) { return num % 2 === 0; }); // false
445483
_.all([1, 2, 3, 4], function(num) { return num < 5; }); // true
446484
*/
447485
@Test
448486
public void all() {
449-
final Boolean result1 = $.all(asList(1, 2, 3, 4),
487+
final boolean result1 = $.all(asList(1, 2, 3, 4),
488+
new Predicate<Integer>() {
489+
public Boolean apply(Integer item) {
490+
return item % 2 == 0;
491+
}
492+
});
493+
final boolean result1obj = new $(asList(1, 2, 3, 4))
494+
.all(
450495
new Predicate<Integer>() {
451496
public Boolean apply(Integer item) {
452497
return item % 2 == 0;
453498
}
454499
});
455-
final Boolean result2 = $.all(asList(1, 2, 3, 4),
500+
final boolean result2 = $.all(asList(1, 2, 3, 4),
501+
new Predicate<Integer>() {
502+
public Boolean apply(Integer item) {
503+
return item < 5;
504+
}
505+
});
506+
final boolean result2obj = new $(asList(1, 2, 3, 4))
507+
.all(
456508
new Predicate<Integer>() {
457509
public Boolean apply(Integer item) {
458510
return item < 5;
459511
}
460512
});
461513
assertFalse(result1);
514+
assertFalse(result1obj);
462515
assertTrue(result2);
516+
assertTrue(result2obj);
463517
}
464518

465519
/*
@@ -468,20 +522,36 @@ public Boolean apply(Integer item) {
468522
*/
469523
@Test
470524
public void any() {
471-
final Boolean result1 = $.any(asList(1, 2, 3, 4),
525+
final boolean result1 = $.any(asList(1, 2, 3, 4),
526+
new Predicate<Integer>() {
527+
public Boolean apply(Integer item) {
528+
return item % 2 == 0;
529+
}
530+
});
531+
final boolean result1obj = new $(asList(1, 2, 3, 4))
532+
.any(
472533
new Predicate<Integer>() {
473534
public Boolean apply(Integer item) {
474535
return item % 2 == 0;
475536
}
476537
});
477-
final Boolean result2 = $.any(asList(1, 2, 3, 4),
538+
final boolean result2 = $.any(asList(1, 2, 3, 4),
539+
new Predicate<Integer>() {
540+
public Boolean apply(Integer item) {
541+
return item == 5;
542+
}
543+
});
544+
final boolean result2obj = new $(asList(1, 2, 3, 4))
545+
.any(
478546
new Predicate<Integer>() {
479547
public Boolean apply(Integer item) {
480548
return item == 5;
481549
}
482550
});
483551
assertTrue(result1);
552+
assertTrue(result1obj);
484553
assertFalse(result2);
554+
assertFalse(result2obj);
485555
}
486556

487557
/*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public void once() throws Exception {
146146
Function<Void> onceIncr = $.once(incr);
147147
onceIncr.apply();
148148
onceIncr.apply();
149-
Thread.sleep(48);
149+
Thread.sleep(60);
150150
assertEquals("incr was called only once", 1, counter[0]);
151151
}
152152

0 commit comments

Comments
 (0)