Created
July 29, 2008 21:13
-
-
Save pjb3/3174 to your computer and use it in GitHub Desktop.
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
import java.util.*; | |
/** | |
* @author Paul Barry | |
*/ | |
public class Hash { | |
@SuppressWarnings("unchecked") | |
public static Map init(Map map, Object... keyValuePairs) { | |
if(keyValuePairs != null) { | |
if(keyValuePairs.length%2 != 0) { | |
throw new RuntimeException("Uneven number of key/value pairs"); | |
} else { | |
for(int i = 0; i<keyValuePairs.length;) { | |
map.put(keyValuePairs[i++], keyValuePairs[i++]); | |
} | |
} | |
} | |
return map; | |
} | |
public static Map init(Object... keyValuePairs) { | |
return init(new HashMap(), keyValuePairs); | |
} | |
public static Map linkedMap(Object... keyValuePairs) { | |
return init(new LinkedHashMap(), keyValuePairs); | |
} | |
@SuppressWarnings("unchecked") | |
public static Map<String, Object> stringMap(Object... keyValuePairs) { | |
return init(new HashMap<String, Object>(), keyValuePairs); | |
} | |
@SuppressWarnings("unchecked") | |
public static Map<String, Object> linkedStringMap(Object... keyValuePairs) { | |
return init(new LinkedHashMap<String, Object>(), keyValuePairs); | |
} | |
@SuppressWarnings("unchecked") | |
public static Map merge(Map mergee, Object... keyValuePairs) { | |
return mergeMaps(mergee, init(keyValuePairs)); | |
} | |
@SuppressWarnings("unchecked") | |
public static Map reverseMerge(Map mergee, Object... keyValuePairs) { | |
return mergeMaps(init(keyValuePairs), mergee); | |
} | |
@SuppressWarnings("unchecked") | |
public static Map mergeMaps(Map mergee, Map merger) { | |
Map map = new HashMap(mergee); | |
map.putAll(merger); | |
return map; | |
} | |
@SuppressWarnings("unchecked") | |
public static Map<String, Object> mergeStringMaps(Map<String, Object> mergee, Map<String, Object> merger) { | |
Map map = new HashMap<String, Object>(mergee); | |
map.putAll(merger); | |
return map; | |
} | |
} |
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
import junit.framework.TestCase; | |
import java.util.Map; | |
/** | |
* @author Paul Barry | |
*/ | |
public class TestHash extends TestCase { | |
public void testMap() { | |
assertEquals(true, Hash.init(1, true, 0, false).get(1)); | |
} | |
public void testLinkedMap() { | |
assertEquals("a", Hash.linkedMap(1, "a", 2, "b", 3, "c").values().iterator().next()); | |
} | |
@SuppressWarnings("unchecked") | |
public void testStringMap() { | |
assertEquals("bar", Hash.stringMap("foo", "bar").get("foo")); | |
assertEquals(new Integer(42), Hash.stringMap("foo", 42).get("foo")); | |
assertTrue((Boolean) Hash.stringMap("foo", true).get("foo")); | |
} | |
@SuppressWarnings("unchecked") | |
public void testStringLinkedMap() { | |
assertEquals(1, Hash.linkedMap("a", 1, "b", 2, "c", 3).values().iterator().next()); | |
} | |
public void testMerge() { | |
Map result = Hash.merge(Hash.init(0, "zero", 1, "one"), 0, "nil", 2, "two"); | |
assertEquals("nil", result.get(0)); | |
assertEquals("one", result.get(1)); | |
assertEquals("two", result.get(2)); | |
} | |
public void testReverseMerge() { | |
Map result = Hash.reverseMerge(Hash.init(0, "zero", 1, "one"), 0, "nil", 2, "two"); | |
assertEquals("zero", result.get(0)); | |
assertEquals("one", result.get(1)); | |
assertEquals("two", result.get(2)); | |
} | |
public void testMergeMaps() { | |
Map result = Hash.mergeMaps( | |
Hash.init(0, "zero", 1, "one"), | |
Hash.init(0, "nil", 2, "two")); | |
assertEquals("nil", result.get(0)); | |
assertEquals("one", result.get(1)); | |
assertEquals("two", result.get(2)); | |
} | |
@SuppressWarnings("unchecked") | |
public void testMergeStringMaps() { | |
Map<String, Object> result = Hash.mergeMaps( | |
Hash.stringMap("foo", 0, "bar", 2), | |
Hash.stringMap("foo", 1, "bang", true)); | |
assertEquals(1, result.get("foo")); | |
assertEquals(2, result.get("bar")); | |
assertTrue((Boolean)result.get("bang")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment