Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.datamodel

File RangeIterator.java

 

Coverage histogram

../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
23
6
1
134
67
12
0.52
3.83
6
2

Classes

Class Line # Actions
RangeIterator 31 23 12 3
0.918918991.9%
 

Contributing tests

This file is covered by 35 tests. .

Source view

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 a list of ranges. Works with a copy of the
29    * collection of ranges.
30    */
 
31    public class RangeIterator implements Iterator<int[]>
32    {
33    // current index in rangeList
34    private int currentPosition = 0;
35   
36    // current range in rangeList
37    private int[] currentRange;
38   
39    // local copy or reference to rangeList
40    private List<int[]> localRanges;
41   
42    /**
43    * Unbounded constructor
44    *
45    * @param rangeList
46    * list of ranges to iterate over
47    */
 
48  184 toggle RangeIterator(List<int[]> rangeList)
49    {
50  184 if (!rangeList.isEmpty())
51    {
52  111 int last = rangeList.get(rangeList.size() - 1)[1];
53  111 init(0, last, rangeList);
54    }
55    else
56    {
57  73 init(0, 0, rangeList);
58    }
59    }
60   
61    /**
62    * Construct an iterator over rangeList bounded at [lowerBound,upperBound]
63    *
64    * @param lowerBound
65    * lower bound to iterate from
66    * @param upperBound
67    * upper bound to iterate to
68    * @param rangeList
69    * list of ranges to iterate over
70    */
 
71  47 toggle RangeIterator(int lowerBound, int upperBound,
72    List<int[]> rangeList)
73    {
74  47 init(lowerBound, upperBound, rangeList);
75    }
76   
77    /**
78    * Construct an iterator over rangeList bounded at [lowerBound,upperBound]
79    *
80    * @param lowerBound
81    * lower bound to iterate from
82    * @param upperBound
83    * upper bound to iterate to
84    */
 
85  231 toggle private void init(int lowerBound, int upperBound,
86    List<int[]> rangeList)
87    {
88  231 int start = lowerBound;
89  231 int end = upperBound;
90   
91  231 if (rangeList != null)
92    {
93  231 localRanges = new ArrayList<>();
94   
95    // iterate until a range overlaps with [start,end]
96  231 int i = 0;
97  233 while ((i < rangeList.size()) && (rangeList.get(i)[1] < start))
98    {
99  2 i++;
100    }
101   
102    // iterate from start to end, adding each range. Positions are
103    // absolute, and all ranges which *overlap* [start,end] are added.
104  472 while (i < rangeList.size() && (rangeList.get(i)[0] <= end))
105    {
106  241 int[] rh = rangeList.get(i);
107  241 int[] cp = new int[2];
108  241 System.arraycopy(rh, 0, cp, 0, rh.length);
109  241 localRanges.add(cp);
110  241 i++;
111    }
112    }
113    }
114   
 
115  335 toggle @Override
116    public boolean hasNext()
117    {
118  335 return (localRanges != null) && (currentPosition < localRanges.size());
119    }
120   
 
121  235 toggle @Override
122    public int[] next()
123    {
124  235 currentRange = localRanges.get(currentPosition);
125  235 currentPosition++;
126  235 return currentRange;
127    }
128   
 
129  0 toggle @Override
130    public void remove()
131    {
132  0 localRanges.remove(--currentPosition);
133    }
134    }