Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'Sets' utility class and unit tests (at request of Simon). #43

Merged
merged 1 commit into from
Nov 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(""));
}
}