From 327368a55095d5610b71d03e4dcefeaf6131d300 Mon Sep 17 00:00:00 2001 From: kiramt Date: Fri, 17 Nov 2017 07:34:45 +0000 Subject: [PATCH 1/1] JAL-2759 HiddenColumnsCursor tests --- src/jalview/datamodel/HiddenColumns.java | 51 ++++++++- src/jalview/datamodel/HiddenColumnsCursor.java | 46 ++++++-- .../jalview/datamodel/HiddenColumnsCursorTest.java | 114 ++++++++++++++++++++ 3 files changed, 200 insertions(+), 11 deletions(-) create mode 100644 test/jalview/datamodel/HiddenColumnsCursorTest.java diff --git a/src/jalview/datamodel/HiddenColumns.java b/src/jalview/datamodel/HiddenColumns.java index 8c1a0fb..8a67741 100644 --- a/src/jalview/datamodel/HiddenColumns.java +++ b/src/jalview/datamodel/HiddenColumns.java @@ -289,6 +289,52 @@ public class HiddenColumns { try { + /* LOCK.readLock().lock(); + int result = hiddenColumn; + int[] region = null; + if (hiddenColumns != null) + { + Iterator it = new RegionsIterator(0, hiddenColumn, + hiddenColumns, cursor); + while (it.hasNext()) + { + region = it.next(); + if (hiddenColumn > region[1]) + { + result -= region[1] + 1 - region[0]; + } + } + + if (region != null && hiddenColumn >= region[0] + && hiddenColumn <= region[1]) + { + // Here the hidden column is within a region, so + // we want to return the position of region[0]-1, adjusted for any + // earlier hidden columns. + // Calculate the difference between the actual hidden col position + // and region[0]-1, and then subtract from result to convert result + // from the adjusted hiddenColumn value to the adjusted region[0]-1 + // value. + + // However, if the region begins at 0 we cannot return region[0]-1 + // just return 0 + if (region[0] == 0) + { + return 0; + } + else + { + return result - (hiddenColumn - region[0] + 1); + } + } + } + return result; // return the shifted position after removing hidden + // columns. + } finally + { + LOCK.readLock().unlock(); + }*/ + LOCK.readLock().lock(); int result = hiddenColumn; // int[] region = null; @@ -296,11 +342,11 @@ public class HiddenColumns { int index = cursor.findRegionForColumn(hiddenColumn); int hiddenBeforeCol = cursor.getHiddenSoFar(); - + // just subtract hidden cols count - this works fine if column is // visible result = hiddenColumn - hiddenBeforeCol; - + // now check in case column is hidden - it will be in the returned // hidden region if (index < hiddenColumns.size()) @@ -321,6 +367,7 @@ public class HiddenColumns } } } + System.out.println(hiddenColumn + " " + result); return result; // return the shifted position after removing hidden // columns. } finally diff --git a/src/jalview/datamodel/HiddenColumnsCursor.java b/src/jalview/datamodel/HiddenColumnsCursor.java index 9efcc0b..b40afdf 100644 --- a/src/jalview/datamodel/HiddenColumnsCursor.java +++ b/src/jalview/datamodel/HiddenColumnsCursor.java @@ -1,3 +1,23 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.datamodel; import java.util.List; @@ -7,9 +27,6 @@ public class HiddenColumnsCursor // absolute position of first hidden column private int firstColumn; - // absolute position of last hidden column - private int lastColumn; - // index of last visited region private int regionIndex; @@ -43,7 +60,6 @@ public class HiddenColumnsCursor { hiddenColumns = hiddenCols; firstColumn = hiddenColumns.get(0)[0]; - lastColumn = hiddenColumns.get(hiddenColumns.size() - 1)[1]; regionIndex = 0; hiddenSoFar = 0; } @@ -130,17 +146,29 @@ public class HiddenColumnsCursor // column is before current region else if (column < hiddenColumns.get(index)[0]) { - while ((index > 0) && (hiddenColumns.get(index)[1] > column)) + // column is before or in the previous region + if ((index > 0) && (hiddenColumns.get(index - 1)[1] >= column)) { - index--; - int[] region = hiddenColumns.get(index); - hiddenCount -= region[1] - region[0] + 1; + while ((index > 0) && (hiddenColumns.get(index)[1] > column)) + { + index--; + int[] region = hiddenColumns.get(index); + hiddenCount -= region[1] - region[0] + 1; + } } } updateCursor(index, hiddenCount); return index; } + /** + * Get the number of hidden columns in regions before column i.e. excludes + * hidden columns in the region column is in, if any + * + * @param column + * index of column in visible alignment + * @return + */ protected int getHiddenOffset(int column) { if (hiddenColumns == null) @@ -169,7 +197,7 @@ public class HiddenColumnsCursor index++; } } - else if (index < hiddenColumns.size()) + else { while ((index > 0) && (hiddenColumns.get(index - 1)[1] >= column + hiddenCount)) diff --git a/test/jalview/datamodel/HiddenColumnsCursorTest.java b/test/jalview/datamodel/HiddenColumnsCursorTest.java new file mode 100644 index 0000000..91cd3cd --- /dev/null +++ b/test/jalview/datamodel/HiddenColumnsCursorTest.java @@ -0,0 +1,114 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ +package jalview.datamodel; + +import static org.testng.AssertJUnit.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.testng.annotations.Test; + +public class HiddenColumnsCursorTest +{ + /** + * Test the method which finds the corresponding region given a column + */ + @Test(groups = { "Functional" }) + public void testFindRegionForColumn() + { + HiddenColumnsCursor cursor = new HiddenColumnsCursor(); + + int regionIndex = cursor.findRegionForColumn(20); + assertEquals(-1, regionIndex); + + List hidden = new ArrayList<>(); + hidden.add(new int[] { 53, 76 }); + hidden.add(new int[] { 104, 125 }); + cursor.resetCursor(hidden); + + regionIndex = cursor.findRegionForColumn(126); + assertEquals(2, regionIndex); + + regionIndex = cursor.findRegionForColumn(125); + assertEquals(1, regionIndex); + + regionIndex = cursor.findRegionForColumn(108); + assertEquals(1, regionIndex); + + regionIndex = cursor.findRegionForColumn(104); + assertEquals(1, regionIndex); + + regionIndex = cursor.findRegionForColumn(103); + assertEquals(1, regionIndex); + + regionIndex = cursor.findRegionForColumn(77); + assertEquals(1, regionIndex); + + regionIndex = cursor.findRegionForColumn(76); + assertEquals(0, regionIndex); + + regionIndex = cursor.findRegionForColumn(53); + assertEquals(0, regionIndex); + + regionIndex = cursor.findRegionForColumn(52); + assertEquals(0, regionIndex); + + regionIndex = cursor.findRegionForColumn(0); + assertEquals(0, regionIndex); + } + + /** + * Test the method which counts the number of hidden columns before a column + */ + @Test(groups = { "Functional" }) + public void testGetHiddenOffset() + { + HiddenColumnsCursor cursor = new HiddenColumnsCursor(); + + int offset = cursor.getHiddenOffset(20); + assertEquals(-1, offset); + + List hidden = new ArrayList<>(); + hidden.add(new int[] { 53, 76 }); + hidden.add(new int[] { 104, 125 }); + cursor.resetCursor(hidden); + + offset = cursor.getHiddenOffset(80); + assertEquals(46, offset); + + offset = cursor.getHiddenOffset(79); + assertEquals(24, offset); + + offset = cursor.getHiddenOffset(53); + assertEquals(24, offset); + + offset = cursor.getHiddenOffset(52); + assertEquals(0, offset); + + offset = cursor.getHiddenOffset(10); + assertEquals(0, offset); + + offset = cursor.getHiddenOffset(0); + assertEquals(0, offset); + } + +} -- 1.7.10.2