package jalview.datamodel; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; /** * Iterator over the visible *columns* (not regions) as determined by the set of * hidden columns. Uses a local copy of hidden columns. * * @author kmourao * */ public class VisibleColsIterator implements Iterator { private int last; private int current; private int next; private List localHidden = new ArrayList<>(); private int nexthiddenregion; VisibleColsIterator(int firstcol, int lastcol, List hiddenColumns) { last = lastcol; current = firstcol; next = firstcol; nexthiddenregion = 0; if (hiddenColumns != null) { int i = 0; for (i = 0; i < hiddenColumns.size() && (current <= hiddenColumns.get(i)[0]); ++i) { if (current >= hiddenColumns.get(i)[0] && current <= hiddenColumns.get(i)[1]) { // current is hidden, move to right current = hiddenColumns.get(i)[1] + 1; next = current; nexthiddenregion = i + 1; } } for (i = hiddenColumns.size() - 1; i >= 0 && (last >= hiddenColumns.get(i)[1]); --i) { if (last >= hiddenColumns.get(i)[0] && last <= hiddenColumns.get(i)[1]) { // last is hidden, move to left last = hiddenColumns.get(i)[0] - 1; } } // make a local copy of the bit we need i = nexthiddenregion; while (i < hiddenColumns.size() && hiddenColumns.get(i)[0] <= last) { int[] region = new int[] { hiddenColumns.get(i)[0], hiddenColumns.get(i)[1] }; localHidden.add(region); i++; } } } @Override public boolean hasNext() { return next <= last; } @Override public Integer next() { if (next > last) { throw new NoSuchElementException(); } current = next; if ((localHidden != null) && (nexthiddenregion < localHidden.size())) { // still some more hidden regions if (next + 1 < localHidden.get(nexthiddenregion)[0]) { // next+1 is still before the next hidden region next++; } else if ((next + 1 >= localHidden.get(nexthiddenregion)[0]) && (next + 1 <= localHidden.get(nexthiddenregion)[1])) { // next + 1 is in the next hidden region next = localHidden.get(nexthiddenregion)[1] + 1; nexthiddenregion++; } } else { // finished with hidden regions, just increment normally next++; } return current; } @Override public void remove() { throw new UnsupportedOperationException(); } }