Java Code Examples for java.util.stream.Stream#count()

The following examples show how to use java.util.stream.Stream#count() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GroupingOnSplittingSpliteratorTest.java    From streams-utils with Apache License 2.0 6 votes vote down vote up
@Test
public void should_conform_to_specified_trySplit_behavior() {
    // Given
    Stream<String> strings = Stream.of("o", "1", "2", "3", "4", "5", "6", "7", "8", "9", "c");
    Predicate<String> splitter = s -> s.startsWith("o");

    Stream<Stream<String>> testedStream = StreamsUtils.group(strings, splitter);
    TryAdvanceCheckingSpliterator<Stream<String>> spliterator = new TryAdvanceCheckingSpliterator<>(testedStream.spliterator());
    Stream<String> monitoredStream = StreamSupport.stream(spliterator, false).flatMap(Function.identity());

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(11L);
}
 
Example 2
Source File: ShiftingWindowCollectTest.java    From streams-utils with Apache License 2.0 6 votes vote down vote up
@Test
public void should_conform_to_specified_trySplit_behavior() {
    // Given
    Stream<String> strings = Stream.of("2", "4", "2", "4", "2", "4", "2");
    int groupingFactor = 3;

    Stream<String> testedStream = StreamsUtils.shiftingWindowCollect(strings, groupingFactor, joining());
    TryAdvanceCheckingSpliterator<String> spliterator = new TryAdvanceCheckingSpliterator<>(testedStream.spliterator());
    Stream<String> monitoredStream = StreamSupport.stream(spliterator, false);

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(5L);
}
 
Example 3
Source File: ZippingSpliteratorTest.java    From streams-utils with Apache License 2.0 6 votes vote down vote up
@Test
public void should_conform_to_specified_trySplit_behavior() {
    // Given
    Stream<String> strings = Stream.of("one", "two", "three");
    Stream<Integer> ints = Stream.of(1, 2, 3);
    BiFunction<String, Integer, String> zip = (s, i) -> s + " - " + i;

    Stream<String> zippedStream = StreamsUtils.zip(strings, ints, zip);
    TryAdvanceCheckingSpliterator<String> spliterator = new TryAdvanceCheckingSpliterator<>(zippedStream.spliterator());
    Stream<String> monitoredStream = StreamSupport.stream(spliterator, false);

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(3L);
}
 
Example 4
Source File: GroupingOnGatingSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void should_correctly_count_the_elements_of_a_sized_stream() {
    // Given
    Stream<String> strings = Stream.of("o", "1", "2", "3", "4", "5", "6", "7", "8", "9", "c");
    Predicate<String> open = s -> s.startsWith("o");
    Predicate<String> close = s -> s.startsWith("c");

    Stream<Stream<String>> stream = StreamsUtils.group(strings, open, close);

    // When
    long count = stream.count();

    // Then
    assertThat(count).isEqualTo(1L);
}
 
Example 5
Source File: GroupingOnSplittingSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void should_correctly_count_the_elements_of_a_sized_stream() {
    // Given
    Stream<String> strings = Stream.of("o", "1", "2", "3", "4", "5", "6", "7", "8", "9", "c");
    Predicate<String> splitter = s -> s.startsWith("o");

    Stream<Stream<String>> stream = StreamsUtils.group(strings, splitter);

    // When
    long count = stream.count();

    // Then
    assertThat(count).isEqualTo(1L);
}
 
Example 6
Source File: CrossProductNaturalyOrderedSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void should_cross_an_empty_stream_into_an_empty_stream() {
    // Given
    Stream<String> strings = Stream.empty();

    // When
    Stream<Map.Entry<String, String>> stream = StreamsUtils.crossProductNaturallyOrdered(strings);
    long count = stream.count();

    // Then
    assertThat(count).isEqualTo(0L);
}
 
Example 7
Source File: CrossProductSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void should_correctly_count_the_elements_of_a_sized_stream() {
    // Given
    Stream<String> strings = Stream.of("a", "d", "c", "b");
    Stream<Map.Entry<String, String>> stream = StreamsUtils.crossProduct(strings);

    // When
    long count = stream.count();

    // Then
    assertThat(count).isEqualTo(16L);
}
 
Example 8
Source File: GroupingOnSplittingNonIncludedSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void should_correctly_count_the_elements_of_a_sized_stream() {
    // Given
    Stream<String> strings = Stream.of("o", "1", "2", "3", "4", "5", "6", "7", "8", "9", "c");
    Predicate<String> splitter = s -> s.startsWith("o");

    Stream<Stream<String>> stream = StreamsUtils.group(strings, splitter, false);

    // When
    long count = stream.count();

    // Then
    assertThat(count).isEqualTo(1L);
}
 
Example 9
Source File: CrossProductNoDoublesSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void should_cross_an_empty_stream_into_an_empty_stream() {
    // Given
    Stream<String> strings = Stream.empty();

    // When
    Stream<Map.Entry<String, String>> stream = StreamsUtils.crossProductNoDoubles(strings);
    long count = stream.count();

    // Then
    assertThat(count).isEqualTo(0L);
}
 
