-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'Sets' utility class and unit tests (at request of Simon).
- Loading branch information
1 parent
1339aec
commit 47695bc
Showing
2 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
cougar-framework/cougar-util/src/main/java/com/betfair/cougar/util/configuration/Sets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
...r-framework/cougar-util/src/test/java/com/betfair/cougar/util/configuration/SetsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("")); | ||
} | ||
} |