[New] Added a DefaultedHashMap for default values on unmapped keys
This commit is contained in:
parent
2d922df572
commit
f5cf3e3ad0
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
* 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 <eitch@eitchnet.ch>
|
||||
*/
|
||||
public class DefaultedHashMap<K, V> extends HashMap<K, V> {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
* 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 <eitch@eitchnet.ch>
|
||||
*/
|
||||
public class DefaultedHashMapTest {
|
||||
|
||||
private Map<String, String> map;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.map = new DefaultedHashMap<String, String>("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"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue