From ea2e22f20579b0ca244583058a291d0fcffbf6d5 Mon Sep 17 00:00:00 2001 From: kiramt Date: Wed, 31 Jan 2018 13:37:10 +0000 Subject: [PATCH] JAL-2759 Generalised HiddenColsIterator naming --- src/jalview/datamodel/HiddenColsIterator.java | 111 ------------------------ src/jalview/datamodel/HiddenColumns.java | 4 +- src/jalview/datamodel/RangeIterator.java | 114 +++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 113 deletions(-) delete mode 100644 src/jalview/datamodel/HiddenColsIterator.java create mode 100644 src/jalview/datamodel/RangeIterator.java diff --git a/src/jalview/datamodel/HiddenColsIterator.java b/src/jalview/datamodel/HiddenColsIterator.java deleted file mode 100644 index 0084912..0000000 --- a/src/jalview/datamodel/HiddenColsIterator.java +++ /dev/null @@ -1,111 +0,0 @@ -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 -{ - // current index in hiddenColumns - private int currentPosition = 0; - - // current column in hiddenColumns - private int[] currentRegion; - - // local copy or reference to hiddenColumns - private List localHidden; - - /** - * Unbounded constructor - */ - HiddenColsIterator(List 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 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 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); - } -} diff --git a/src/jalview/datamodel/HiddenColumns.java b/src/jalview/datamodel/HiddenColumns.java index 45dcab2..3e1f205 100644 --- a/src/jalview/datamodel/HiddenColumns.java +++ b/src/jalview/datamodel/HiddenColumns.java @@ -1008,7 +1008,7 @@ public class HiddenColumns try { LOCK.readLock().lock(); - return new HiddenColsIterator(hiddenColumns); + return new RangeIterator(hiddenColumns); } finally { LOCK.readLock().unlock(); @@ -1029,7 +1029,7 @@ public class HiddenColumns try { LOCK.readLock().lock(); - return new HiddenColsIterator(start, end, hiddenColumns); + return new RangeIterator(start, end, hiddenColumns); } finally { LOCK.readLock().unlock(); diff --git a/src/jalview/datamodel/RangeIterator.java b/src/jalview/datamodel/RangeIterator.java new file mode 100644 index 0000000..2c5d949 --- /dev/null +++ b/src/jalview/datamodel/RangeIterator.java @@ -0,0 +1,114 @@ +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 +{ + // current index in regionsList + private int currentPosition = 0; + + // current column in regionsList + private int[] currentRegion; + + // local copy or reference to regionsList + private List localRegions; + + /** + * Unbounded constructor + * + * @param regionsList + * list of regions to iterate over + */ + RangeIterator(List 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 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 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); + } +} -- 1.7.10.2