JAL-2446 merged to spike 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, AlignmentI alignment)
55   {
56     al = alignment;
57     current = firstrow;
58     last = lastrow;
59     hidden = al.getHiddenSequences();
60     while (last > current && hidden.isHidden(last))
61     {
62       last--;
63     }
64     current = firstrow;
65     while (current < last && hidden.isHidden(current))
66     {
67       current++;
68     }
69     next = current;
70   }
71
72   @Override
73   public boolean hasNext()
74   {
75     return next <= last;
76   }
77
78   @Override
79   public Integer next()
80   {
81     if (next > last)
82     {
83       throw new NoSuchElementException();
84     }
85     current = next;
86     do
87     {
88       next++;
89     } while (next <= last && hidden.isHidden(next));
90     return current;
91   }
92
93   @Override
94   public void remove()
95   {
96     throw new UnsupportedOperationException();
97   }
98 }
99