Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch some Observable ops to direct, map fuseable #4124

Merged
merged 1 commit into from
Jun 25, 2016
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
16 changes: 8 additions & 8 deletions src/main/java/io/reactivex/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ public static <T, R> Observable<R> zipIterable(Function<? super Object[], ? exte
@SchedulerSupport(SchedulerSupport.NONE)
public final Observable<Boolean> all(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return lift(new NbpOperatorAll<T>(predicate));
return (new ObservableAll<T>(this, predicate));
}

@SuppressWarnings("unchecked")
Expand All @@ -1112,7 +1112,7 @@ public final Observable<T> ambWith(ObservableConsumable<? extends T> other) {
@SchedulerSupport(SchedulerSupport.NONE)
public final Observable<Boolean> any(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return lift(new NbpOperatorAny<T>(predicate));
return new ObservableAny<T>(this, predicate);
}

@SchedulerSupport(SchedulerSupport.NONE)
Expand Down Expand Up @@ -1149,7 +1149,7 @@ public final <U extends Collection<? super T>> Observable<U> buffer(int count, i
throw new IllegalArgumentException("skip > 0 required but it was " + count);
}
Objects.requireNonNull(bufferSupplier, "bufferSupplier is null");
return lift(new NbpOperatorBuffer<T, U>(count, skip, bufferSupplier));
return new ObservableBuffer<T, U>(this, count, skip, bufferSupplier);
}

@SchedulerSupport(SchedulerSupport.NONE)
Expand Down Expand Up @@ -1182,7 +1182,7 @@ public final <U extends Collection<? super T>> Observable<U> buffer(long timespa
Objects.requireNonNull(unit, "unit is null");
Objects.requireNonNull(scheduler, "scheduler is null");
Objects.requireNonNull(bufferSupplier, "bufferSupplier is null");
return lift(new NbpOperatorBufferTimed<T, U>(timespan, timeskip, unit, scheduler, bufferSupplier, Integer.MAX_VALUE, false));
return new ObservableBufferTimed<T, U>(this, timespan, timeskip, unit, scheduler, bufferSupplier, Integer.MAX_VALUE, false);
}

@SchedulerSupport(SchedulerSupport.COMPUTATION)
Expand Down Expand Up @@ -1217,7 +1217,7 @@ public final <U extends Collection<? super T>> Observable<U> buffer(
if (count <= 0) {
throw new IllegalArgumentException("count > 0 required but it was " + count);
}
return lift(new NbpOperatorBufferTimed<T, U>(timespan, timespan, unit, scheduler, bufferSupplier, count, restartTimerOnMaxSize));
return new ObservableBufferTimed<T, U>(this, timespan, timespan, unit, scheduler, bufferSupplier, count, restartTimerOnMaxSize);
}

@SchedulerSupport(SchedulerSupport.CUSTOM)
Expand Down Expand Up @@ -1250,7 +1250,7 @@ public final <TOpening, TClosing, U extends Collection<? super T>> Observable<U>
Objects.requireNonNull(bufferOpenings, "bufferOpenings is null");
Objects.requireNonNull(bufferClosingSelector, "bufferClosingSelector is null");
Objects.requireNonNull(bufferSupplier, "bufferSupplier is null");
return lift(new NbpOperatorBufferBoundary<T, U, TOpening, TClosing>(bufferOpenings, bufferClosingSelector, bufferSupplier));
return new ObservableBufferBoundary<T, U, TOpening, TClosing>(this, bufferOpenings, bufferClosingSelector, bufferSupplier);
}

