forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
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 ReactiveX#137 from mairbek/ranges
Implemented range operator. Fixes issue ReactiveX#68
- Loading branch information
Showing
2 changed files
with
122 additions
and
0 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
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,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; | ||
} | ||
|
||
} | ||
} |