From aece7fa4c78e52cb64cbcd086b8d5b2ee3992a87 Mon Sep 17 00:00:00 2001 From: aaron Date: Tue, 17 Nov 2009 16:50:01 +0000 Subject: [PATCH] a convenience method to join a map into a single string, which I need for some VCF work. Added some documentation to the join method as well. git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@2057 348d0f76-0448-11de-a6fe-93d51630548a --- .../org/broadinstitute/sting/utils/Utils.java | 30 +++++++++++++++++++ .../broadinstitute/sting/utils/UtilsTest.java | 29 ++++++++++++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/java/src/org/broadinstitute/sting/utils/Utils.java b/java/src/org/broadinstitute/sting/utils/Utils.java index c43b57928..36fa44539 100755 --- a/java/src/org/broadinstitute/sting/utils/Utils.java +++ b/java/src/org/broadinstitute/sting/utils/Utils.java @@ -237,6 +237,36 @@ public class Utils { return flags; } + /** + * join the key value pairs of a map into one string, i.e. myMap = [A->1,B->2,C->3] with a call of: + * joinMap("-","*",myMap) -> returns A-1*B-2*C-3 + * + * Be forewarned, if you're not using a map that is aware of the ordering (i.e. HashMap instead of LinkedHashMap) + * the ordering of the string you get back might not be what you expect! (i.e. C-3*A-1*B-2 vrs A-1*B-2*C-3) + * + * @param keyValueSeperator the string to seperate the key-value pairs + * @param recordSeperator the string to use to seperate each key-value pair from other key-value pairs + * @param map the map to draw from + * @param the map's key type + * @param the map's value type + * @return a string representing the joined map + */ + public static String joinMap(String keyValueSeperator, String recordSeperator, Map map) { + if (map.size() < 1) { return null; } + String joinedKeyValues[] = new String[map.size()]; + int index = 0; + for (L key : map.keySet()) { + joinedKeyValues[index++] = String.format("%s%s%s",key.toString(),keyValueSeperator,map.get(key).toString()); + } + return join(recordSeperator,joinedKeyValues); + } + + /** + * join an array of strings given a seperator + * @param separator the string to insert between each array element + * @param strings the array of strings + * @return a string, which is the joining of all array values with the separator + */ public static String join(String separator, String[] strings) { return join(separator, strings, 0, strings.length); } diff --git a/java/test/org/broadinstitute/sting/utils/UtilsTest.java b/java/test/org/broadinstitute/sting/utils/UtilsTest.java index bd7d208c3..4d96e4fa0 100644 --- a/java/test/org/broadinstitute/sting/utils/UtilsTest.java +++ b/java/test/org/broadinstitute/sting/utils/UtilsTest.java @@ -24,9 +24,12 @@ package org.broadinstitute.sting.utils; -import org.junit.Test; -import org.broadinstitute.sting.BaseTest; import junit.framework.Assert; +import org.broadinstitute.sting.BaseTest; +import org.junit.Test; + +import java.util.LinkedHashMap; +import java.util.Map; /** * Testing framework for general purpose utilities class. @@ -57,4 +60,26 @@ public class UtilsTest extends BaseTest { Assert.assertEquals("dupString string was incorrect", "ccccc", duped); } + @Test + public void testJoinMap() { + Map map = new LinkedHashMap(); + map.put("one",1); + map.put("two",2); + String joined = Utils.joinMap("-",";",map); + Assert.assertTrue("one-1;two-2".equals(joined)); + } + + @Test + public void testJoinMapLargerSet() { + Map map = new LinkedHashMap(); + map.put("one",1); + map.put("two",2); + map.put("three",1); + map.put("four",2); + map.put("five",1); + map.put("six",2); + String joined = Utils.joinMap("-",";",map); + Assert.assertTrue("one-1;two-2;three-1;four-2;five-1;six-2".equals(joined)); + } + }