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 cursor pointing to the hidden region that column is within (if
120 * column is hidden) or which is to the right of column (if column is
121 * visible). If no hidden columns are to the right, returns a cursor pointing
122 * to an imaginary hidden region beyond the end of the hidden columns
123 * collection (this ensures the count of previous hidden columns is correct).
124 * If hidden columns is empty returns null.
127 * absolute position of a column in the alignment
128 * @return cursor pointing to hidden region containing the column (if hidden)
129 * or to the right of the column (if visible)
131 protected HiddenCursorPosition findRegionForColumn(int column)
133 if (hiddenColumns == null)
138 HiddenCursorPosition oldpos = cursorPos.get();
139 int index = oldpos.getRegionIndex();
140 int hiddenCount = oldpos.getHiddenSoFar();
142 if (index == hiddenColumns.size())
144 // went past the end of hiddenColumns collection last time
146 int[] region = hiddenColumns.get(index);
147 hiddenCount -= region[1] - region[0] + 1;
150 // this if statement excludes case where column is in current region
151 // - no changes needed
152 if (column < firstColumn)
157 // column is after current region
158 else if (column > hiddenColumns.get(index)[1])
160 // iterate from where we are now, if we're lucky we'll be close by
161 // (but still better than iterating from 0)
162 // stop when we find the region *before* column
163 // i.e. the next region starts after column or if not, ends after column
164 while ((index < hiddenColumns.size())
165 && (column > hiddenColumns.get(index)[1]))
167 int[] region = hiddenColumns.get(index);
168 hiddenCount += region[1] - region[0] + 1;
172 // column is before current region
173 else if (column < hiddenColumns.get(index)[0])
175 // column is before or in the previous region
176 while ((index > 0) && (hiddenColumns.get(index - 1)[1] >= column))
179 int[] region = hiddenColumns.get(index);
180 hiddenCount -= region[1] - region[0] + 1;
184 if (index != oldpos.getRegionIndex()
185 || hiddenCount != oldpos.getHiddenSoFar())
187 HiddenCursorPosition newpos = new HiddenCursorPosition(index,
189 cursorPos.compareAndSet(oldpos, newpos);
196 * Get the cursor pointing to the hidden region just after a visible column
199 * index of column in *visible* alignment (therefore by definition
201 * @return cursor pointing to hidden region to the right of the column
203 protected HiddenCursorPosition findRegionForVisColumn(int column)
205 if (hiddenColumns == null)
210 HiddenCursorPosition oldpos = cursorPos.get();
211 int index = oldpos.getRegionIndex();
212 int hiddenCount = oldpos.getHiddenSoFar();
214 if (column < firstColumn)
219 else if ((index < hiddenColumns.size())
220 && (hiddenColumns.get(index)[0] <= column + hiddenCount))
222 // iterate from where we are now, if we're lucky we'll be close by
223 // (but still better than iterating from 0)
224 while ((index < hiddenColumns.size())
225 && (hiddenColumns.get(index)[0] <= column + hiddenCount))
227 int[] region = hiddenColumns.get(index);
228 hiddenCount += region[1] - region[0] + 1;
235 && (hiddenColumns.get(index - 1)[1] >= column + hiddenCount))
238 int[] region = hiddenColumns.get(index);
239 hiddenCount -= region[1] - region[0] + 1;
244 if (index != oldpos.getRegionIndex()
245 || hiddenCount != oldpos.getHiddenSoFar())
247 HiddenCursorPosition newpos = new HiddenCursorPosition(index,
249 cursorPos.compareAndSet(oldpos, newpos);