com.softicar.platform.common.container.iterator.LimitingIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-common Show documentation
Show all versions of platform-common Show documentation
The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.
package com.softicar.platform.common.container.iterator;
import java.util.Iterator;
/**
* An {@link Iterator} limiting the returned values of another {@link Iterator}.
*
* @author Oliver Richers
*/
public class LimitingIterator implements Iterator {
private final Iterator iterator;
private final int offset;
private final int limit;
private int count;
public LimitingIterator(Iterator iterator, int limit) {
this(iterator, 0, limit);
}
public LimitingIterator(Iterator iterator, int offset, int limit) {
this.iterator = iterator;
this.offset = offset;
this.limit = limit;
this.count = 0;
iterateToOffset();
}
private void iterateToOffset() {
for (int i = 0; i < offset && iterator.hasNext(); i++) {
iterator.next();
}
}
@Override
public boolean hasNext() {
return iterator.hasNext() && !isLimitReached();
}
private boolean isLimitReached() {
return limit > 0 && count >= limit;
}
@Override
public T next() {
count++;
return iterator.next();
}
}