1 package jalview.datamodel;
3 import java.util.ArrayList;
4 import java.util.Iterator;
8 * An iterator which iterates over visible start positions of hidden column
11 public class BoundedStartRegionIterator implements Iterator<Integer>
13 // start position to iterate from
16 // end position to iterate to
19 // current index in hiddenColumns
20 private int currentPosition = 0;
22 // local copy or reference to hiddenColumns
23 private List<Integer> positions = null;
26 * Construct an iterator over hiddenColums bounded at [lowerBound,upperBound]
29 * lower bound to iterate from
31 * upper bound to iterate to
33 * whether to make a local copy of hiddenColumns for iteration (set
34 * to true if calling from outwith the HiddenColumns class)
36 BoundedStartRegionIterator(int lowerBound, int upperBound,
37 List<int[]> hiddenColumns)
42 if (hiddenColumns != null)
44 positions = new ArrayList<>(hiddenColumns.size());
46 // navigate to start, keeping count of hidden columns
49 while ((i < hiddenColumns.size())
50 && (hiddenColumns.get(i)[0] < start + hiddenSoFar))
52 int[] region = hiddenColumns.get(i);
53 hiddenSoFar += region[1] - region[0] + 1;
57 // iterate from start to end, adding start positions of each
58 // hidden region. Positions are visible columns count, not absolute
59 while (i < hiddenColumns.size()
60 && (hiddenColumns.get(i)[0] <= end + hiddenSoFar))
62 int[] region = hiddenColumns.get(i);
63 positions.add(region[0] - hiddenSoFar);
64 hiddenSoFar += region[1] - region[0] + 1;
70 positions = new ArrayList<>();
76 * Construct an iterator over hiddenColums bounded at [lowerBound,upperBound]
79 * a hidden cursor position to start from - may be null
81 * lower bound to iterate from - will be ignored if pos != null
83 * upper bound to iterate to
84 * @param hiddenColumns
85 * the hidden columns collection to use
87 BoundedStartRegionIterator(HiddenCursorPosition pos, int lowerBound,
88 int upperBound, List<int[]> hiddenColumns)
93 if (hiddenColumns != null)
95 positions = new ArrayList<>(hiddenColumns.size());
97 // navigate to start, keeping count of hidden columns
103 // use the cursor position provided
104 i = pos.getRegionIndex();
105 hiddenSoFar = pos.getHiddenSoFar();
110 while ((i < hiddenColumns.size())
111 && (hiddenColumns.get(i)[0] < start + hiddenSoFar))
113 int[] region = hiddenColumns.get(i);
114 hiddenSoFar += region[1] - region[0] + 1;
119 // iterate from start to end, adding start positions of each
120 // hidden region. Positions are visible columns count, not absolute
121 while (i < hiddenColumns.size()
122 && (hiddenColumns.get(i)[0] <= end + hiddenSoFar))
124 int[] region = hiddenColumns.get(i);
125 positions.add(region[0] - hiddenSoFar);
126 hiddenSoFar += region[1] - region[0] + 1;
132 positions = new ArrayList<>();
138 public boolean hasNext()
140 return (currentPosition < positions.size());
144 * Get next hidden region start position
146 * @return the start position in *visible* coordinates
149 public Integer next()
151 int result = positions.get(currentPosition);