0185978a05df45086c8c6b2eaf7f900c9c3c3a0f
[jalview.git] / src / jalview / datamodel / VisibleContigsIterator.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.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 /**
28  * An iterator which iterates over visible regions in a range. Provides a
29  * special "endsAtHidden" indicator to allow callers to determine if the final
30  * visible column is adjacent to a hidden region.
31  */
32 public class VisibleContigsIterator implements Iterator<int[]>
33 {
34   private List<int[]> vcontigs = new ArrayList<>();
35
36   private int currentPosition = 0;
37
38   private boolean endsAtHidden = false;
39
40   VisibleContigsIterator(int start, int end,
41           List<int[]> hiddenColumns)
42   {
43     if (hiddenColumns != null && hiddenColumns.size() > 0)
44     {
45       int vstart = start;
46       int hideStart;
47       int hideEnd;
48
49       for (int[] region : hiddenColumns)
50       {
51         endsAtHidden = false;
52         hideStart = region[0];
53         hideEnd = region[1];
54
55         // navigate to start
56         if (hideEnd < vstart)
57         {
58           continue;
59         }
60         if (hideStart > vstart)
61         {
62           if (end - 1 > hideStart - 1)
63           {
64             int[] contig = new int[] { vstart, hideStart - 1 };
65             vcontigs.add(contig);
66             endsAtHidden = true;
67           }
68           else
69           {
70             int[] contig = new int[] { vstart, end - 1 };
71             vcontigs.add(contig);
72           }
73         }
74         vstart = hideEnd + 1;
75
76         // exit if we're past the end
77         if (vstart >= end)
78         {
79           break;
80         }
81       }
82
83       if (vstart < end)
84       {
85         int[] contig = new int[] { vstart, end - 1 };
86         vcontigs.add(contig);
87         endsAtHidden = false;
88       }
89     }
90     else
91     {
92       int[] contig = new int[] { start, end - 1 };
93       vcontigs.add(contig);
94     }
95   }
96
97   @Override
98   public boolean hasNext()
99   {
100     return (currentPosition < vcontigs.size());
101   }
102
103   @Override
104   public int[] next()
105   {
106     int[] result = vcontigs.get(currentPosition);
107     currentPosition++;
108     return result;
109   }
110
111   public boolean endsAtHidden()
112   {
113     return endsAtHidden;
114   }
115 }
116