Merge branch 'releases/Release_2_11_3_Branch'
[jalview.git] / src / jalview / datamodel / VisibleRowsIterator.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.Iterator;
24 import java.util.NoSuchElementException;
25
26 /**
27  * An iterator which iterates over all visible rows in an alignment
28  * 
29  * @author kmourao
30  *
31  */
32 public class VisibleRowsIterator implements Iterator<Integer>
33 {
34   private int last;
35
36   private int current;
37
38   private int next;
39
40   private HiddenSequences hidden;
41
42   private AlignmentI al;
43
44   /**
45    * Create an iterator for all visible rows in the alignment
46    * 
47    * @param firstrow
48    *          absolute row index to start from
49    * @param lastrow
50    *          absolute row index to end at
51    * @param alignment
52    *          alignment to work with
53    */
54   public VisibleRowsIterator(int firstrow, int lastrow,
55           AlignmentI alignment)
56   {
57     al = alignment;
58     current = firstrow;
59     last = lastrow;
60     hidden = al.getHiddenSequences();
61     while (last > current && hidden.isHidden(last))
62     {
63       last--;
64     }
65     current = firstrow;
66     while (current < last && hidden.isHidden(current))
67     {
68       current++;
69     }
70     next = current;
71   }
72
73   @Override
74   public boolean hasNext()
75   {
76     return next <= last;
77   }
78
79   @Override
80   public Integer next()
81   {
82     if (next > last)
83     {
84       throw new NoSuchElementException();
85     }
86     current = next;
87     do
88     {
89       next++;
90     } while (next <= last && hidden.isHidden(next));
91     return current;
92   }
93
94   @Override
95   public void remove()
96   {
97     throw new UnsupportedOperationException();
98   }
99 }