+++ /dev/null
-package jalview.datamodel;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-/**
- * An iterator which iterates over hidden column regions in a range. Works with
- * a copy of the hidden columns collection. Intended to be used by callers
- * OUTSIDE of HiddenColumns.
- */
-public class HiddenColsIterator implements Iterator<int[]>
-{
- // current index in hiddenColumns
- private int currentPosition = 0;
-
- // current column in hiddenColumns
- private int[] currentRegion;
-
- // local copy or reference to hiddenColumns
- private List<int[]> localHidden;
-
- /**
- * Unbounded constructor
- */
- HiddenColsIterator(List<int[]> hiddenColumns)
- {
- if (!hiddenColumns.isEmpty())
- {
- int last = hiddenColumns.get(hiddenColumns.size() - 1)[1];
- init(0, last, hiddenColumns);
- }
- else
- {
- init(0, 0, hiddenColumns);
- }
- }
-
- /**
- * Construct an iterator over hiddenColums bounded at [lowerBound,upperBound]
- *
- * @param lowerBound
- * lower bound to iterate from
- * @param upperBound
- * upper bound to iterate to
- */
- HiddenColsIterator(int lowerBound, int upperBound,
- List<int[]> hiddenColumns)
- {
- init(lowerBound, upperBound, hiddenColumns);
- }
-
- /**
- * Construct an iterator over hiddenColums bounded at [lowerBound,upperBound]
- *
- * @param lowerBound
- * lower bound to iterate from
- * @param upperBound
- * upper bound to iterate to
- */
- private void init(int lowerBound, int upperBound,
- List<int[]> hiddenColumns)
- {
- int start = lowerBound;
- int end = upperBound;
-
- if (hiddenColumns != null)
- {
- localHidden = new ArrayList<>();
-
- // iterate until a region overlaps with [start,end]
- int i = 0;
- while ((i < hiddenColumns.size())
- && (hiddenColumns.get(i)[1] < start))
- {
- i++;
- }
-
- // iterate from start to end, adding each hidden region. Positions are
- // absolute, and all regions which *overlap* [start,end] are added.
- while (i < hiddenColumns.size() && (hiddenColumns.get(i)[0] <= end))
- {
- int[] rh = hiddenColumns.get(i);
- int[] cp = new int[2];
- System.arraycopy(rh, 0, cp, 0, rh.length);
- localHidden.add(cp);
- i++;
- }
- }
- }
-
- @Override
- public boolean hasNext()
- {
- return (localHidden != null) && (currentPosition < localHidden.size());
- }
-
- @Override
- public int[] next()
- {
- currentRegion = localHidden.get(currentPosition);
- currentPosition++;
- return currentRegion;
- }
-
- @Override
- public void remove()
- {
- localHidden.remove(--currentPosition);
- }
-}
try
{
LOCK.readLock().lock();
- return new HiddenColsIterator(hiddenColumns);
+ return new RangeIterator(hiddenColumns);
} finally
{
LOCK.readLock().unlock();
try
{
LOCK.readLock().lock();
- return new HiddenColsIterator(start, end, hiddenColumns);
+ return new RangeIterator(start, end, hiddenColumns);
} finally
{
LOCK.readLock().unlock();
--- /dev/null
+package jalview.datamodel;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * An iterator which iterates over regions in a range. Works with a copy of the
+ * collection of regions.
+ */
+public class RangeIterator implements Iterator<int[]>
+{
+ // current index in regionsList
+ private int currentPosition = 0;
+
+ // current column in regionsList
+ private int[] currentRegion;
+
+ // local copy or reference to regionsList
+ private List<int[]> localRegions;
+
+ /**
+ * Unbounded constructor
+ *
+ * @param regionsList
+ * list of regions to iterate over
+ */
+ RangeIterator(List<int[]> regionsList)
+ {
+ if (!regionsList.isEmpty())
+ {
+ int last = regionsList.get(regionsList.size() - 1)[1];
+ init(0, last, regionsList);
+ }
+ else
+ {
+ init(0, 0, regionsList);
+ }
+ }
+
+ /**
+ * Construct an iterator over regionsList bounded at [lowerBound,upperBound]
+ *
+ * @param lowerBound
+ * lower bound to iterate from
+ * @param upperBound
+ * upper bound to iterate to
+ * @param regionsList
+ * list of regions to iterate over
+ */
+ RangeIterator(int lowerBound, int upperBound,
+ List<int[]> regionsList)
+ {
+ init(lowerBound, upperBound, regionsList);
+ }
+
+ /**
+ * Construct an iterator over regionsList bounded at [lowerBound,upperBound]
+ *
+ * @param lowerBound
+ * lower bound to iterate from
+ * @param upperBound
+ * upper bound to iterate to
+ */
+ private void init(int lowerBound, int upperBound,
+ List<int[]> regionsList)
+ {
+ int start = lowerBound;
+ int end = upperBound;
+
+ if (regionsList != null)
+ {
+ localRegions = new ArrayList<>();
+
+ // iterate until a region overlaps with [start,end]
+ int i = 0;
+ while ((i < regionsList.size()) && (regionsList.get(i)[1] < start))
+ {
+ i++;
+ }
+
+ // iterate from start to end, adding each region. Positions are
+ // absolute, and all regions which *overlap* [start,end] are added.
+ while (i < regionsList.size() && (regionsList.get(i)[0] <= end))
+ {
+ int[] rh = regionsList.get(i);
+ int[] cp = new int[2];
+ System.arraycopy(rh, 0, cp, 0, rh.length);
+ localRegions.add(cp);
+ i++;
+ }
+ }
+ }
+
+ @Override
+ public boolean hasNext()
+ {
+ return (localRegions != null) && (currentPosition < localRegions.size());
+ }
+
+ @Override
+ public int[] next()
+ {
+ currentRegion = localRegions.get(currentPosition);
+ currentPosition++;
+ return currentRegion;
+ }
+
+ @Override
+ public void remove()
+ {
+ localRegions.remove(--currentPosition);
+ }
+}