From fba88b586dee61e55d740202c4727a6571529404 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Fri, 30 Mar 2018 10:39:11 +0100 Subject: [PATCH] JAL-2599 move utility method to MappingUtils --- src/jalview/util/MapList.java | 59 ++----------------------------- src/jalview/util/MappingUtils.java | 55 ++++++++++++++++++++++++++++ test/jalview/util/MapListTest.java | 54 ---------------------------- test/jalview/util/MappingUtilsTest.java | 54 ++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 111 deletions(-) diff --git a/src/jalview/util/MapList.java b/src/jalview/util/MapList.java index c944345..81b9964 100644 --- a/src/jalview/util/MapList.java +++ b/src/jalview/util/MapList.java @@ -1021,70 +1021,15 @@ public class MapList for (int[] range : map.getFromRanges()) { - addRange(range, fromShifts); + MappingUtils.addRange(range, fromShifts); } for (int[] range : map.getToRanges()) { - addRange(range, toShifts); + MappingUtils.addRange(range, toShifts); } } /** - * Adds the given range to a list of ranges. If the new range just extends - * existing ranges, the current endpoint is updated instead. - * - * @param range - * @param addTo - */ - static void addRange(int[] range, List addTo) - { - /* - * list is empty - add to it! - */ - if (addTo.size() == 0) - { - addTo.add(range); - return; - } - - int[] last = addTo.get(addTo.size() - 1); - boolean lastForward = last[1] >= last[0]; - boolean newForward = range[1] >= range[0]; - - /* - * contiguous range in the same direction - just update endpoint - */ - if (lastForward == newForward && last[1] == range[0]) - { - last[1] = range[1]; - return; - } - - /* - * next range starts at +1 in forward sense - update endpoint - */ - if (lastForward && newForward && range[0] == last[1] + 1) - { - last[1] = range[1]; - return; - } - - /* - * next range starts at -1 in reverse sense - update endpoint - */ - if (!lastForward && !newForward && range[0] == last[1] - 1) - { - last[1] = range[1]; - return; - } - - /* - * just add the new range - */ - addTo.add(range); - } - - /** * Returns true if mapping is from forward strand, false if from reverse * strand. Result is just based on the first 'from' range that is not a single * position. Default is true unless proven to be false. Behaviour is not well diff --git a/src/jalview/util/MappingUtils.java b/src/jalview/util/MappingUtils.java index b552c21..cd8821d 100644 --- a/src/jalview/util/MappingUtils.java +++ b/src/jalview/util/MappingUtils.java @@ -1020,4 +1020,59 @@ public final class MappingUtils } } } + + /** + * Adds the given range to a list of ranges. If the new range just extends + * existing ranges, the current endpoint is updated instead. + * + * @param range + * @param addTo + */ + public static void addRange(int[] range, List addTo) + { + /* + * list is empty - add to it! + */ + if (addTo.size() == 0) + { + addTo.add(range); + return; + } + + int[] last = addTo.get(addTo.size() - 1); + boolean lastForward = last[1] >= last[0]; + boolean newForward = range[1] >= range[0]; + + /* + * contiguous range in the same direction - just update endpoint + */ + if (lastForward == newForward && last[1] == range[0]) + { + last[1] = range[1]; + return; + } + + /* + * next range starts at +1 in forward sense - update endpoint + */ + if (lastForward && newForward && range[0] == last[1] + 1) + { + last[1] = range[1]; + return; + } + + /* + * next range starts at -1 in reverse sense - update endpoint + */ + if (!lastForward && !newForward && range[0] == last[1] - 1) + { + last[1] = range[1]; + return; + } + + /* + * just add the new range + */ + addTo.add(range); + } } diff --git a/test/jalview/util/MapListTest.java b/test/jalview/util/MapListTest.java index 029b681..fd68e96 100644 --- a/test/jalview/util/MapListTest.java +++ b/test/jalview/util/MapListTest.java @@ -599,60 +599,6 @@ public class MapListTest assertEquals("[ [11, 16] ] 1:3 to [ [72, 53] ]", ml.toString()); } - @Test(groups = "Functional") - public void testAddRange() - { - int[] range = { 1, 5 }; - List ranges = new ArrayList<>(); - - // add to empty list: - MapList.addRange(range, ranges); - assertEquals(1, ranges.size()); - assertSame(range, ranges.get(0)); - - // extend contiguous (same position): - MapList.addRange(new int[] { 5, 10 }, ranges); - assertEquals(1, ranges.size()); - assertEquals(1, ranges.get(0)[0]); - assertEquals(10, ranges.get(0)[1]); - - // extend contiguous (next position): - MapList.addRange(new int[] { 11, 15 }, ranges); - assertEquals(1, ranges.size()); - assertEquals(1, ranges.get(0)[0]); - assertEquals(15, ranges.get(0)[1]); - - // change direction: range is not merged: - MapList.addRange(new int[] { 16, 10 }, ranges); - assertEquals(2, ranges.size()); - assertEquals(16, ranges.get(1)[0]); - assertEquals(10, ranges.get(1)[1]); - - // extend reverse contiguous (same position): - MapList.addRange(new int[] { 10, 8 }, ranges); - assertEquals(2, ranges.size()); - assertEquals(16, ranges.get(1)[0]); - assertEquals(8, ranges.get(1)[1]); - - // extend reverse contiguous (next position): - MapList.addRange(new int[] { 7, 6 }, ranges); - assertEquals(2, ranges.size()); - assertEquals(16, ranges.get(1)[0]); - assertEquals(6, ranges.get(1)[1]); - - // change direction: range is not merged: - MapList.addRange(new int[] { 6, 9 }, ranges); - assertEquals(3, ranges.size()); - assertEquals(6, ranges.get(2)[0]); - assertEquals(9, ranges.get(2)[1]); - - // not contiguous: not merged - MapList.addRange(new int[] { 11, 12 }, ranges); - assertEquals(4, ranges.size()); - assertEquals(11, ranges.get(3)[0]); - assertEquals(12, ranges.get(3)[1]); - } - /** * Check state after construction */ diff --git a/test/jalview/util/MappingUtilsTest.java b/test/jalview/util/MappingUtilsTest.java index 097ccd4..af82ac6 100644 --- a/test/jalview/util/MappingUtilsTest.java +++ b/test/jalview/util/MappingUtilsTest.java @@ -1284,4 +1284,58 @@ public class MappingUtilsTest assertEquals(1, ranges.size()); assertEquals(9, ranges.get(0)[1]); } + + @Test(groups = "Functional") + public void testAddRange() + { + int[] range = { 1, 5 }; + List ranges = new ArrayList<>(); + + // add to empty list: + MappingUtils.addRange(range, ranges); + assertEquals(1, ranges.size()); + assertSame(range, ranges.get(0)); + + // extend contiguous (same position): + MappingUtils.addRange(new int[] { 5, 10 }, ranges); + assertEquals(1, ranges.size()); + assertEquals(1, ranges.get(0)[0]); + assertEquals(10, ranges.get(0)[1]); + + // extend contiguous (next position): + MappingUtils.addRange(new int[] { 11, 15 }, ranges); + assertEquals(1, ranges.size()); + assertEquals(1, ranges.get(0)[0]); + assertEquals(15, ranges.get(0)[1]); + + // change direction: range is not merged: + MappingUtils.addRange(new int[] { 16, 10 }, ranges); + assertEquals(2, ranges.size()); + assertEquals(16, ranges.get(1)[0]); + assertEquals(10, ranges.get(1)[1]); + + // extend reverse contiguous (same position): + MappingUtils.addRange(new int[] { 10, 8 }, ranges); + assertEquals(2, ranges.size()); + assertEquals(16, ranges.get(1)[0]); + assertEquals(8, ranges.get(1)[1]); + + // extend reverse contiguous (next position): + MappingUtils.addRange(new int[] { 7, 6 }, ranges); + assertEquals(2, ranges.size()); + assertEquals(16, ranges.get(1)[0]); + assertEquals(6, ranges.get(1)[1]); + + // change direction: range is not merged: + MappingUtils.addRange(new int[] { 6, 9 }, ranges); + assertEquals(3, ranges.size()); + assertEquals(6, ranges.get(2)[0]); + assertEquals(9, ranges.get(2)[1]); + + // not contiguous: not merged + MappingUtils.addRange(new int[] { 11, 12 }, ranges); + assertEquals(4, ranges.size()); + assertEquals(11, ranges.get(3)[0]); + assertEquals(12, ranges.get(3)[1]); + } } -- 1.7.10.2