-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3224 from akarnokd/OperatorDistinct2x
Operator distinct, timeInterval, common Timed container.
- Loading branch information
Showing
6 changed files
with
354 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
src/main/java/io/reactivex/internal/operators/OperatorDistinct.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/** | ||
* Copyright 2015 Netflix, Inc. | ||
* | ||
* 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.internal.operators; | ||
|
||
import java.util.*; | ||
import java.util.function.*; | ||
|
||
import org.reactivestreams.*; | ||
|
||
import io.reactivex.Observable.Operator; | ||
import io.reactivex.internal.subscribers.CancellingSubscriber; | ||
import io.reactivex.internal.subscriptions.*; | ||
|
||
public final class OperatorDistinct<T> implements Operator<T, T> { | ||
|
||
final Supplier<? extends Predicate<? super T>> predicateSupplier; | ||
|
||
public OperatorDistinct(Supplier<? extends Predicate<? super T>> predicateSupplier) { | ||
this.predicateSupplier = predicateSupplier; | ||
} | ||
|
||
public static <T> OperatorDistinct<T> withCollection(Supplier<? extends Collection<? super T>> collectionSupplier) { | ||
Supplier<? extends Predicate<? super T>> p = () -> { | ||
Collection<? super T> coll = collectionSupplier.get(); | ||
|
||
return t -> { | ||
if (t == null) { | ||
coll.clear(); | ||
return true; | ||
} | ||
return coll.add(t); | ||
}; | ||
}; | ||
|
||
return new OperatorDistinct<>(p); | ||
} | ||
|
||
static final OperatorDistinct<Object> UNTIL_CHANGED; | ||
static { | ||
Supplier<? extends Predicate<? super Object>> p = () -> { | ||
Object[] last = { null }; | ||
|
||
return t -> { | ||
if (t == null) { | ||
last[0] = null; | ||
return true; | ||
} | ||
Object o = last[0]; | ||
last[0] = t; | ||
return !Objects.equals(o, t); | ||
}; | ||
}; | ||
UNTIL_CHANGED = new OperatorDistinct<>(p); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <T> OperatorDistinct<T> untilChanged() { | ||
return (OperatorDistinct<T>)UNTIL_CHANGED; | ||
} | ||
|
||
@Override | ||
public Subscriber<? super T> apply(Subscriber<? super T> t) { | ||
Predicate<? super T> coll; | ||
try { | ||
coll = predicateSupplier.get(); | ||
} catch (Throwable e) { | ||
t.onSubscribe(EmptySubscription.INSTANCE); | ||
t.onError(e); | ||
return CancellingSubscriber.INSTANCE; | ||
} | ||
|
||
if (coll == null) { | ||
t.onSubscribe(EmptySubscription.INSTANCE); | ||
t.onError(new NullPointerException("predicateSupplier returned null")); | ||
return CancellingSubscriber.INSTANCE; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
static final class DistinctSubscriber<T> implements Subscriber<T> { | ||
final Subscriber<? super T> actual; | ||
final Predicate<? super T> predicate; | ||
|
||
Subscription s; | ||
|
||
public DistinctSubscriber(Subscriber<? super T> actual, Predicate<? super T> predicate) { | ||
this.actual = actual; | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Subscription s) { | ||
if (SubscriptionHelper.validateSubscription(this.s, s)) { | ||
return; | ||
} | ||
this.s = s; | ||
actual.onSubscribe(s); | ||
} | ||
|
||
@Override | ||
public void onNext(T t) { | ||
boolean b; | ||
try { | ||
b = predicate.test(t); | ||
} catch (Throwable e) { | ||
s.cancel(); | ||
actual.onError(e); | ||
return; | ||
} | ||
|
||
if (b) { | ||
actual.onNext(t); | ||
} else { | ||
s.request(1); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
try { | ||
predicate.test(null); // special case: poison pill | ||
} catch (Throwable e) { | ||
t.addSuppressed(e); | ||
actual.onError(t); | ||
return; | ||
} | ||
actual.onError(t); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
try { | ||
predicate.test(null); // special case: poison pill | ||
} catch (Throwable e) { | ||
actual.onError(e); | ||
return; | ||
} | ||
actual.onComplete(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/io/reactivex/internal/operators/OperatorTimeInterval.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* Copyright 2015 Netflix, Inc. | ||
* | ||
* 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.internal.operators; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.reactivestreams.*; | ||
|
||
import io.reactivex.Observable.Operator; | ||
import io.reactivex.Scheduler; | ||
import io.reactivex.schedulers.Timed; | ||
|
||
public final class OperatorTimeInterval<T> implements Operator<Timed<T>, T> { | ||
final Scheduler scheduler; | ||
final TimeUnit unit; | ||
|
||
public OperatorTimeInterval(TimeUnit unit, Scheduler scheduler) { | ||
this.scheduler = scheduler; | ||
this.unit = unit; | ||
} | ||
|
||
@Override | ||
public Subscriber<? super T> apply(Subscriber<? super Timed<T>> t) { | ||
return new TimeIntervalSubscriber<>(t, unit, scheduler); | ||
} | ||
|
||
static final class TimeIntervalSubscriber<T> implements Subscriber<T> { | ||
final Subscriber<? super Timed<T>> actual; | ||
final TimeUnit unit; | ||
final Scheduler scheduler; | ||
|
||
long lastTime; | ||
|
||
public TimeIntervalSubscriber(Subscriber<? super Timed<T>> actual, TimeUnit unit, Scheduler scheduler) { | ||
this.actual = actual; | ||
this.scheduler = scheduler; | ||
this.unit = unit; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Subscription s) { | ||
lastTime = scheduler.now(unit); | ||
actual.onSubscribe(s); | ||
} | ||
|
||
@Override | ||
public void onNext(T t) { | ||
long now = scheduler.now(unit); | ||
long last = lastTime; | ||
lastTime = now; | ||
long delta = now - last; | ||
actual.onNext(new Timed<>(t, delta, unit)); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
actual.onError(t); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
actual.onComplete(); | ||
} | ||
} | ||
} |
Oops, something went wrong.