Skip to content

Commit

Permalink
Merge pull request ReactiveX#137 from mairbek/ranges
Browse files Browse the repository at this point in the history
Implemented range operator. Fixes issue ReactiveX#68
  • Loading branch information
benjchristensen committed Feb 11, 2013
2 parents 8cdeafd + c59b0d1 commit 36d6c43
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
15 changes: 15 additions & 0 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import rx.operators.OperationZip;
import rx.util.AtomicObservableSubscription;
import rx.util.AtomicObserver;
import rx.util.Range;
import rx.util.functions.Action0;
import rx.util.functions.Action1;
import rx.util.functions.Func1;
Expand Down Expand Up @@ -547,6 +548,20 @@ public static <T> Observable<T> from(T... items) {
return toObservable(items);
}

/**
* Generates an observable sequence of integral numbers within a specified range.
*
* @param start The value of the first integer in the sequence
* @param count The number of sequential integers to generate.
*
* @return An observable sequence that contains a range of sequential integral numbers.
*
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229460(v=vs.103).aspx">Observable.Range Method (Int32, Int32)</a>
*/
public static Observable<Integer> range(int start, int count) {
return from(Range.createWithCount(start, count));
}

/**
* Returns an Observable that notifies an {@link Observer} of a single value and then completes.
* <p>
Expand Down
107 changes: 107 additions & 0 deletions rxjava-core/src/main/java/rx/util/Range.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* 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.util;

import org.junit.Test;

import java.util.*;

import static org.junit.Assert.assertEquals;

public final class Range implements Iterable<Integer> {
private final int start;
private final int end;
private final int step;

public static Range createWithCount(int start, int count) {
return create(start, start * (count + 1));
}

public static Range create(int start, int end) {
return new Range(start, end, 1);
}

public static Range createWithStep(int start, int end, int step) {
return new Range(start, end, step);
}

private Range(int start, int end, int step) {
this.start = start;
this.end = end;
this.step = step;
}

@Override
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
private int current = start;

@Override
public boolean hasNext() {
return current < end;
}

@Override
public Integer next() {
if (!hasNext()) {
throw new NoSuchElementException("No more elements");
}
int result = current;
current += step;
return result;
}

@Override
public void remove() {
throw new UnsupportedOperationException("Read only iterator");
}
};
}

@Override
public String toString() {
return "Range (" + start + ", " + end + "), step " + step;
}


public static class UnitTest {

@Test
public void testSimpleRange() {
assertEquals(Arrays.asList(1, 2, 3, 4), toList(Range.create(1, 5)));
}

@Test
public void testRangeWithStep() {
assertEquals(Arrays.asList(1, 3, 5, 7, 9), toList(Range.createWithStep(1, 10, 2)));
}

@Test
public void testRangeWithCount() {
assertEquals(Arrays.asList(1, 2, 3, 4, 5), toList(Range.createWithCount(1, 5)));
}


private static <T> List<T> toList(Iterable<T> iterable) {
List<T> result = new ArrayList<T>();
for (T element : iterable) {
result.add(element);
}
return result;
}

}
}

0 comments on commit 36d6c43

Please sign in to comment.