-
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 #699 from benjchristensen/repeat
Repeat Operator
- Loading branch information
Showing
4 changed files
with
158 additions
and
2 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
71 changes: 71 additions & 0 deletions
71
rxjava-core/src/main/java/rx/operators/OperationRepeat.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,71 @@ | ||
/** | ||
* Copyright 2013 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 rx.operators; | ||
|
||
import rx.Observable; | ||
import rx.Observer; | ||
import rx.Scheduler; | ||
import rx.Subscription; | ||
import rx.subscriptions.CompositeSubscription; | ||
import rx.subscriptions.MultipleAssignmentSubscription; | ||
import rx.util.functions.Action0; | ||
import rx.util.functions.Action1; | ||
|
||
public class OperationRepeat<T> implements Observable.OnSubscribeFunc<T> { | ||
|
||
private final Observable<T> source; | ||
private final Scheduler scheduler; | ||
|
||
public static <T> Observable.OnSubscribeFunc<T> repeat(Observable<T> source, Scheduler scheduler) { | ||
return new OperationRepeat<T>(source, scheduler); | ||
} | ||
|
||
private OperationRepeat(Observable<T> source, Scheduler scheduler) { | ||
this.source = source; | ||
this.scheduler = scheduler; | ||
} | ||
|
||
@Override | ||
public Subscription onSubscribe(final Observer<? super T> observer) { | ||
final CompositeSubscription compositeSubscription = new CompositeSubscription(); | ||
final MultipleAssignmentSubscription innerSubscription = new MultipleAssignmentSubscription(); | ||
compositeSubscription.add(innerSubscription); | ||
compositeSubscription.add(scheduler.schedule(new Action1<Action0>() { | ||
@Override | ||
public void call(final Action0 self) { | ||
innerSubscription.set(source.subscribe(new Observer<T>() { | ||
|
||
@Override | ||
public void onCompleted() { | ||
self.call(); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable error) { | ||
observer.onError(error); | ||
} | ||
|
||
@Override | ||
public void onNext(T value) { | ||
observer.onNext(value); | ||
} | ||
})); | ||
} | ||
})); | ||
return compositeSubscription; | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
rxjava-core/src/test/java/rx/operators/OperationRepeatTest.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,62 @@ | ||
/** | ||
* Copyright 2013 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 rx.operators; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.junit.Test; | ||
|
||
import rx.Observable; | ||
import rx.Observable.OnSubscribeFunc; | ||
import rx.Observer; | ||
import rx.Subscription; | ||
import rx.schedulers.Schedulers; | ||
import rx.subscriptions.Subscriptions; | ||
|
||
public class OperationRepeatTest { | ||
|
||
@Test | ||
public void testRepetition() { | ||
int NUM = 10; | ||
final AtomicInteger count = new AtomicInteger(); | ||
int value = Observable.create(new OnSubscribeFunc<Integer>() { | ||
|
||
@Override | ||
public Subscription onSubscribe(Observer<? super Integer> o) { | ||
o.onNext(count.incrementAndGet()); | ||
o.onCompleted(); | ||
return Subscriptions.empty(); | ||
} | ||
}).repeat(Schedulers.threadPoolForComputation()).take(NUM).toBlockingObservable().last(); | ||
|
||
assertEquals(NUM, value); | ||
} | ||
|
||
@Test | ||
public void testRepeatTake() { | ||
Observable<Integer> xs = Observable.from(1, 2); | ||
Object[] ys = xs.repeat(Schedulers.newThread()).take(4).toList().toBlockingObservable().last().toArray(); | ||
assertArrayEquals(new Object[] { 1, 2, 1, 2 }, ys); | ||
} | ||
|
||
@Test | ||
public void testNoStackOverFlow() { | ||
Observable.from(1).repeat(Schedulers.newThread()).take(100000).toBlockingObservable().last(); | ||
} | ||
|
||
} |