JAL-2759 Tidy up javadoc and naming in HiddenColumnsCursor after review
[jalview.git] / src / jalview / datamodel / HiddenColumnsCursor.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.datamodel;
22
23 import java.util.List;
24 import java.util.concurrent.atomic.AtomicReference;
25
26 public class HiddenColumnsCursor
27 {
28   // absolute position of first hidden column
29   private int firstColumn;
30
31   private List<int[]> hiddenColumns;
32
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
35   // faster
36   private final AtomicReference<HiddenCursorPosition> cursorPos = new AtomicReference<>(
37           new HiddenCursorPosition(0, 0));
38
39   protected HiddenColumnsCursor()
40   {
41
42   }
43
44   /**
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
48    * already exist.
49    * 
50    * @param hiddenCols
51    *          new hidden columns collection
52    */
53   protected void resetCursor(List<int[]> hiddenCols)
54   {
55     resetCursor(hiddenCols, 0, 0);
56   }
57
58   /**
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.
61    * 
62    * @param hiddenCols
63    *          new hidden columns collection
64    * @param index
65    *          cursor index to reset to
66    * @param hiddencount
67    *          hidden columns count to reset to
68    */
69   protected void resetCursor(List<int[]> hiddenCols, int index,
70           int hiddencount)
71   {
72     hiddenColumns = hiddenCols;
73     if ((hiddenCols != null) && (!hiddenCols.isEmpty()))
74     {
75       firstColumn = hiddenColumns.get(0)[0];
76       HiddenCursorPosition oldpos = cursorPos.get();
77       HiddenCursorPosition newpos = new HiddenCursorPosition(index,
78               hiddencount);
79       cursorPos.compareAndSet(oldpos, newpos);
80     }
81   }
82
83   /**
84    * Delete the region the cursor is currently at. Avoids having to reset the
85    * cursor just because we deleted a region.
86    * 
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.
90    *
91    * @param hiddenCols
92    */
93   protected void updateForDeletedRegion(List<int[]> hiddenCols)
94   {
95
96     if ((hiddenCols != null) && (!hiddenCols.isEmpty()))
97     {
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();
103
104       int index = oldpos.getRegionIndex();
105       if (index >= hiddenColumns.size() - 1)
106       {
107         // deleted last region, index is now end of alignment
108         index = hiddenCols.size();
109
110         HiddenCursorPosition newpos = new HiddenCursorPosition(index,
111                 oldpos.getHiddenSoFar());
112         cursorPos.compareAndSet(oldpos, newpos);
113       }
114     }
115     hiddenColumns = hiddenCols;
116   }
117
118   /**
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.
125    * 
126    * @param column
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)
130    */
131   protected HiddenCursorPosition findRegionForColumn(int column)
132   {
133     if (hiddenColumns == null)
134     {
135       return null;
136     }
137
138     HiddenCursorPosition oldpos = cursorPos.get();
139     int index = oldpos.getRegionIndex();
140     int hiddenCount = oldpos.getHiddenSoFar();
141
142     if (index == hiddenColumns.size())
143     {
144       // went past the end of hiddenColumns collection last time
145       index--;
146       int[] region = hiddenColumns.get(index);
147       hiddenCount -= region[1] - region[0] + 1;
148     }
149
150     // this if statement excludes case where column is in current region
151     // - no changes needed
152     if (column < firstColumn)
153     {
154       index = 0;
155       hiddenCount = 0;
156     }
157     // column is after current region
158     else if (column > hiddenColumns.get(index)[1])
159     {
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]))
166       {
167         int[] region = hiddenColumns.get(index);
168         hiddenCount += region[1] - region[0] + 1;
169         index++;
170       }
171     }
172     // column is before current region
173     else if (column < hiddenColumns.get(index)[0])
174     {
175       // column is before or in the previous region
176       while ((index > 0) && (hiddenColumns.get(index - 1)[1] >= column))
177       {
178         index--;
179         int[] region = hiddenColumns.get(index);
180         hiddenCount -= region[1] - region[0] + 1;
181       }
182     }
183
184     if (index != oldpos.getRegionIndex()
185             || hiddenCount != oldpos.getHiddenSoFar())
186     {
187       HiddenCursorPosition newpos = new HiddenCursorPosition(index,
188               hiddenCount);
189       cursorPos.compareAndSet(oldpos, newpos);
190       return newpos;
191     }
192     return oldpos;
193   }
194
195   /**
196    * Get the cursor pointing to the hidden region just after a visible column
197    * 
198    * @param column
199    *          index of column in *visible* alignment (therefore by definition
200    *          column is visible)
201    * @return cursor pointing to hidden region to the right of the column
202    */
203   protected HiddenCursorPosition findRegionForVisColumn(int column)
204   {
205     if (hiddenColumns == null)
206     {
207       return null;
208     }
209
210     HiddenCursorPosition oldpos = cursorPos.get();
211     int index = oldpos.getRegionIndex();
212     int hiddenCount = oldpos.getHiddenSoFar();
213
214     if (column < firstColumn)
215     {
216       index = 0;
217       hiddenCount = 0;
218     }
219     else if ((index < hiddenColumns.size())
220             && (hiddenColumns.get(index)[0] <= column + hiddenCount))
221     {
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))
226       {
227         int[] region = hiddenColumns.get(index);
228         hiddenCount += region[1] - region[0] + 1;
229         index++;
230       }
231     }
232     else
233     {
234       while ((index > 0)
235               && (hiddenColumns.get(index - 1)[1] >= column + hiddenCount))
236       {
237         index--;
238         int[] region = hiddenColumns.get(index);
239         hiddenCount -= region[1] - region[0] + 1;
240       }
241
242     }
243
244     if (index != oldpos.getRegionIndex()
245             || hiddenCount != oldpos.getHiddenSoFar())
246     {
247       HiddenCursorPosition newpos = new HiddenCursorPosition(index,
248               hiddenCount);
249       cursorPos.compareAndSet(oldpos, newpos);
250       return newpos;
251     }
252     return oldpos;
253   }
254 }