Skip to content

Commit

Permalink
Add 'Sets' utility class and unit tests (at request of Simon).
Browse files Browse the repository at this point in the history
  • Loading branch information
atropineal committed Nov 5, 2013
1 parent 1339aec commit 47695bc
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> fromCommaSeparatedValues(String csv) {
if (csv == null || csv.isEmpty()) {
return Collections.EMPTY_SET;
}
String[] tokens = csv.split(",");
return new HashSet<String>(Arrays.asList(tokens));
}
}
Original file line number Diff line number Diff line change
@@ -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(""));
}
}

0 comments on commit 47695bc

Please sign in to comment.