JAL-2591 Hidden columns regions can be output to string + test
authorkiramt <k.mourao@dundee.ac.uk>
Wed, 7 Jun 2017 10:32:20 +0000 (11:32 +0100)
committerkiramt <k.mourao@dundee.ac.uk>
Wed, 7 Jun 2017 10:32:20 +0000 (11:32 +0100)
src/jalview/datamodel/HiddenColumns.java
src/jalview/io/AnnotationFile.java
src/jalview/io/JSONFile.java
test/jalview/datamodel/HiddenColumnsTest.java

index 1e1a58b..0036402 100644 (file)
@@ -33,6 +33,36 @@ public class HiddenColumns implements Iterable<int[]>
   }
 
   /**
+   * Output regions data as a string. String is in the format:
+   * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
+   * 
+   * @param delimiter
+   *          string to delimit regions
+   * @param betweenstring
+   *          to put between start and end region values
+   * @return regions formatted according to delimiter and between strings
+   */
+  public String regionsToString(String delimiter, String between)
+  {
+    try
+    {
+      lock.readLock().lock();
+      StringBuilder regionBuilder = new StringBuilder();
+      for (int[] range : hiddenColumns)
+      {
+        regionBuilder.append(delimiter).append(range[0]).append(between)
+                .append(range[1]);
+      }
+
+      regionBuilder.deleteCharAt(0);
+      return regionBuilder.toString();
+    } finally
+    {
+      lock.readLock().unlock();
+    }
+  }
+
+  /**
    * Find the number of hidden columns
    * 
    * @return number of hidden columns
index 2b78cb6..b669ee5 100755 (executable)
@@ -171,21 +171,9 @@ public class AnnotationFile
     if (cs != null && cs.hasHiddenColumns())
     {
       text.append("VIEW_HIDECOLS\t");
-      boolean comma = false;
-      for (int[] r : cs.getHiddenRegions())
-      {
-        if (!comma)
-        {
-          comma = true;
-        }
-        else
-        {
-          text.append(",");
-        }
-        text.append(r[0]);
-        text.append("-");
-        text.append(r[1]);
-      }
+
+      String regions = cs.regionsToString(",", "-");
+      text.append(regions);
       text.append("\n");
     }
     // TODO: allow efficient recovery of annotation data shown in several
index a48ee33..c293cd4 100644 (file)
@@ -279,15 +279,8 @@ public class JSONFile extends AlignFile implements ComplexAlignFile
     // hidden column business
     if (getViewport().hasHiddenColumns())
     {
-      StringBuilder hiddenColsBuilder = new StringBuilder();
-      for (int[] range : getViewport().getAlignment().getHiddenColumns())
-      {
-        hiddenColsBuilder.append(";").append(range[0]).append("-")
-                .append(range[1]);
-      }
-
-      hiddenColsBuilder.deleteCharAt(0);
-      hiddenSections[0] = hiddenColsBuilder.toString();
+      hiddenSections[0] = getViewport().getAlignment().getHiddenColumns()
+              .regionsToString(";", "-");
     }
 
     // hidden rows/seqs business
index b9becf3..9f77eaa 100644 (file)
@@ -600,4 +600,16 @@ public class HiddenColumnsTest
     assertEquals(5, positions.get(1).intValue());
     assertEquals(8, positions.get(2).intValue());
   }
+
+  @Test(groups = { "Functional" })
+  public void testRegionsToString()
+  {
+    HiddenColumns hc = new HiddenColumns();
+    hc.hideColumns(3, 7);
+    hc.hideColumns(10, 10);
+    hc.hideColumns(14, 15);
+
+    String result = hc.regionsToString(",", "--");
+    assertEquals("3--7,10--10,14--15", result);
+  }
 }