Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/io/reactivex/rxjava4/core/Streamable.java
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,16 @@ default T blockingLast(@NonNull T defaultItem, @NonNull StreamerCancellation can
return StreamableBlocking.blockingLast(this, defaultItem, cancellation);
}

/**
* Counts all upstream items and emits the count as a {@code Single}.
* @return the new {@code Single} instance
*/
@CheckReturnValue
@NonNull
default Single<Long> count() {
return collect(Collectors.counting()).lastOrError();
}

/**
* Collects all upstream values via the use of a {@link Collector} configuration
* and emits its resulting value as a single item of the returned {@code Streamable}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

public record StreamableIntervalRange(
long start,
long count,
long itemCount,
long initialDelay,
long period,
TimeUnit unit,
Expand All @@ -41,7 +41,7 @@ public record StreamableIntervalRange(

@Override
public @NonNull Streamer<Long> stream(@NonNull StreamerCancellation cancellation) {
var streamer = new IntervalStreamer(start, start + count);
var streamer = new IntervalStreamer(start, start + itemCount);
if (scheduler != null) {
var d = scheduler.schedulePeriodicallyDirect(streamer, initialDelay, period, unit);
DisposableHelper.setOnce(streamer, d);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import io.reactivex.rxjava4.disposables.StreamerCancellation;
import io.reactivex.rxjava4.operators.*;

public record StreamableRange(int start, int count) implements IsSynchronousStreamable<Integer> {
public record StreamableRange(int start, int itemCount) implements IsSynchronousStreamable<Integer> {

@Override
public @NonNull Streamer<@NonNull Integer> stream(@NonNull StreamerCancellation cancellation) {
return new RangeStreamer<>(start, start + count);
return new RangeStreamer<>(start, start + itemCount);
}

static final class RangeStreamer<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import io.reactivex.rxjava4.disposables.StreamerCancellation;
import io.reactivex.rxjava4.operators.*;

public record StreamableRangeLong(long start, long count) implements IsSynchronousStreamable<Long> {
public record StreamableRangeLong(long start, long itemCount) implements IsSynchronousStreamable<Long> {

@Override
public @NonNull Streamer<@NonNull Long> stream(@NonNull StreamerCancellation cancellation) {
return new RangeLongStreamer<>(start, start + count);
return new RangeLongStreamer<>(start, start + itemCount);
}

static final class RangeLongStreamer<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@
import io.reactivex.rxjava4.disposables.StreamerCancellation;
import io.reactivex.rxjava4.operators.*;

public record StreamableSkip<T>(Streamable<T> source, long count) implements Streamable<T> {
public record StreamableSkip<T>(Streamable<T> source, long itemCount) implements Streamable<T> {

@SuppressWarnings("unchecked")
@Override
public @NonNull Streamer<@NonNull T> stream(@NonNull StreamerCancellation cancellation) {
var upstream = source.stream(cancellation);
if (upstream instanceof IndexableSource<?> isrc) {
return new SkipStreamerIndexed<>(upstream, (IndexableSource<T>)isrc, count);
return new SkipStreamerIndexed<>(upstream, (IndexableSource<T>)isrc, itemCount);
} else
if (upstream instanceof DeferredEnumerableSource<?> dsrc) {
return new SkipStreamerDeferredEnumerable<>(upstream, (DeferredEnumerableSource<T>)dsrc, count);
return new SkipStreamerDeferredEnumerable<>(upstream, (DeferredEnumerableSource<T>)dsrc, itemCount);
} else
if (upstream instanceof EnumerableSource<?> esrc) {
return new SkipStreamerEnumerable<>(upstream, (EnumerableSource<T>)esrc, count);
return new SkipStreamerEnumerable<>(upstream, (EnumerableSource<T>)esrc, itemCount);
}
return new SkipStreamerBasic<>(upstream, count);
return new SkipStreamerBasic<>(upstream, itemCount);
}

static abstract class SkipStreamer<T> extends AtomicInteger implements Streamer<T>, BiConsumer<Boolean, Throwable> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@
import io.reactivex.rxjava4.internal.fuseable.HasUpstreamStreamableSource;
import io.reactivex.rxjava4.operators.*;

public record StreamableTake<T>(Streamable<T> source, long count)
public record StreamableTake<T>(Streamable<T> source, long itemCount)
implements Streamable<T>, HasUpstreamStreamableSource<T> {

@SuppressWarnings("unchecked")
@Override
public @NonNull Streamer<@NonNull T> stream(@NonNull StreamerCancellation cancellation) {
var upstream = source.stream(cancellation);
if (upstream instanceof IndexableSource<?> isrc) {
return new TakeStreamerIndexable<>(upstream, (IndexableSource<T>)isrc, count);
return new TakeStreamerIndexable<>(upstream, (IndexableSource<T>)isrc, itemCount);
} else
if (upstream instanceof DeferredEnumerableSource<?> dsrc) {
return new TakeStreamerDeferredEnumerable<>(upstream, (DeferredEnumerableSource<T>)dsrc, count);
return new TakeStreamerDeferredEnumerable<>(upstream, (DeferredEnumerableSource<T>)dsrc, itemCount);
} else
if (upstream instanceof EnumerableSource<?> esrc) {
return new TakeStreamerEnumerable<>(upstream, (EnumerableSource<T>)esrc, count);
return new TakeStreamerEnumerable<>(upstream, (EnumerableSource<T>)esrc, itemCount);
}
return new TakeStreamerBasic<>(upstream, count);
return new TakeStreamerBasic<>(upstream, itemCount);
}

static abstract class TakeStreamerBase<T> implements Streamer<T> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/

package io.reactivex.rxjava4.internal.operators.streamable;

import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.Test;

import io.reactivex.rxjava4.core.Streamable;
import io.reactivex.rxjava4.exceptions.TestException;

public class StreamableCountTest extends StreamableBaseTest {

@Test
public void empty() throws Throwable {
Streamable.empty()
.count()
.test()
.awaitDone(5, TimeUnit.SECONDS)
.assertResult(0L);
}

@Test
public void multipleItems() throws Throwable {
Streamable.range(1, 5)
.count()
.test()
.awaitDone(5, TimeUnit.SECONDS)
.assertResult(5L);
}

@Test
public void error() throws Throwable {
Streamable.error(new TestException("error"))
.count()
.test()
.awaitDone(5, TimeUnit.SECONDS)
.assertFailure(TestException.class);
}
}
Loading