JAL-2759 Updated test
[jalview.git] / src / jalview / datamodel / ReverseRegionsIterator.java
1 package jalview.datamodel;
2
3 import java.util.Iterator;
4 import java.util.List;
5
6 /**
7  * A local iterator which reverse iterates over hidden column regions in a
8  * range. Intended for use ONLY within the HiddenColumns class, because it works
9  * directly with the hiddenColumns collection without locking (callers should
10  * lock hiddenColumns).
11  */
12 public class ReverseRegionsIterator implements Iterator<int[]>
13 {
14   // start position to iterate to
15   private int start;
16
17   // end position to iterate from
18   private int end;
19
20   // current index in hiddenColumns
21   private int currentPosition = 0;
22
23   // current column in hiddenColumns
24   private int[] nextRegion = null;
25
26   private final List<int[]> hiddenColumns;
27
28   // Constructor with bounds
29   ReverseRegionsIterator(int lowerBound, int upperBound,
30           List<int[]> hiddenCols)
31   {
32     hiddenColumns = hiddenCols;
33     start = lowerBound;
34     end = upperBound;
35
36     if (hiddenColumns != null)
37     {
38       // iterate until a region overlaps with [start,end]
39       currentPosition = hiddenColumns.size() - 1;
40       while (currentPosition >= 0
41               && hiddenColumns.get(currentPosition)[1] > end)
42       {
43         currentPosition--;
44       }
45       if (currentPosition >= 0)
46       {
47         nextRegion = hiddenColumns.get(currentPosition);
48       }
49     }
50   }
51
52   @Override
53   public boolean hasNext()
54   {
55     return (hiddenColumns != null) && (nextRegion != null)
56             && (nextRegion[1] >= start);
57   }
58
59   @Override
60   public int[] next()
61   {
62     int[] region = nextRegion;
63     currentPosition--;
64     if (currentPosition >= 0)
65     {
66       nextRegion = hiddenColumns.get(currentPosition);
67     }
68     else
69     {
70       nextRegion = null;
71     }
72     return region;
73   }
74
75 }