JAL-2759 update benchmark code
[jalview.git] / src / jalview / datamodel / RegionsIterator.java
1 package jalview.datamodel;
2
3 import java.util.Iterator;
4 import java.util.List;
5
6 /**
7  * A local iterator which iterates over hidden column regions in a range.
8  * 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 RegionsIterator implements Iterator<int[]>
13 {
14   // start position to iterate from
15   private int start;
16
17   // end position to iterate to
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 int[] currentRegion = null;
27
28   private int removedIndex = -1;
29
30   private final List<int[]> hiddenColumns;
31
32   // Constructor with bounds
33   RegionsIterator(int lowerBound, int upperBound, List<int[]> hiddenCols,
34           HiddenColumnsCursor cursor)
35   {
36     start = lowerBound;
37     end = upperBound;
38     hiddenColumns = hiddenCols;
39
40     if (hiddenColumns != null)
41     {
42       // TODO remove whole class?
43       // commented out to compile
44       // currentPosition = cursor.findRegionForColumn(start);
45
46       if (currentPosition < hiddenColumns.size())
47       {
48         nextRegion = hiddenColumns.get(currentPosition);
49       }
50     }
51   }
52
53   @Override
54   public boolean hasNext()
55   {
56     return (hiddenColumns != null) && (nextRegion != null)
57             && (nextRegion[0] <= end);
58   }
59
60   @Override
61   public int[] next()
62   {
63     currentRegion = nextRegion;
64     currentPosition++;
65     if (currentPosition < hiddenColumns.size())
66     {
67       nextRegion = hiddenColumns.get(currentPosition);
68     }
69     else
70     {
71       nextRegion = null;
72     }
73     return currentRegion;
74   }
75
76   @Override
77   public void remove()
78   {
79     if ((currentRegion != null) && (removedIndex != currentPosition))
80     {
81       currentPosition--;
82       hiddenColumns.subList(currentPosition, currentPosition + 1).clear();
83       removedIndex = currentPosition;
84     }
85     else
86     {
87       // already removed element last returned by next()
88       // or next() has not yet been called
89       throw new IllegalStateException();
90     }
91   }
92
93 }