From c59b0d14943417dd5cab2af0f5255d2d6cb58e59 Mon Sep 17 00:00:00 2001 From: Mairbek Khadikov Date: Sun, 10 Feb 2013 21:09:26 +0200 Subject: [PATCH] Implemented range operator --- rxjava-core/src/main/java/rx/Observable.java | 15 +++ rxjava-core/src/main/java/rx/util/Range.java | 107 +++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 rxjava-core/src/main/java/rx/util/Range.java diff --git a/rxjava-core/src/main/java/rx/Observable.java b/rxjava-core/src/main/java/rx/Observable.java index 0fd95c00681..0df09cb3c7f 100644 --- a/rxjava-core/src/main/java/rx/Observable.java +++ b/rxjava-core/src/main/java/rx/Observable.java @@ -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; @@ -547,6 +548,20 @@ public static Observable 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 Observable.Range Method (Int32, Int32) + */ + public static Observable 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. *

diff --git a/rxjava-core/src/main/java/rx/util/Range.java b/rxjava-core/src/main/java/rx/util/Range.java new file mode 100644 index 00000000000..bfc1bad44d5 --- /dev/null +++ b/rxjava-core/src/main/java/rx/util/Range.java @@ -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 { + 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 iterator() { + return new Iterator() { + 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 List toList(Iterable iterable) { + List result = new ArrayList(); + for (T element : iterable) { + result.add(element); + } + return result; + } + + } +} \ No newline at end of file