package jalview.datamodel; import java.util.Iterator; import java.util.List; /** * A local iterator which iterates over hidden column regions in a range. * Intended for use ONLY within the HiddenColumns class, because it works * directly with the hiddenColumns collection without locking (callers should * lock hiddenColumns). */ public class RegionsIterator implements Iterator { // start position to iterate from private int start; // end position to iterate to private int end; // current index in hiddenColumns private int currentPosition = 0; // current column in hiddenColumns private int[] nextRegion = null; private int[] currentRegion = null; private int removedIndex = -1; private final List hiddenColumns; // Constructor with bounds RegionsIterator(int lowerBound, int upperBound, List hiddenCols, HiddenColumnsCursor cursor) { start = lowerBound; end = upperBound; hiddenColumns = hiddenCols; if (hiddenColumns != null) { // TODO remove whole class? // commented out to compile // currentPosition = cursor.findRegionForColumn(start); if (currentPosition < hiddenColumns.size()) { nextRegion = hiddenColumns.get(currentPosition); } } } @Override public boolean hasNext() { return (hiddenColumns != null) && (nextRegion != null) && (nextRegion[0] <= end); } @Override public int[] next() { currentRegion = nextRegion; currentPosition++; if (currentPosition < hiddenColumns.size()) { nextRegion = hiddenColumns.get(currentPosition); } else { nextRegion = null; } return currentRegion; } @Override public void remove() { if ((currentRegion != null) && (removedIndex != currentPosition)) { currentPosition--; hiddenColumns.subList(currentPosition, currentPosition + 1).clear(); removedIndex = currentPosition; } else { // already removed element last returned by next() // or next() has not yet been called throw new IllegalStateException(); } } }