2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.datamodel;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
28 * An iterator which iterates over a list of ranges. Works with a copy of the
29 * collection of ranges.
31 public class RangeIterator implements Iterator<int[]>
33 // current index in rangeList
34 private int currentPosition = 0;
36 // current range in rangeList
37 private int[] currentRange;
39 // local copy or reference to rangeList
40 private List<int[]> localRanges;
43 * Unbounded constructor
46 * list of ranges to iterate over
48 RangeIterator(List<int[]> rangeList)
50 if (!rangeList.isEmpty())
52 int last = rangeList.get(rangeList.size() - 1)[1];
53 init(0, last, rangeList);
57 init(0, 0, rangeList);
62 * Construct an iterator over rangeList bounded at [lowerBound,upperBound]
65 * lower bound to iterate from
67 * upper bound to iterate to
69 * list of ranges to iterate over
71 RangeIterator(int lowerBound, int upperBound,
72 List<int[]> rangeList)
74 init(lowerBound, upperBound, rangeList);
78 * Construct an iterator over rangeList bounded at [lowerBound,upperBound]
81 * lower bound to iterate from
83 * upper bound to iterate to
85 private void init(int lowerBound, int upperBound,
86 List<int[]> rangeList)
88 int start = lowerBound;
91 if (rangeList != null)
93 localRanges = new ArrayList<>();
95 // iterate until a range overlaps with [start,end]
97 while ((i < rangeList.size()) && (rangeList.get(i)[1] < start))
102 // iterate from start to end, adding each range. Positions are
103 // absolute, and all ranges which *overlap* [start,end] are added.
104 while (i < rangeList.size() && (rangeList.get(i)[0] <= end))
106 int[] rh = rangeList.get(i);
107 int[] cp = new int[2];
108 System.arraycopy(rh, 0, cp, 0, rh.length);
116 public boolean hasNext()
118 return (localRanges != null) && (currentPosition < localRanges.size());
124 currentRange = localRanges.get(currentPosition);
132 localRanges.remove(--currentPosition);