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
This commit is contained in:
parent
21729d9311
commit
aece7fa4c7
|
|
@ -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 <L> the map's key type
|
||||
* @param <R> the map's value type
|
||||
* @return a string representing the joined map
|
||||
*/
|
||||
public static <L,R> String joinMap(String keyValueSeperator, String recordSeperator, Map<L,R> 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<String,Integer> map = new LinkedHashMap<String,Integer>();
|
||||
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<String,Integer> map = new LinkedHashMap<String,Integer>();
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue