From: kiramt Date: Mon, 15 Jan 2018 14:54:35 +0000 (+0000) Subject: JAL-2759 First pass instantiate hiddenColumns collection in constructor X-Git-Tag: Release_2_10_4~55^2~1^2~37 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=60d67f3a980b29fd2039d2c3ad81ee6863f82548;p=jalview.git JAL-2759 First pass instantiate hiddenColumns collection in constructor --- diff --git a/src/jalview/datamodel/HiddenColsIterator.java b/src/jalview/datamodel/HiddenColsIterator.java index 610445c..55a3d8b 100644 --- a/src/jalview/datamodel/HiddenColsIterator.java +++ b/src/jalview/datamodel/HiddenColsIterator.java @@ -25,7 +25,7 @@ public class HiddenColsIterator implements Iterator */ HiddenColsIterator(List hiddenColumns) { - if (hiddenColumns != null) + if (!hiddenColumns.isEmpty()) { int last = hiddenColumns.get(hiddenColumns.size() - 1)[1]; init(0, last, hiddenColumns); diff --git a/src/jalview/datamodel/HiddenColumns.java b/src/jalview/datamodel/HiddenColumns.java index 83b1e36..718f64e 100644 --- a/src/jalview/datamodel/HiddenColumns.java +++ b/src/jalview/datamodel/HiddenColumns.java @@ -69,7 +69,7 @@ public class HiddenColumns * list of hidden column [start, end] ranges; the list is maintained in * ascending start column order */ - private List hiddenColumns; + private List hiddenColumns = new ArrayList<>(); /** * Constructor @@ -119,7 +119,6 @@ public class HiddenColumns LOCK.writeLock().lock(); if (copy != null) { - hiddenColumns = new ArrayList<>(); numColumns = 0; Iterator it = copy.getBoundedIterator(start, end); while (it.hasNext()) @@ -170,11 +169,7 @@ public class HiddenColumns int previndex = 0; int prevHiddenCount = 0; int regionindex = 0; - if (hiddenColumns == null) - { - hiddenColumns = new ArrayList<>(); - } - else + if (!hiddenColumns.isEmpty()) { // set up cursor reset values HiddenCursorPosition cursorPos = cursor.findRegionForColumn(start); @@ -329,21 +324,20 @@ public class HiddenColumns try { LOCK.writeLock().lock(); - if (hiddenColumns != null) + + Iterator it = hiddenColumns.iterator(); + while (it.hasNext()) { - Iterator it = hiddenColumns.iterator(); - while (it.hasNext()) + int[] region = it.next(); + for (int j = region[0]; j < region[1] + 1; j++) { - int[] region = it.next(); - for (int j = region[0]; j < region[1] + 1; j++) - { - sel.addElement(j); - } + sel.addElement(j); } - hiddenColumns = null; - cursor.resetCursor(hiddenColumns); - numColumns = 0; } + hiddenColumns.clear(); + cursor.resetCursor(hiddenColumns); + numColumns = 0; + } finally { LOCK.writeLock().unlock(); @@ -365,7 +359,7 @@ public class HiddenColumns { LOCK.writeLock().lock(); - if (hiddenColumns != null) + if (!hiddenColumns.isEmpty()) { int regionIndex = cursor.findRegionForColumn(start) .getRegionIndex(); @@ -386,7 +380,7 @@ public class HiddenColumns if (hiddenColumns.isEmpty()) { - hiddenColumns = null; + hiddenColumns.clear(); numColumns = 0; } else @@ -459,7 +453,7 @@ public class HiddenColumns BitSet hidden = new BitSet(); markHiddenRegions(hidden); hidden.andNot(gaps); - hiddenColumns = null; + hiddenColumns.clear(); this.hideColumns(hidden); // for each sequence in the alignment, except the profile sequence, @@ -585,20 +579,19 @@ public class HiddenColumns { LOCK.readLock().lock(); StringBuilder regionBuilder = new StringBuilder(); - if (hiddenColumns != null) + + Iterator it = hiddenColumns.iterator(); + while (it.hasNext()) { - Iterator it = hiddenColumns.iterator(); - while (it.hasNext()) + int[] range = it.next(); + regionBuilder.append(delimiter).append(range[0]).append(between) + .append(range[1]); + if (!it.hasNext()) { - int[] range = it.next(); - regionBuilder.append(delimiter).append(range[0]).append(between) - .append(range[1]); - if (!it.hasNext()) - { - regionBuilder.deleteCharAt(0); - } + regionBuilder.deleteCharAt(0); } } + return regionBuilder.toString(); } finally { @@ -617,7 +610,7 @@ public class HiddenColumns { LOCK.readLock().lock(); - if (numColumns == 0 && hiddenColumns != null) + if (numColumns == 0) { // numColumns is out of date, so recalculate int size = 0; @@ -677,12 +670,8 @@ public class HiddenColumns /* * check hidden columns are either both null, or match */ - if (this.hiddenColumns == null) - { - return (that.hiddenColumns == null); - } - if (that.hiddenColumns == null - || that.hiddenColumns.size() != this.hiddenColumns.size()) + + if (that.hiddenColumns.size() != this.hiddenColumns.size()) { return false; } @@ -719,7 +708,7 @@ public class HiddenColumns LOCK.readLock().lock(); int result = column; - if (hiddenColumns != null) + if (!hiddenColumns.isEmpty()) { result += cursor.findRegionForVisColumn(column).getHiddenSoFar(); } @@ -748,7 +737,7 @@ public class HiddenColumns LOCK.readLock().lock(); int result = hiddenColumn; - if (hiddenColumns != null) + if (!hiddenColumns.isEmpty()) { HiddenCursorPosition cursorPos = cursor .findRegionForColumn(hiddenColumn); @@ -854,7 +843,7 @@ public class HiddenColumns try { LOCK.readLock().lock(); - if (hiddenColumns != null) + if (!hiddenColumns.isEmpty()) { int index = cursor.findRegionForColumn(alPos).getRegionIndex(); if (index < hiddenColumns.size()) @@ -895,7 +884,7 @@ public class HiddenColumns { LOCK.readLock().lock(); - if (hiddenColumns != null) + if (!hiddenColumns.isEmpty()) { int index = cursor.findRegionForColumn(alPos).getRegionIndex(); @@ -964,7 +953,7 @@ public class HiddenColumns LOCK.readLock().lock(); int iSize = seqs.length; String[] selections = new String[iSize]; - if (hiddenColumns != null && hiddenColumns.size() > 0) + if (!hiddenColumns.isEmpty()) { for (int i = 0; i < iSize; i++) { @@ -1021,7 +1010,7 @@ public class HiddenColumns LOCK.readLock().lock(); int start = 0; - if (hiddenColumns == null || hiddenColumns.size() == 0) + if (hiddenColumns.isEmpty()) { return seq.findIndex(seq.getStart()) - 1; } @@ -1114,7 +1103,7 @@ public class HiddenColumns if (alignmentAnnotation != null && alignmentAnnotation.annotations != null) { - if (hiddenColumns != null && hiddenColumns.size() > 0) + if (!hiddenColumns.isEmpty()) { removeHiddenAnnotation(startFrom, endAt, alignmentAnnotation); } @@ -1200,7 +1189,7 @@ public class HiddenColumns // we don't use getSize()>0 here because it has to iterate over // the full hiddenColumns collection and so will be much slower - return hiddenColumns != null && hiddenColumns.size() > 0; + return (!hiddenColumns.isEmpty()); } finally { LOCK.readLock().unlock(); @@ -1216,7 +1205,7 @@ public class HiddenColumns try { LOCK.readLock().lock(); - return hiddenColumns != null && hiddenColumns.size() > 1; + return !hiddenColumns.isEmpty() && hiddenColumns.size() > 1; } finally { LOCK.readLock().unlock(); @@ -1284,10 +1273,7 @@ public class HiddenColumns try { LOCK.readLock().lock(); - if (hiddenColumns == null) - { - return; - } + Iterator it = hiddenColumns.iterator(); while (it.hasNext()) { @@ -1319,7 +1305,7 @@ public class HiddenColumns int[] lowestRange = new int[] { -1, -1 }; int[] higestRange = new int[] { -1, -1 }; - if (hiddenColumns == null) + if (hiddenColumns.isEmpty()) { return new int[] { startPos, endPos }; } @@ -1373,7 +1359,7 @@ public class HiddenColumns int[] reveal = null; - if (hiddenColumns != null) + if (!hiddenColumns.isEmpty()) { // look for a region ending just before adjres int regionindex = cursor.findRegionForColumn(adjres - 1) diff --git a/src/jalview/datamodel/HiddenColumnsCursor.java b/src/jalview/datamodel/HiddenColumnsCursor.java index 0fef622..a875f87 100644 --- a/src/jalview/datamodel/HiddenColumnsCursor.java +++ b/src/jalview/datamodel/HiddenColumnsCursor.java @@ -20,6 +20,7 @@ */ package jalview.datamodel; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicReference; @@ -28,7 +29,7 @@ public class HiddenColumnsCursor // absolute position of first hidden column private int firstColumn; - private List hiddenColumns; + private List hiddenColumns = new ArrayList<>(); // AtomicReference to hold the current region index and hidden column count // Could be done with synchronisation but benchmarking shows this way is 2x @@ -70,7 +71,7 @@ public class HiddenColumnsCursor int hiddencount) { hiddenColumns = hiddenCols; - if ((hiddenCols != null) && (!hiddenCols.isEmpty())) + if (!hiddenCols.isEmpty()) { firstColumn = hiddenColumns.get(0)[0]; HiddenCursorPosition oldpos = cursorPos.get(); @@ -93,7 +94,7 @@ public class HiddenColumnsCursor protected void updateForDeletedRegion(List hiddenCols) { - if ((hiddenCols != null) && (!hiddenCols.isEmpty())) + if (!hiddenCols.isEmpty()) { // if there is a region to the right of the current region, // nothing changes; otherwise @@ -130,7 +131,7 @@ public class HiddenColumnsCursor */ protected HiddenCursorPosition findRegionForColumn(int column) { - if (hiddenColumns == null) + if (hiddenColumns.isEmpty()) { return null; } @@ -202,7 +203,7 @@ public class HiddenColumnsCursor */ protected HiddenCursorPosition findRegionForVisColumn(int column) { - if (hiddenColumns == null) + if (hiddenColumns.isEmpty()) { return null; }