Skip to content

Commit 185a575

Browse files
Merge pull request #841 from benjchristensen/operator-range
Range OnSubscribe
2 parents 589d360 + 4154c0f commit 185a575

File tree

8 files changed

+163
-152
lines changed

8 files changed

+163
-152
lines changed

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import rx.observables.ConnectableObservable;
3434
import rx.observables.GroupedObservable;
3535
import rx.observers.SafeSubscriber;
36+
import rx.operators.OnSubscribeRange;
3637
import rx.operators.OperationAll;
3738
import rx.operators.OperationAmb;
3839
import rx.operators.OperationAny;
@@ -95,7 +96,7 @@
9596
import rx.operators.OperationWindow;
9697
import rx.operators.OperatorCast;
9798
import rx.operators.OperatorDoOnEach;
98-
import rx.operators.OperatorFromIterable;
99+
import rx.operators.OnSubscribeFromIterable;
99100
import rx.operators.OperatorGroupBy;
100101
import rx.operators.OperatorMap;
101102
import rx.operators.OperatorMerge;
@@ -120,7 +121,6 @@
120121
import rx.subscriptions.Subscriptions;
121122
import rx.util.Exceptions;
122123
import rx.util.OnErrorNotImplementedException;
123-
import rx.util.Range;
124124
import rx.util.TimeInterval;
125125
import rx.util.Timestamped;
126126
import rx.util.functions.Action0;
@@ -1217,7 +1217,7 @@ public final static <T> Observable<T> from(Future<? extends T> future, Scheduler
12171217
* @see <a href="https://github.com/Netflix/RxJava/wiki/Creating-Observables#wiki-from">RxJava Wiki: from()</a>
12181218
*/
12191219
public final static <T> Observable<T> from(Iterable<? extends T> iterable) {
1220-
return create(new OperatorFromIterable<T>(iterable));
1220+
return create(new OnSubscribeFromIterable<T>(iterable));
12211221
}
12221222

12231223
/**
@@ -1239,7 +1239,7 @@ public final static <T> Observable<T> from(Iterable<? extends T> iterable) {
12391239
* @see <a href="http://msdn.microsoft.com/en-us/library/hh212140.aspx">MSDN: Observable.ToObservable</a>
12401240
*/
12411241
public final static <T> Observable<T> from(Iterable<? extends T> iterable, Scheduler scheduler) {
1242-
return create(new OperatorFromIterable<T>(iterable)).subscribeOn(scheduler);
1242+
return create(new OnSubscribeFromIterable<T>(iterable)).subscribeOn(scheduler);
12431243
}
12441244

12451245
/**
@@ -2439,7 +2439,10 @@ public final static <T> Observable<Observable<T>> parallelMerge(Observable<Obser
24392439
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229460.aspx">MSDN: Observable.Range</a>
24402440
*/
24412441
public final static Observable<Integer> range(int start, int count) {
2442-
return from(Range.createWithCount(start, count));
2442+
if ((start + count) > Integer.MAX_VALUE) {
2443+
throw new IllegalArgumentException("start + count can not exceed Integer.MAX_VALUE");
2444+
}
2445+
return Observable.create(new OnSubscribeRange(start, start + count));
24432446
}
24442447

24452448
/**
@@ -2459,7 +2462,10 @@ public final static Observable<Integer> range(int start, int count) {
24592462
* @see <a href="http://msdn.microsoft.com/en-us/library/hh211896.aspx">MSDN: Observable.Range</a>
24602463
*/
24612464
public final static Observable<Integer> range(int start, int count, Scheduler scheduler) {
2462-
return from(Range.createWithCount(start, count), scheduler);
2465+
if ((start + count) > Integer.MAX_VALUE) {
2466+
throw new IllegalArgumentException("start + count can not exceed Integer.MAX_VALUE");
2467+
}
2468+
return Observable.create(new OnSubscribeRange(start, start + count)).subscribeOn(scheduler);
24632469
}
24642470

24652471
/**

rxjava-core/src/main/java/rx/operators/OperatorFromIterable.java renamed to rxjava-core/src/main/java/rx/operators/OnSubscribeFromIterable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
* You can convert any object that supports the Iterable interface into an Observable that emits
2727
* each item in the object, with the toObservable operation.
2828
*/
29-
public final class OperatorFromIterable<T> implements OnSubscribe<T> {
29+
public final class OnSubscribeFromIterable<T> implements OnSubscribe<T> {
3030

3131
final Iterable<? extends T> is;
3232

33-
public OperatorFromIterable(Iterable<? extends T> iterable) {
33+
public OnSubscribeFromIterable(Iterable<? extends T> iterable) {
3434
this.is = iterable;
3535
}
3636

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.operators;
17+
18+
import rx.Observable.OnSubscribe;
19+
import rx.Subscriber;
20+
21+
/**
22+
*/
23+
public final class OnSubscribeRange implements OnSubscribe<Integer> {
24+
25+
private final int start;
26+
private final int end;
27+
28+
public OnSubscribeRange(int start, int end) {
29+
this.start = start;
30+
this.end = end;
31+
}
32+
33+
@Override
34+
public void call(Subscriber<? super Integer> o) {
35+
for (int i = start; i < end; i++) {
36+
if (o.isUnsubscribed()) {
37+
return;
38+
}
39+
o.onNext(i);
40+
}
41+
o.onCompleted();
42+
}
43+
44+
}

rxjava-core/src/main/java/rx/util/Range.java

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package rx.operators;
2+
3+
import rx.Observable;
4+
import rx.perf.AbstractPerformanceTester;
5+
import rx.perf.IntegerSumObserver;
6+
import rx.util.functions.Action0;
7+
8+
public class OperatorRangePerformance extends AbstractPerformanceTester {
9+
10+
static int reps = Integer.MAX_VALUE / 8;
11+
12+
OperatorRangePerformance() {
13+
super(reps);
14+
}
15+
16+
public static void main(String args[]) {
17+
18+
final OperatorRangePerformance spt = new OperatorRangePerformance();
19+
try {
20+
spt.runTest(new Action0() {
21+
22+
@Override
23+
public void call() {
24+
spt.timeRange();
25+
}
26+
});
27+
} catch (Exception e) {
28+
e.printStackTrace();
29+
}
30+
31+
}
32+
33+
/**
34+
*
35+
* -- 0.17
36+
*
37+
* Run: 10 - 271,147,198 ops/sec
38+
* Run: 11 - 274,821,481 ops/sec
39+
* Run: 12 - 271,632,295 ops/sec
40+
* Run: 13 - 277,876,014 ops/sec
41+
* Run: 14 - 274,821,763 ops/sec
42+
*
43+
* -- 0.16.1
44+
*
45+
* Run: 10 - 222,104,280 ops/sec
46+
* Run: 11 - 224,311,761 ops/sec
47+
* Run: 12 - 222,999,339 ops/sec
48+
* Run: 13 - 222,344,174 ops/sec
49+
* Run: 14 - 225,247,983 ops/sec
50+
*
51+
* @return
52+
*/
53+
public long timeRange() {
54+
IntegerSumObserver o = new IntegerSumObserver();
55+
Observable.range(1, reps).subscribe(o);
56+
return o.sum;
57+
}
58+
59+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.operators;
17+
18+
import static org.mockito.Mockito.*;
19+
20+
import org.junit.Test;
21+
22+
import rx.Observable;
23+
import rx.Observer;
24+
25+
public class OnSubscribeRangeTest {
26+
27+
@Test
28+
public void testRangeStartAt2Count3() {
29+
@SuppressWarnings("unchecked")
30+
Observer<Integer> observer = mock(Observer.class);
31+
Observable.range(2, 3).subscribe(observer);
32+
33+
verify(observer, times(1)).onNext(2);
34+
verify(observer, times(1)).onNext(3);
35+
verify(observer, times(1)).onNext(4);
36+
verify(observer, never()).onNext(5);
37+
verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class));
38+
verify(observer, times(1)).onCompleted();
39+
}
40+
}

rxjava-core/src/test/java/rx/operators/OperatorFromIterableTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
*/
1616
package rx.operators;
1717

18-
import static org.mockito.Matchers.*;
19-
import static org.mockito.Mockito.*;
18+
import static org.mockito.Matchers.any;
19+
import static org.mockito.Mockito.mock;
20+
import static org.mockito.Mockito.times;
21+
import static org.mockito.Mockito.verify;
2022

2123
import java.util.Arrays;
2224

@@ -30,7 +32,7 @@ public class OperatorFromIterableTest {
3032

3133
@Test
3234
public void testIterable() {
33-
Observable<String> observable = Observable.create(new OperatorFromIterable<String>(Arrays.<String> asList("one", "two", "three")));
35+
Observable<String> observable = Observable.create(new OnSubscribeFromIterable<String>(Arrays.<String> asList("one", "two", "three")));
3436

3537
@SuppressWarnings("unchecked")
3638
Observer<String> observer = mock(Observer.class);
@@ -41,7 +43,7 @@ public void testIterable() {
4143
verify(observer, Mockito.never()).onError(any(Throwable.class));
4244
verify(observer, times(1)).onCompleted();
4345
}
44-
46+
4547
@Test
4648
public void testObservableFromIterable() {
4749
Observable<String> observable = Observable.from(Arrays.<String> asList("one", "two", "three"));

0 commit comments

Comments
 (0)