My code is littered with bits like this:
import com.google.common.collect.UnmodifiableIterator;
public final class TimesIterable
implements Iterable<Integer> {
private final int n;
public static Iterable<Integer> upTo(final int n) {
return new TimesIterable(n);
}
public TimesIterable(final int n) {
this.n = n;
}
@Override
public Iterator<Integer> iterator() {
return new UnmodifiableIterator<Integer>() {
private int i = 0;
@Override
public boolean hasNext() {
return i < n;
}
@Override
public Integer next() {
return ++i;
}
};
}
} All so I can write:
import static TimesIterable.upto;
import static java.lang.System.out;
public static void main(final String... args) {
for (int n : upTo(4))
out.println(n);
} To my surprise Google is not quite there yet. (I'd prefer to be wrong).
No comments:
Post a Comment