Created
November 12, 2012 20:29
-
-
Save electrum/4061684 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package javatest; | |
import com.google.common.collect.AbstractIterator; | |
import com.google.common.collect.BoundType; | |
import com.google.common.collect.DiscreteDomains; | |
import com.google.common.collect.Ranges; | |
import java.util.Iterator; | |
import static com.google.common.base.Preconditions.checkArgument; | |
public class NumberRange | |
{ | |
public static void main(String[] args) | |
{ | |
for (Long n : rangesRange(0, 10)) { | |
System.out.println(n); | |
} | |
System.out.println(); | |
for (Long n : iteratorRange(0, 10)) { | |
System.out.println(n); | |
} | |
System.out.println(); | |
} | |
private static Iterable<Long> rangesRange(long start, long end) | |
{ | |
return Ranges.range(start, BoundType.CLOSED, end, BoundType.OPEN).asSet(DiscreteDomains.longs()); | |
} | |
public static Iterable<Long> iteratorRange(final long start, final long end) | |
{ | |
checkArgument(start <= end, "start is greater than end"); | |
return new Iterable<Long>() | |
{ | |
public Iterator<Long> iterator() | |
{ | |
return new AbstractIterator<Long>() | |
{ | |
private long n = start; | |
@Override | |
protected Long computeNext() | |
{ | |
if (n == end) { | |
return endOfData(); | |
} | |
return n++; | |
} | |
}; | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment