diff --git a/cougar-framework/cougar-util/src/main/java/com/betfair/cougar/util/configuration/Sets.java b/cougar-framework/cougar-util/src/main/java/com/betfair/cougar/util/configuration/Sets.java new file mode 100644 index 000000000..b1c74d92c --- /dev/null +++ b/cougar-framework/cougar-util/src/main/java/com/betfair/cougar/util/configuration/Sets.java @@ -0,0 +1,38 @@ +package com.betfair.cougar.util.configuration; + +import java.util.*; + +public class Sets { + + /** + * Given a map and a set of keys, return a set containing the values referred-to by the keys. + * If the map is null or the keys are null, the EMPTY_SET is returned. + * If a passed key does not exist in the map, nothing is added to the set. + * However, if the key is present and maps to null, null is added to the set. + */ + public static final Set fromMap(Map map, Object... keys) { + if (keys != null && map != null) { + Set answer = new HashSet(); + for (Object key : keys) { + if (map.containsKey(key)) { + answer.add(map.get(key)); + } + } + return Collections.unmodifiableSet(answer); + } + return Collections.EMPTY_SET; + } + + /** + * Given a comma-separated list of values, return a set of those values. + * If the passed string is null, the EMPTY_SET is returned. + * If the passed string is empty, the EMPTY_SET is returned. + */ + public static final Set fromCommaSeparatedValues(String csv) { + if (csv == null || csv.isEmpty()) { + return Collections.EMPTY_SET; + } + String[] tokens = csv.split(","); + return new HashSet(Arrays.asList(tokens)); + } +} diff --git a/cougar-framework/cougar-util/src/test/java/com/betfair/cougar/util/configuration/SetsTest.java b/cougar-framework/cougar-util/src/test/java/com/betfair/cougar/util/configuration/SetsTest.java new file mode 100644 index 000000000..9533ca1bc --- /dev/null +++ b/cougar-framework/cougar-util/src/test/java/com/betfair/cougar/util/configuration/SetsTest.java @@ -0,0 +1,102 @@ +package com.betfair.cougar.util.configuration; + +import org.junit.Test; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import static org.junit.Assert.*; + +public class SetsTest { + + /** + * Given a map and a set of keys, return a set containing the values referred-to by the keys. + */ + @Test + public void testFromMap() { + Map m = new HashMap() {{ + put("A", 1); + put("B", 2); + put("C", 3); + }}; + Set s = Sets.fromMap(m, "A", "C"); + assertTrue(s.contains(1)); + assertFalse(s.contains(2)); + assertTrue(s.contains(3)); + assertEquals(2, s.size()); + } + + /** + * If the map is null or the keys are null, the EMPTY_SET is returned. + */ + @Test + public void testFromMap_NullMap() { + assertEquals(Collections.EMPTY_SET, Sets.fromMap(null, "A")); + } + + /** + * If the map is null or the keys are null, the EMPTY_SET is returned. + */ + @Test + public void testFromMap_NullKeys() { + assertEquals(Collections.EMPTY_SET, Sets.fromMap(new HashMap(), null)); + } + + /** + * If a passed key does not exist in the map, nothing is added to the set. + */ + @Test + public void testFromMap_MissingKey() { + Map m = new HashMap() {{ + put("A", 1); + }}; + Set s = Sets.fromMap(m, "A", "D"); + assertTrue(s.contains(1)); + assertEquals(1, s.size()); + } + + /** + * However, if the key is present and maps to null, null is added to the set. + */ + @Test + public void testFromMap_KeyMappedToNull() { + Map m = new HashMap() {{ + put("A", 1); + put("B", null); + }}; + Set s = Sets.fromMap(m, "A", "B"); + assertTrue(s.contains(1)); + assertTrue(s.contains(null)); + assertEquals(2, s.size()); + } + + /** + * Given a comma-separated list of values, return a set of those values. + */ + @Test + public void testFromCommaSeparatedValues() { + Set s = Sets.fromCommaSeparatedValues("1,2,3"); + assertTrue(s.contains("1")); + assertTrue(s.contains("2")); + assertTrue(s.contains("3")); + assertEquals(3, s.size()); + } + + /** + * If the passed string is null, the EMPTY_SET is returned. + */ + @Test + public void testFromCommaSeparatedValues_NullInput() { + assertEquals(Collections.EMPTY_SET, Sets.fromCommaSeparatedValues(null)); + } + + /** + * If the passed string is empty, the EMPTY_SET is returned. + */ + @Test + public void testFromCommaSeparatedValues_EmptyInput() { + assertEquals(Collections.EMPTY_SET, Sets.fromCommaSeparatedValues("")); + } +}