Example 10
Source File: ZippingSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void should_correctly_count_the_elements_of_a_sized_stream_with_different_length() {
    // Given
    Stream<String> strings = Stream.of("one", "two", "three", "four");
    Stream<Integer> ints = Stream.of(1, 2, 3, 4, 5, 6);
    BiFunction<String, Integer, String> zip = (s, i) -> s + " - " + i;
    Stream<String> stream = StreamsUtils.zip(strings, ints, zip);

    // When
    long count = stream.count();

    // Then
    assertThat(count).isEqualTo(4L);
}
 
Example 11
Source File: ZippingSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void should_correctly_count_the_elements_of_a_sized_stream_with_same_length() {
    // Given
    Stream<String> strings = Stream.of("one", "two", "three", "four");
    Stream<Integer> ints = Stream.of(1, 2, 3, 4);
    BiFunction<String, Integer, String> zip = (s, i) -> s + " - " + i;
    Stream<String> stream = StreamsUtils.zip(strings, ints, zip);

    // When
    long count = stream.count();

    // Then
    assertThat(count).isEqualTo(4L);
}
 
Example 12
Source File: TraversingSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void should_correctly_count_the_elements_of_a_sized_stream_for_different_length_streams() {
    // Given
    Stream<String> streamA = Stream.of("a1", "a2", "a3");
    Stream<String> streamB = Stream.of("b1", "b2", "b3", "b4");
    Stream<Stream<String>> stream = StreamsUtils.traverse(streamA, streamB);

    // When
    long count = stream.count();

    // Then
    assertThat(count).isEqualTo(3L);
}
 
Example 13
Source File: ValidatingSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void should_validate_empty_streams_into_an_empty_stream() {
    // Given
    Stream<String> strings = Stream.empty();
    Predicate<String> validator = String::isEmpty;

    // When
    Stream<String> validateStream =
            StreamsUtils.validate(strings, validator, Function.identity(), Function.identity());
    long count = validateStream.count();

    // Then
    assertThat(count).isEqualTo(0);
}
 
Example 14
Source File: BdbWrapperTest.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void getItemFromValueRange() throws Exception {
  final Stream<String> stream = database.databaseGetter()
    .key("ab")
    .skipNearValue("b")
    .onlyValuesMatching((prefix, value) -> {
      return value.startsWith(prefix);
    })
    .forwards()
    .getValues(database.valueRetriever());
  final long count = stream
    .count();
  stream.close();
  assertThat(count, is(2L));
}
 
Example 15
Source File: GroupingSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void should_correctly_count_the_elements_of_a_sized_stream() {
    // Given
    Stream<String> strings = Stream.of("o", "1", "2", "3", "4", "5", "6", "7", "8", "9", "c");
    int groupingFactor = 3;

    Stream<Stream<String>> stream = StreamsUtils.group(strings, groupingFactor);

    // When
    long count = stream.count();

    // Then
    assertThat(count).isEqualTo(3L);
}
 
Example 16
Source File: RollingSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void should_roll_an_empty_stream_into_a_stream_of_an_empty_stream() {
    // Given
    // a trick to create an empty ORDERED stream
    Stream<String> strings = Stream.of("one").filter(s -> s.isEmpty());
    int groupingFactor = 2;

    // When
    Stream<Stream<String>> stream = StreamsUtils.roll(strings, groupingFactor);
    long numberOfRolledStreams = stream.count();

    // Then
    assertThat(numberOfRolledStreams).isEqualTo(1);
}
 
Example 17
Source File: CyclingSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void should_conform_to_specified_trySplit_behavior() {
    // Given
    Stream<String> strings = Stream.of("one", "two", "three");
    Stream<String> cyclingStream = StreamsUtils.cycle(strings).limit(2);
    TryAdvanceCheckingSpliterator<String> spliterator = new TryAdvanceCheckingSpliterator<>(cyclingStream.spliterator());
    Stream<String> monitoredStream = StreamSupport.stream(spliterator, false);

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(2L);
}
 
Example 18
Source File: VaultQueryEngine.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@Override
public long count(@Nullable VaultQuery vaultQuery, String keyspace) {

	Stream<String> stream = getRequiredAdapter().doList(keyspace).stream();

	if (vaultQuery != null) {
		stream = stream.filter(vaultQuery::test);
	}

	return stream.count();
}
 
Example 19
Source File: GroupingOnGatingSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void should_group_an_empty_stream_into_an_empty_stream() {
    // Given
    // a trick to create an empty ORDERED stream
    Stream<String> strings = Stream.of("one").filter(s -> s.isEmpty());
    Predicate<String> open = s -> s.startsWith("o");
    Predicate<String> close = s -> s.startsWith("c");

    // When
    Stream<Stream<String>> groupingOnGatingStream = StreamsUtils.group(strings, open, close);
    long numberOfGroupedSteams = groupingOnGatingStream.count();

    // Then
    assertThat(numberOfGroupedSteams).isEqualTo(0);
}
 
Example 20
Source File: ShiftingWindowSummarizingIntTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@Test
public void should_summarize_an_empty_stream_into_a_stream_of_an_empty_stream() {
    // Given
    // a trick to create an empty ORDERED stream
    Stream<Integer> ints = Stream.of(1, 2, 3).filter(i -> i == 0);
    int groupingFactor = 3;

    // When
    Stream<IntSummaryStatistics> stream = StreamsUtils.shiftingWindowSummarizingInt(ints, groupingFactor, Integer::valueOf);
    long numberOfRolledStreams = stream.count();

    // Then
    assertThat(numberOfRolledStreams).isEqualTo(1);
}