X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fdatamodel%2FColumnSelection.java;h=d651c0bfb7e67dfecbbac0d1161b25998b7b51d6;hb=refs%2Fheads%2Freleases%2FRelease_2_10_0_Branch;hp=7ef2a68d3a2aa8c1d0a59f0524b08b4a00291094;hpb=585a6abef0a5beef8b5247d9b44105dbf389ba45;p=jalview.git diff --git a/src/jalview/datamodel/ColumnSelection.java b/src/jalview/datamodel/ColumnSelection.java index 7ef2a68..d651c0b 100644 --- a/src/jalview/datamodel/ColumnSelection.java +++ b/src/jalview/datamodel/ColumnSelection.java @@ -45,19 +45,52 @@ public class ColumnSelection /* * list of selected columns (ordered by selection order, not column order) */ - private List order = new ArrayList(); + private List order; + + /* + * an unmodifiable view of the selected columns list + */ + private List _uorder; /** * bitfield for column selection - allows quick lookup */ - private BitSet selected = new BitSet(); + private BitSet selected; + + /** + * Constructor + */ + IntList() + { + order = new ArrayList(); + _uorder = Collections.unmodifiableList(order); + selected = new BitSet(); + } + + /** + * Copy constructor + * + * @param other + */ + IntList(IntList other) + { + this(); + if (other != null) + { + int j = other.size(); + for (int i = 0; i < j; i++) + { + add(other.elementAt(i)); + } + } + } /** * adds a new column i to the selection - only if i is not already selected * * @param i */ - public void add(int i) + void add(int i) { if (!selected.get(i)) { @@ -66,13 +99,13 @@ public class ColumnSelection } } - public void clear() + void clear() { order.clear(); selected.clear(); } - public void remove(int col) + void remove(int col) { Integer colInt = new Integer(col); @@ -87,22 +120,27 @@ public class ColumnSelection } } - public boolean contains(Integer colInt) + boolean contains(Integer colInt) { return selected.get(colInt); } - public boolean isEmpty() + boolean isEmpty() { return order.isEmpty(); } - public List getList() + /** + * Returns a read-only view of the selected columns list + * + * @return + */ + List getList() { - return order; + return _uorder; } - public int size() + int size() { return order.size(); } @@ -113,7 +151,7 @@ public class ColumnSelection * @param i * @return */ - public int elementAt(int i) + int elementAt(int i) { return order.get(i); } @@ -156,7 +194,7 @@ public class ColumnSelection * @param change * - delta for shift */ - public void compensateForEdits(int start, int change) + void compensateForEdits(int start, int change) { BitSet mask = new BitSet(); for (int i = 0; i < order.size(); i++) @@ -175,17 +213,17 @@ public class ColumnSelection selected.or(mask); } - public boolean isSelected(int column) + boolean isSelected(int column) { return selected.get(column); } - public int getMaxColumn() + int getMaxColumn() { return selected.length() - 1; } - public int getMinColumn() + int getMinColumn() { return selected.get(0) ? 0 : selected.nextSetBit(0); } @@ -193,7 +231,7 @@ public class ColumnSelection /** * @return a series of selection intervals along the range */ - public List getRanges() + List getRanges() { List rlist = new ArrayList(); if (selected.isEmpty()) @@ -288,14 +326,18 @@ public class ColumnSelection } /** - * Returns a list of selected columns. The list contains no duplicates but is - * not necessarily ordered. It also may include columns hidden from the - * current view. This returns a copy of the actual list, and changes to the - * copy will not affect the selection. + * Returns a read-only view of the (possibly empty) list of selected columns + *

+ * The list contains no duplicates but is not necessarily ordered. It also may + * include columns hidden from the current view. To modify (for example sort) + * the list, you should first make a copy. + *

+ * The list is not thread-safe: iterating over it could result in + * ConcurrentModificationException if it is modified by another thread. */ public List getSelected() { - return new ArrayList(selection.getList()); + return selection.getList(); } /** @@ -949,14 +991,7 @@ public class ColumnSelection { if (copy != null) { - if (copy.selection != null) - { - selection = new IntList(); - for (int i = 0, j = copy.selection.size(); i < j; i++) - { - selection.add(copy.selection.elementAt(i)); - } - } + selection = new IntList(copy.selection); if (copy.hiddenColumns != null) { hiddenColumns = new Vector(copy.hiddenColumns.size()); @@ -1113,9 +1148,9 @@ public class ColumnSelection */ public int[] locateVisibleBoundsOfSequence(SequenceI seq) { - int fpos=seq.getStart(),lpos= seq.getEnd(); + int fpos = seq.getStart(), lpos = seq.getEnd(); int start = 0; - + if (hiddenColumns == null || hiddenColumns.size() == 0) { int ifpos = seq.findIndex(fpos) - 1, ilpos = seq.findIndex(lpos) - 1;