All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softicar.platform.common.container.iterator.LimitingIterator Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
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();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy