Skip to content

Commit 0f3b487

Browse files
committed
- add contains #25
1 parent 6c13594 commit 0f3b487

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

src/main/java/br/com/zbra/androidlinq/AbstractStream.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,26 @@ public boolean any(Predicate<T> predicate) {
250250
return where(predicate).any();
251251
}
252252

253+
@Override
254+
public boolean contains(final T element) {
255+
return contains(element, new EqualityComparator<T>() {
256+
@Override
257+
public boolean compare(T value1, T value2) {
258+
return value1.equals(value2);
259+
}
260+
});
261+
}
262+
263+
@Override
264+
public boolean contains(final T element, final EqualityComparator<T> comparator) {
265+
return any(new Predicate<T>() {
266+
@Override
267+
public boolean apply(T value) {
268+
return comparator.compare(value, element);
269+
}
270+
});
271+
}
272+
253273
@Override
254274
public int count() {
255275
return aggregate(0, new Aggregator<T, Integer>() {

src/main/java/br/com/zbra/androidlinq/Stream.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,23 @@ interface Stream<T> extends Iterable<T> {
336336
*/
337337
boolean any(Predicate<T> predicate);
338338

339+
/**
340+
* Determines whether a sequence contains a specific element.
341+
*
342+
* @param element element for checking
343+
* @return {@code true} if the source sequence contains the given element; otherwise, {@code false}.
344+
*/
345+
boolean contains(T element);
346+
347+
/**
348+
* Determines whether a sequence contains a specific element.
349+
*
350+
* @param element element for checking
351+
* @param comparator expression defining how to compare elements
352+
* @return {@code true} if the source sequence contains the given element; otherwise, {@code false}.
353+
*/
354+
boolean contains(T element, EqualityComparator<T> comparator);
355+
339356
/**
340357
* Returns the number of elements in a sequence.
341358
*
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package br.com.zbra.androidlinq.delegate;
2+
3+
public interface EqualityComparator<T> {
4+
boolean compare(T value1, T value2);
5+
}

src/test/groovy/br/com/zbra/androidlinq/StreamTest.groovy

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,22 @@ class StreamTest extends GroovyTestCase {
399399
assert stream(integers).any({ n -> n == 100 } as Predicate) == false
400400
}
401401

402+
@SuppressWarnings("GroovyPointlessBoolean")
403+
void testContains() {
404+
def integers = 0..9
405+
406+
// contains passing element
407+
assert stream(new int[0]).contains(1) == false
408+
assert stream(integers).contains(99) == false
409+
assert stream(integers).contains(7) == true
410+
411+
// contains passing element and compare function
412+
assert stream([]).contains(9, { n1, n2 -> n1 == n2 }) == false
413+
assert stream([1]).contains(1, { n1, n2 -> n1 == n2 }) == true
414+
assert stream(integers).contains(9, { n1, n2 -> n1 == n2 }) == true
415+
assert stream(integers).contains(100, { n1, n2 -> n1 == n2 }) == false
416+
}
417+
402418
void testCount() {
403419
assert stream([]).count() == 0
404420
assert stream(0..9).count() == 10

0 commit comments

Comments
 (0)