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.List;
24 import java.util.concurrent.atomic.AtomicReference;
26 public class HiddenColumnsCursor
28 // absolute position of first hidden column
29 private int firstColumn;
31 private List<int[]> hiddenColumns;
33 // AtomicReference to hold the current region index and hidden column count
34 // Could be done with synchronisation but benchmarking shows this way is 2x
36 private final AtomicReference<HiddenCursorPosition> cursorPos = new AtomicReference<>(
37 new HiddenCursorPosition(0, 0));
39 protected HiddenColumnsCursor()
45 * Reset the cursor with a new hidden columns collection. Calls to resetCursor
46 * should be made from within a writeLock in the HiddenColumns class - since
47 * changes to the hiddenColumns collection require a writeLock the lock should
51 * new hidden columns collection
53 protected void resetCursor(List<int[]> hiddenCols)
55 resetCursor(hiddenCols, 0, 0);
59 * Reset the cursor with a new hidden columns collection, where we know in
60 * advance the index and hidden columns count of a particular location.
63 * new hidden columns collection
65 * cursor index to reset to
67 * hidden columns count to reset to
69 protected void resetCursor(List<int[]> hiddenCols, int index,
72 hiddenColumns = hiddenCols;
73 if ((hiddenCols != null) && (!hiddenCols.isEmpty()))
75 firstColumn = hiddenColumns.get(0)[0];
76 HiddenCursorPosition oldpos = cursorPos.get();
77 HiddenCursorPosition newpos = new HiddenCursorPosition(index,
79 cursorPos.compareAndSet(oldpos, newpos);
84 * Delete the region the cursor is currently at. Avoids having to reset the
85 * cursor just because we deleted a region.
87 * Calls to updateForDeletedRegion should be made from within a writeLock in
88 * the HiddenColumns class - since changes to the hiddenColumns collection
89 * require a writeLock the lock should already exist.
93 protected void updateForDeletedRegion(List<int[]> hiddenCols)
96 if ((hiddenCols != null) && (!hiddenCols.isEmpty()))
98 // if there is a region to the right of the current region,
99 // nothing changes; otherwise
100 // we deleted the last region (index=hiddenCols.size()-1)
101 // or the index was at the end of the alignment (index=hiddenCols.size())
102 HiddenCursorPosition oldpos = cursorPos.get();
104 int index = oldpos.getRegionIndex();
105 if (index >= hiddenColumns.size() - 1)
107 // deleted last region, index is now end of alignment
108 index = hiddenCols.size();
110 HiddenCursorPosition newpos = new HiddenCursorPosition(index,
111 oldpos.getHiddenSoFar());
112 cursorPos.compareAndSet(oldpos, newpos);
115 hiddenColumns = hiddenCols;
119 * Get the index of the region that column is within (if column is hidden) or
120 * which is to the right of column (if column is visible). If no hidden
121 * columns are to the right, will return size of hiddenColumns. If hidden
122 * columns is empty returns -1.
125 * absolute position of a column in the alignment
126 * @return region index
128 protected HiddenCursorPosition findRegionForColumn(int column)
130 if (hiddenColumns == null)
135 HiddenCursorPosition oldpos = cursorPos.get();
136 int index = oldpos.getRegionIndex();
137 int hiddenCount = oldpos.getHiddenSoFar();
139 if (index == hiddenColumns.size())
141 // went past the end of hiddenColumns collection last time
143 int[] region = hiddenColumns.get(index);
144 hiddenCount -= region[1] - region[0] + 1;
147 // this if statement excludes case where column is in current region
148 // - no changes needed
149 if (column < firstColumn)
154 // column is after current region
155 else if (column > hiddenColumns.get(index)[1])
157 // iterate from where we are now, if we're lucky we'll be close by
158 // (but still better than iterating from 0)
159 // stop when we find the region *before* column
160 // i.e. the next region starts after column or if not, ends after column
161 while ((index < hiddenColumns.size())
162 && (column > hiddenColumns.get(index)[1]))
164 int[] region = hiddenColumns.get(index);
165 hiddenCount += region[1] - region[0] + 1;
169 // column is before current region
170 else if (column < hiddenColumns.get(index)[0])
172 // column is before or in the previous region
173 while ((index > 0) && (hiddenColumns.get(index - 1)[1] >= column))
176 int[] region = hiddenColumns.get(index);
177 hiddenCount -= region[1] - region[0] + 1;
181 if (index != oldpos.getRegionIndex()
182 || hiddenCount != oldpos.getHiddenSoFar())
184 HiddenCursorPosition newpos = new HiddenCursorPosition(index,
186 cursorPos.compareAndSet(oldpos, newpos);
193 * Get the number of hidden columns in regions before column i.e. excludes
194 * hidden columns in the region column is in, if any
197 * index of column in visible alignment
200 protected HiddenCursorPosition getHiddenOffset(int column)
202 if (hiddenColumns == null)
207 HiddenCursorPosition oldpos = cursorPos.get();
208 int index = oldpos.getRegionIndex();
209 int hiddenCount = oldpos.getHiddenSoFar();
211 if (column < firstColumn)
216 else if ((index < hiddenColumns.size())
217 && (hiddenColumns.get(index)[0] <= column + hiddenCount))
219 // iterate from where we are now, if we're lucky we'll be close by
220 // (but still better than iterating from 0)
221 while ((index < hiddenColumns.size())
222 && (hiddenColumns.get(index)[0] <= column + hiddenCount))
224 int[] region = hiddenColumns.get(index);
225 hiddenCount += region[1] - region[0] + 1;
232 && (hiddenColumns.get(index - 1)[1] >= column + hiddenCount))
235 int[] region = hiddenColumns.get(index);
236 hiddenCount -= region[1] - region[0] + 1;
241 if (index != oldpos.getRegionIndex()
242 || hiddenCount != oldpos.getHiddenSoFar())
244 HiddenCursorPosition newpos = new HiddenCursorPosition(index,
246 cursorPos.compareAndSet(oldpos, newpos);