2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
28 * An iterator which iterates over visible start positions of hidden column
31 public class StartRegionIterator implements Iterator<Integer>
33 // start position to iterate from
36 // end position to iterate to
39 // current index in hiddenColumns
40 private int currentPosition = 0;
42 // local copy or reference to hiddenColumns
43 private List<Integer> positions = null;
46 * Construct an iterator over hiddenColums bounded at [lowerBound,upperBound]
49 * lower bound to iterate from
51 * upper bound to iterate to
53 * whether to make a local copy of hiddenColumns for iteration (set
54 * to true if calling from outwith the HiddenColumns class)
56 StartRegionIterator(int lowerBound, int upperBound,
57 List<int[]> hiddenColumns)
59 this(null, lowerBound, upperBound, hiddenColumns);
63 * Construct an iterator over hiddenColums bounded at [lowerBound,upperBound]
66 * a hidden cursor position to start from - may be null
68 * lower bound to iterate from - will be ignored if pos != null
70 * upper bound to iterate to
71 * @param hiddenColumns
72 * the hidden columns collection to use
74 StartRegionIterator(HiddenCursorPosition pos, int lowerBound,
75 int upperBound, List<int[]> hiddenColumns)
80 if (hiddenColumns != null)
82 positions = new ArrayList<>(hiddenColumns.size());
84 // navigate to start, keeping count of hidden columns
90 // use the cursor position provided
91 i = pos.getRegionIndex();
92 hiddenSoFar = pos.getHiddenSoFar();
97 while ((i < hiddenColumns.size())
98 && (hiddenColumns.get(i)[0] < start + hiddenSoFar))
100 int[] region = hiddenColumns.get(i);
101 hiddenSoFar += region[1] - region[0] + 1;
106 // iterate from start to end, adding start positions of each
107 // hidden region. Positions are visible columns count, not absolute
108 while (i < hiddenColumns.size()
109 && (hiddenColumns.get(i)[0] <= end + hiddenSoFar))
111 int[] region = hiddenColumns.get(i);
112 positions.add(region[0] - hiddenSoFar);
113 hiddenSoFar += region[1] - region[0] + 1;
119 positions = new ArrayList<>();
125 public boolean hasNext()
127 return (currentPosition < positions.size());
131 * Get next hidden region start position
133 * @return the start position in *visible* coordinates
136 public Integer next()
138 int result = positions.get(currentPosition);