@SchedulerSupport(SchedulerSupport.NONE)
Expand Down Expand Up @@ -1280,7 +1280,7 @@ public List<T> get() {
public final <B, U extends Collection<? super T>> Observable<U> buffer(ObservableConsumable<B> boundary, Supplier<U> bufferSupplier) {
Objects.requireNonNull(boundary, "boundary is null");
Objects.requireNonNull(bufferSupplier, "bufferSupplier is null");
return lift(new NbpOperatorBufferExactBoundary<T, U, B>(boundary, bufferSupplier));
return new ObservableBufferExactBoundary<T, U, B>(this, boundary, bufferSupplier);
}

@SchedulerSupport(SchedulerSupport.NONE)
Expand All @@ -1298,7 +1298,7 @@ public List<T> get() {
public final <B, U extends Collection<? super T>> Observable<U> buffer(Supplier<? extends ObservableConsumable<B>> boundarySupplier, Supplier<U> bufferSupplier) {
Objects.requireNonNull(boundarySupplier, "boundarySupplier is null");
Objects.requireNonNull(bufferSupplier, "bufferSupplier is null");
return lift(new NbpOperatorBufferBoundarySupplier<T, U, B>(boundarySupplier, bufferSupplier));
return new ObservableBufferBoundarySupplier<T, U, B>(this, boundarySupplier, bufferSupplier);
}

@SchedulerSupport(SchedulerSupport.NONE)
Expand Down
39 changes: 16 additions & 23 deletions src/main/java/io/reactivex/disposables/CompositeDisposable.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public boolean isDisposed() {
return disposed;
}

public void add(Disposable d) {
public boolean add(Disposable d) {
Objects.requireNonNull(d, "d is null");
if (!disposed) {
synchronized (this) {
Expand All @@ -81,14 +81,15 @@ public void add(Disposable d) {
resources = set;
}
set.add(d);
return;
return true;
}
}
}
d.dispose();
return false;
}

public void addAll(Disposable... ds) {
public boolean addAll(Disposable... ds) {
Objects.requireNonNull(ds, "ds is null");
if (!disposed) {
synchronized (this) {
Expand All @@ -102,48 +103,40 @@ public void addAll(Disposable... ds) {
Objects.requireNonNull(d, "d is null");
set.add(d);
}
return;
return true;
}
}
}
for (Disposable d : ds) {
d.dispose();
}
return false;
}

public void remove(Disposable d) {
Objects.requireNonNull(d, "Disposable item is null");
if (disposed) {
return;
}
synchronized (this) {
if (disposed) {
return;
}

OpenHashSet<Disposable> set = resources;
if (set == null || !set.remove(d)) {
return;
}
public boolean remove(Disposable d) {
if (delete(d)) {
d.dispose();
return true;
}
d.dispose();
return false;
}

public void delete(Disposable d) {
public boolean delete(Disposable d) {
Objects.requireNonNull(d, "Disposable item is null");
if (disposed) {
return;
return false;
}
synchronized (this) {
if (disposed) {
return;
return false;
}

OpenHashSet<Disposable> set = resources;
if (set == null || !set.remove(d)) {
return;
return false;
}
}
return true;
}

public void clear() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,79 +18,130 @@

import io.reactivex.Flowable;
import io.reactivex.functions.Function;
import io.reactivex.internal.subscriptions.SubscriptionHelper;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.internal.fuseable.ConditionalSubscriber;
import io.reactivex.internal.subscribers.flowable.*;

public final class FlowableMap<T, U> extends Flowable<U> {
final Publisher<T> source;
final Function<? super T, ? extends U> function;
public FlowableMap(Publisher<T> source, Function<? super T, ? extends U> function) {
final Function<? super T, ? extends U> mapper;
public FlowableMap(Publisher<T> source, Function<? super T, ? extends U> mapper) {
this.source = source;
this.function = function;
this.mapper = mapper;
}

@Override
protected void subscribeActual(Subscriber<? super U> s) {
source.subscribe(new MapperSubscriber<T, U>(s, function));
}

static final class MapperSubscriber<T, U> implements Subscriber<T> {
final Subscriber<? super U> actual;
final Function<? super T, ? extends U> function;

Subscription subscription;

boolean done;

public MapperSubscriber(Subscriber<? super U> actual, Function<? super T, ? extends U> function) {
this.actual = actual;
this.function = function;
if (s instanceof ConditionalSubscriber) {
source.subscribe(new MapConditionalSubscriber<T, U>((ConditionalSubscriber<? super U>)s, mapper));
} else {
source.subscribe(new MapSubscriber<T, U>(s, mapper));
}
@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.validateSubscription(this.subscription, s)) {
subscription = s;
actual.onSubscribe(s);
}
}

static final class MapSubscriber<T, U> extends BasicFuseableSubscriber<T, U> {
final Function<? super T, ? extends U> mapper;

public MapSubscriber(Subscriber<? super U> actual, Function<? super T, ? extends U> mapper) {
super(actual);
this.mapper = mapper;
}

@Override
public void onNext(T t) {
if (done) {
return;
}
U u;
try {
u = function.apply(t);
} catch (Throwable e) {
done = true;
subscription.cancel();
actual.onError(e);

if (sourceMode == ASYNC) {
actual.onNext(null);
return;
}
if (u == null) {
done = true;
subscription.cancel();
actual.onError(new NullPointerException("Value returned by the function is null"));

U v;

try {
v = nullCheck(mapper.apply(t), "The mapper function returned a null value.");
} catch (Throwable ex) {
fail(ex);
return;
}
actual.onNext(u);
actual.onNext(v);
}

@Override
public int requestFusion(int mode) {
return transitiveBoundaryFusion(mode);
}

@Override
public U poll() {
T t = qs.poll();
return t != null ? nullCheck(mapper.apply(t), "The mapper function returned a null value.") : null;
}
}

static final class MapConditionalSubscriber<T, U> extends BasicFuseableConditionalSubscriber<T, U> {
final Function<? super T, ? extends U> mapper;

public MapConditionalSubscriber(ConditionalSubscriber<? super U> actual, Function<? super T, ? extends U> function) {
super(actual);
this.mapper = function;
}

@Override
public void onError(Throwable t) {
public void onNext(T t) {
if (done) {
RxJavaPlugins.onError(t);
return;
}
done = true;
actual.onError(t);

if (sourceMode == ASYNC) {
actual.onNext(null);
return;
}

U v;

try {
v = nullCheck(mapper.apply(t), "The mapper function returned a null value.");
} catch (Throwable ex) {
fail(ex);
return;
}
actual.onNext(v);
}

@Override
public void onComplete() {
public boolean tryOnNext(T t) {
if (done) {
return;
return false;
}
done = true;
actual.onComplete();

if (sourceMode == ASYNC) {
actual.onNext(null);
return true;
}

U v;

try {
v = mapper.apply(t);
} catch (Throwable ex) {
fail(ex);
return true;
}
return actual.tryOnNext(v);
}

@Override
public int requestFusion(int mode) {
return transitiveBoundaryFusion(mode);
}

@Override
public U poll() {
T t = qs.poll();
return t != null ? nullCheck(mapper.apply(t), "The mapper function returned a null value.") : null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@
*/
package io.reactivex.internal.operators.observable;

import io.reactivex.Observable.NbpOperator;
import io.reactivex.Observer;
import io.reactivex.*;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Predicate;
import io.reactivex.internal.disposables.DisposableHelper;
import io.reactivex.plugins.RxJavaPlugins;

public final class NbpOperatorAll<T> implements NbpOperator<Boolean, T> {
public final class ObservableAll<T> extends Observable<Boolean> {
final ObservableConsumable<T> source;

final Predicate<? super T> predicate;
public NbpOperatorAll(Predicate<? super T> predicate) {
public ObservableAll(ObservableConsumable<T> source, Predicate<? super T> predicate) {
this.source = source;
this.predicate = predicate;
}

@Override
public Observer<? super T> apply(Observer<? super Boolean> t) {
return new AllSubscriber<T>(t, predicate);
protected void subscribeActual(Observer<? super Boolean> t) {
source.subscribe(new AllSubscriber<T>(t, predicate));
}

static final class AllSubscriber<T> implements Observer<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@
*/
package io.reactivex.internal.operators.observable;

import io.reactivex.Observable.NbpOperator;
import io.reactivex.Observer;
import io.reactivex.*;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Predicate;
import io.reactivex.internal.disposables.DisposableHelper;

public final class NbpOperatorAny<T> implements NbpOperator<Boolean, T> {
public final class ObservableAny<T> extends Observable<Boolean> {
final ObservableConsumable<T> source;
final Predicate<? super T> predicate;
public NbpOperatorAny(Predicate<? super T> predicate) {
public ObservableAny(ObservableConsumable<T> source, Predicate<? super T> predicate) {
this.source = source;
this.predicate = predicate;
}

@Override
public Observer<? super T> apply(Observer<? super Boolean> t) {
return new AnySubscriber<T>(t, predicate);
protected void subscribeActual(Observer<? super Boolean> t) {
source.subscribe(new AnySubscriber<T>(t, predicate));
}

static final class AnySubscriber<T> implements Observer<T> {
Expand Down
Loading