From f5cf3e3ad0a4865ec75ffb4e58b03557b4d1db2e Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Mon, 10 Aug 2015 22:20:40 +0200 Subject: [PATCH] [New] Added a DefaultedHashMap for default values on unmapped keys --- .../utils/collections/DefaultedHashMap.java | 43 ++++++++++++++++ .../collections/DefaultedHashMapTest.java | 51 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/main/java/ch/eitchnet/utils/collections/DefaultedHashMap.java create mode 100644 src/test/java/ch/eitchnet/utils/collections/DefaultedHashMapTest.java diff --git a/src/main/java/ch/eitchnet/utils/collections/DefaultedHashMap.java b/src/main/java/ch/eitchnet/utils/collections/DefaultedHashMap.java new file mode 100644 index 000000000..10ef2d75b --- /dev/null +++ b/src/main/java/ch/eitchnet/utils/collections/DefaultedHashMap.java @@ -0,0 +1,43 @@ +/* + * Copyright 2015 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package ch.eitchnet.utils.collections; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Robert von Burg + */ +public class DefaultedHashMap extends HashMap { + + private static final long serialVersionUID = 1L; + private V defaultValue; + + /** + * Constructs this {@link Map} instance to have a default value on inexistent keys + * + * @param defaultValue + * the default to return if a key is not mapped + */ + public DefaultedHashMap(V defaultValue) { + this.defaultValue = defaultValue; + } + + @Override + public V get(Object key) { + return getOrDefault(key, this.defaultValue); + } +} diff --git a/src/test/java/ch/eitchnet/utils/collections/DefaultedHashMapTest.java b/src/test/java/ch/eitchnet/utils/collections/DefaultedHashMapTest.java new file mode 100644 index 000000000..ddf7c6569 --- /dev/null +++ b/src/test/java/ch/eitchnet/utils/collections/DefaultedHashMapTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2015 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package ch.eitchnet.utils.collections; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +/** + * @author Robert von Burg + */ +public class DefaultedHashMapTest { + + private Map map; + + @Before + public void setUp() { + this.map = new DefaultedHashMap("foobar"); + this.map.put("foo", "foofoo"); + } + + @Test + public void shouldReturnMappedValue() { + assertTrue(this.map.containsKey("foo")); + assertEquals("foofoo", this.map.get("foo")); + } + + @Test + public void shouldReturnDefaultValue() { + assertFalse(this.map.containsKey("bar")); + assertEquals("foobar", this.map.get("bar")); + } +}