I am using the Google Collections library AbstractIterator to implement a generator. I ran across a problem while doing so; I've reduced it to a more basic type and reproduced the problem. This reduction is obviously overkill for what it does, counting from 1 to numelements via an Iterable.
Essentially in the following code, the uncommented version works, and the commented one does not (provides a null element last, instead of ending on the last number).
Am I doing something wrong, or is this a problem with the library?
private Iterable<Integer> elementGenerator(final int numelements) {
return new Iterable<Integer>() {
@Override public Iterator<Integer> iterator() {
return new AbstractIterator<Integer>(){
int localcount=0;
@Override protected Integer computeNext() {
if (localcount++ == numelements) return endOfData();
return localcount;
// return (localcount++ == numelements) ? endOfData() : localcount;
}
};
}
};
}
I also tried fiddling around with the ?:
arrangement (e.g., prefixing the return and comparing to +1 instead), to no avail. I poked around a bit looking for documentation about this, but didn't find anything. Obviously, the ?:
syntax is only a convenience, not a necessity, but still...