JAL-2418 source formatting
[jalview.git] / src / jalview / datamodel / VisibleColsIterator.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.List;
25 import java.util.NoSuchElementException;
26
27 /**
28  * An iterator which iterates over all visible columns in an alignment
29  * 
30  * @author kmourao
31  *
32  */
33 public class VisibleColsIterator implements Iterator<Integer>
34 {
35   private int last;
36
37   private int current;
38
39   private int next;
40
41   private List<int[]> hidden;
42
43   private int lasthiddenregion;
44
45   public VisibleColsIterator(int firstcol, int lastcol,
46           HiddenColumns hiddenCols)
47   {
48     last = lastcol;
49     current = firstcol;
50     next = firstcol;
51     hidden = hiddenCols.getHiddenColumnsCopy();
52     lasthiddenregion = -1;
53
54     if (hidden != null)
55     {
56       int i = 0;
57       for (i = 0; i < hidden.size(); ++i)
58       {
59         if (current >= hidden.get(i)[0] && current <= hidden.get(i)[1])
60         {
61           // current is hidden, move to right
62           current = hidden.get(i)[1] + 1;
63           next = current;
64         }
65         if (current < hidden.get(i)[0])
66         {
67           break;
68         }
69       }
70       lasthiddenregion = i - 1;
71
72       for (i = hidden.size() - 1; i >= 0; --i)
73       {
74         if (last >= hidden.get(i)[0] && last <= hidden.get(i)[1])
75         {
76           // last is hidden, move to left
77           last = hidden.get(i)[0] - 1;
78         }
79         if (last > hidden.get(i)[1])
80         {
81           break;
82         }
83       }
84     }
85   }
86
87   @Override
88   public boolean hasNext()
89   {
90     return next <= last;
91   }
92
93   @Override
94   public Integer next()
95   {
96     if (next > last)
97     {
98       throw new NoSuchElementException();
99     }
100     current = next;
101     if ((hidden != null) && (lasthiddenregion + 1 < hidden.size()))
102     {
103       // still some more hidden regions
104       if (next + 1 < hidden.get(lasthiddenregion + 1)[0])
105       {
106         // next+1 is still before the next hidden region
107         next++;
108       }
109       else if ((next + 1 >= hidden.get(lasthiddenregion + 1)[0])
110               && (next + 1 <= hidden.get(lasthiddenregion + 1)[1]))
111       {
112         // next + 1 is in the next hidden region
113         next = hidden.get(lasthiddenregion + 1)[1] + 1;
114         lasthiddenregion++;
115       }
116     }
117     else
118     {
119       // finished with hidden regions, just increment normally
120       next++;
121     }
122     return current;
123   }
124
125   @Override
126   public void remove()
127   {
128     throw new UnsupportedOperationException();
129   }
130 }