in ch-commons-util/src/main/java/com/cloudhopper/commons/util/RoundRobinLoadBalancedList.java [174:228]
public E getNext() {
// hmm... actually, this is a good safety check to do now -- if allitems
// is zero, then there is no way we could have a next item
if (allItems.isEmpty()) {
return null;
}
// if there are items in "allItems", but remainingItems is still zero
// then this means some sort of bug occurred since this should be impossible
if (remainingItems.isEmpty()) {
logger.error("Impossible bug occurred with RoundRobinLoadBalancedList where allItems > 0, but remainingItems == 0, going to completely internally rebuild everything");
resetCountsAndRebuildRemainingItems();
}
// saftey check -- see if we need to reset the index back to zero
if (this.currentIndex >= remainingItems.size()) {
// reset back to zero
this.currentIndex = 0;
}
// get the item at this index
Node<E> node = this.remainingItems.get(this.currentIndex);
// increment count
node.incrementCount();
// is the max count reached?
if (node.getCount() >= node.getWeight()) {
// reset count back to zero
node.setCount(0);
//logger.debug("Removing node " + node.getValue());
// remove this node from possible items
this.remainingItems.remove(this.currentIndex);
// don't increment index since the current index actually would
// be the next item we want to return
} else {
// keep the current node and increment the index
// always increment currentIndex first
this.currentIndex++;
}
// if there aren't any more remaining items, rebuid it
if (remainingItems.isEmpty()) {
// rebuild a new remainingItems array
rebuildRemainingItems();
}
// see if we need to reset the index back to zero for next getNext call
if (this.currentIndex >= remainingItems.size()) {
// reset back to zero
this.currentIndex = 0;
}
return node.getValue();
}