hu.akarnokd.rxjava2.operators.FlowableBufferPredicate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava2-extensions Show documentation
Show all versions of rxjava2-extensions Show documentation
rxjava2-extensions developed by David Karnok
/*
* Copyright 2016-2017 David Karnok
*
* 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 hu.akarnokd.rxjava2.operators;
import java.util.Collection;
import java.util.concurrent.Callable;
import org.reactivestreams.*;
import io.reactivex.*;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.Predicate;
import io.reactivex.internal.functions.ObjectHelper;
import io.reactivex.internal.fuseable.ConditionalSubscriber;
import io.reactivex.internal.subscriptions.*;
import io.reactivex.plugins.RxJavaPlugins;
/**
* Buffer into the same buffer while the predicate returns true or
* buffer into the same buffer until predicate returns true.
*
* @param the source value type
* @param the buffer type
*
* @since 0.8.0
*/
final class FlowableBufferPredicate> extends Flowable implements FlowableTransformer {
enum Mode {
/** The item triggering the new buffer will be part of the new buffer. */
BEFORE,
/** The item triggering the new buffer will be part of the old buffer. */
AFTER,
/** The item won't be part of any buffers. */
SPLIT
}
final Publisher source;
final Predicate super T> predicate;
final Mode mode;
final Callable bufferSupplier;
FlowableBufferPredicate(Publisher source, Predicate super T> predicate, Mode mode,
Callable bufferSupplier) {
this.source = source;
this.predicate = predicate;
this.mode = mode;
this.bufferSupplier = bufferSupplier;
}
@Override
protected void subscribeActual(Subscriber super C> s) {
C buffer;
try {
buffer = ObjectHelper.requireNonNull(bufferSupplier.call(), "The bufferSupplier returned a null buffer");
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
EmptySubscription.error(ex, s);
return;
}
source.subscribe(new BufferPredicateSubscriber(s, buffer, predicate, mode, bufferSupplier));
}
@Override
public Publisher apply(Flowable upstream) {
return new FlowableBufferPredicate(upstream, predicate, mode, bufferSupplier);
}
static final class BufferPredicateSubscriber>
implements ConditionalSubscriber, Subscription {
final Subscriber super C> actual;
final Predicate super T> predicate;
final Mode mode;
final Callable bufferSupplier;
C buffer;
Subscription s;
int count;
BufferPredicateSubscriber(Subscriber super C> actual,
C buffer,
Predicate super T> predicate, Mode mode,
Callable bufferSupplier) {
this.actual = actual;
this.predicate = predicate;
this.mode = mode;
this.buffer = buffer;
this.bufferSupplier = bufferSupplier;
}
@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.validate(this.s, s)) {
this.s = s;
actual.onSubscribe(this);
}
}
@Override
public void onNext(T t) {
if (!tryOnNext(t)) {
s.request(1);
}
}
@Override
public boolean tryOnNext(T t) {
C buf = buffer;
if (buf != null) {
boolean b;
try {
b = predicate.test(t);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
s.cancel();
buffer = null;
actual.onError(ex);
return true;
}
switch (mode) {
case AFTER: {
buf.add(t);
if (b) {
actual.onNext(buf);
try {
buffer = bufferSupplier.call();
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
s.cancel();
onError(ex);
return true;
}
count = 0;
} else {
count++;
return false;
}
break;
}
case BEFORE: {
if (b) {
buf.add(t);
count++;
return false;
} else {
actual.onNext(buf);
try {
buf = bufferSupplier.call();
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
s.cancel();
onError(ex);
return true;
}
buf.add(t);
buffer = buf;
count = 1;
}
break;
}
default:
if (b) {
actual.onNext(buf);
try {
buffer = bufferSupplier.call();
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
s.cancel();
onError(ex);
return true;
}
count = 0;
} else {
buf.add(t);
count++;
return false;
}
}
}
return true;
}
@Override
public void onError(Throwable t) {
if (buffer != null) {
buffer = null;
actual.onError(t);
} else {
RxJavaPlugins.onError(t);
}
}
@Override
public void onComplete() {
C b = buffer;
if (b != null) {
buffer = null;
if (count != 0) {
actual.onNext(b);
}
actual.onComplete();
}
}
@Override
public void request(long n) {
s.request(n);
}
@Override
public void cancel() {
s.cancel();
}
}
}