org.modelmapper.internal.util.CopyOnWriteLinkedHashMap Maven / Gradle / Ivy
package org.modelmapper.internal.util;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/**
* A thread-safe {@link LinkedHashMap} which creates a copy of the underlying map on write
* operations.
*
*
* Read operations, including iteration, do not interfere with writes since the underlying map is
* never modified.
*
* @author Jonathan Halterman
*/
public class CopyOnWriteLinkedHashMap implements Map {
private volatile Map map;
public CopyOnWriteLinkedHashMap() {
map = new LinkedHashMap();
}
public CopyOnWriteLinkedHashMap(Map data) {
map = new LinkedHashMap(data);
}
public synchronized void clear() {
map = new LinkedHashMap();
}
public boolean containsKey(Object key) {
return map.containsKey(key);
}
public boolean containsValue(Object value) {
return map.containsValue(value);
}
public Set> entrySet() {
return map.entrySet();
}
public V get(Object key) {
return map.get(key);
}
public boolean isEmpty() {
return map.isEmpty();
}
public Set keySet() {
return map.keySet();
}
public synchronized V put(K key, V value) {
Map newMap = new LinkedHashMap(map);
V previous = newMap.put(key, value);
map = newMap;
return previous;
}
public synchronized void putAll(Map extends K, ? extends V> newData) {
Map newMap = new LinkedHashMap(map);
newMap.putAll(newData);
map = newMap;
}
public synchronized V remove(Object key) {
Map newMap = new LinkedHashMap(map);
V previous = newMap.remove(key);
map = newMap;
return previous;
}
public int size() {
return map.size();
}
public Collection values() {
return map.values();
}
}