Skip to content

Commit

Permalink
[#260] count(Predicate)
Browse files Browse the repository at this point in the history
  • Loading branch information
amaembo committed Sep 24, 2022
1 parent 87535e8 commit 38b99cb
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 16 deletions.
17 changes: 17 additions & 0 deletions src/main/java/one/util/streamex/AbstractStreamEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,23 @@ public long count() {
return stream().count();
}

/**
* Counts the number of elements in the stream that satisfy the predicate.
*
* <p>
* This is a terminal operation.
*
* @param predicate a <a
* href="package-summary.html#NonInterference">non-interfering </a>,
* <a href="package-summary.html#Statelessness">stateless</a>
* predicate to apply to stream elements. Only elements passing
* the predicate will be counted.
* @return the count of elements in this stream satisfying the predicate.
*/
public long count(Predicate<? super T> predicate) {
return filter(predicate).count();
}

@Override
public boolean anyMatch(Predicate<? super T> predicate) {
if (context.fjp != null)
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/one/util/streamex/DoubleStreamEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,23 @@ public long count() {
return stream().count();
}

/**
* Counts the number of elements in the stream that satisfy the predicate.
*
* <p>
* This is a terminal operation.
*
* @param predicate a <a
* href="package-summary.html#NonInterference">non-interfering </a>,
* <a href="package-summary.html#Statelessness">stateless</a>
* predicate to apply to stream elements. Only elements passing
* the predicate will be counted.
* @return the count of elements in this stream satisfying the predicate.
*/
public long count(DoublePredicate predicate) {
return filter(predicate).count();
}

@Override
public OptionalDouble average() {
if (context.fjp != null)
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/one/util/streamex/IntStreamEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,23 @@ public long count() {
return stream().count();
}

/**
* Counts the number of elements in the stream that satisfy the predicate.
*
* <p>
* This is a terminal operation.
*
* @param predicate a <a
* href="package-summary.html#NonInterference">non-interfering </a>,
* <a href="package-summary.html#Statelessness">stateless</a>
* predicate to apply to stream elements. Only elements passing
* the predicate will be counted.
* @return the count of elements in this stream satisfying the predicate.
*/
public long count(IntPredicate predicate) {
return filter(predicate).count();
}

@Override
public OptionalDouble average() {
if (context.fjp != null)
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/one/util/streamex/LongStreamEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,23 @@ public long count() {
return stream().count();
}

/**
* Counts the number of elements in the stream that satisfy the predicate.
*
* <p>
* This is a terminal operation.
*
* @param predicate a <a
* href="package-summary.html#NonInterference">non-interfering </a>,
* <a href="package-summary.html#Statelessness">stateless</a>
* predicate to apply to stream elements. Only elements passing
* the predicate will be counted.
* @return the count of elements in this stream satisfying the predicate.
*/
public long count(LongPredicate predicate) {
return filter(predicate).count();
}

@Override
public OptionalDouble average() {
if (context.fjp != null)
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/one/util/streamex/api/DoubleStreamExTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -539,4 +539,9 @@ public void testIntersperse() {
.toArray(), 0.0);
assertEquals(0L, IntStreamEx.empty().intersperse(1).count());
}

@Test
public void testCount() {
assertEquals(5L, DoubleStreamEx.of(0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4).count(x -> x == (int) x));
}
}
18 changes: 11 additions & 7 deletions src/test/java/one/util/streamex/api/IntStreamExTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
*/
package one.util.streamex.api;

import one.util.streamex.IntStreamEx;
import one.util.streamex.StreamEx;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -47,13 +53,6 @@
import java.util.stream.IntStream;
import java.util.stream.IntStream.Builder;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import one.util.streamex.IntStreamEx;
import one.util.streamex.StreamEx;

import static one.util.streamex.TestHelpers.checkSpliterator;
import static one.util.streamex.TestHelpers.intStreamEx;
import static one.util.streamex.TestHelpers.streamEx;
Expand Down Expand Up @@ -802,4 +801,9 @@ public void testIntersperse() {
.toArray());
assertEquals(0L, IntStreamEx.empty().intersperse(1).count());
}

@Test
public void testCount() {
assertEquals(5L, IntStreamEx.range(10).count(x -> x % 2 == 0));
}
}
20 changes: 12 additions & 8 deletions src/test/java/one/util/streamex/api/LongStreamExTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
*/
package one.util.streamex.api;

import one.util.streamex.IntStreamEx;
import one.util.streamex.LongStreamEx;
import one.util.streamex.StreamEx;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import java.nio.LongBuffer;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -42,14 +49,6 @@
import java.util.stream.LongStream;
import java.util.stream.LongStream.Builder;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import one.util.streamex.IntStreamEx;
import one.util.streamex.LongStreamEx;
import one.util.streamex.StreamEx;

import static one.util.streamex.TestHelpers.checkSpliterator;
import static one.util.streamex.TestHelpers.longStreamEx;
import static one.util.streamex.TestHelpers.streamEx;
Expand Down Expand Up @@ -633,4 +632,9 @@ public void testIntersperse() {
LongStreamEx.of(1, 10, 100, 1000).intersperse(0).toArray());
assertEquals(0L, IntStreamEx.empty().intersperse(1).count());
}

@Test
public void testCount() {
assertEquals(5L, LongStreamEx.range(10).count(x -> x % 2 == 0));
}
}
6 changes: 5 additions & 1 deletion src/test/java/one/util/streamex/api/StreamExTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import one.util.streamex.MoreCollectors;
import one.util.streamex.StreamEx;
import one.util.streamex.TestHelpers.Point;

import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -2298,4 +2297,9 @@ public void testReduceWithZero() {
assertEquals((Integer) 0, s.get().reduceWithZero(0, 1, (a, b) -> a * b));
});
}

@Test
public void testCount() {
assertEquals(3L, StreamEx.of("x", "", "y", "", "z").count(x -> !x.isEmpty()));
}
}
3 changes: 3 additions & 0 deletions wiki/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Check also [MIGRATION.md](MIGRATION.md) for possible compatibility problems.

### 0.8.2
* [#260] Added: `count(Predicate)`

### 0.8.1
* [#245] Fixed: back to 'jar' packaging
* [#253] Added: Version information in module descriptor
Expand Down

0 comments on commit 38b99cb

Please sign in to comment.