- 在自定义 Map 类中保留两个地图字段,
originalMap
并且temporaryMap
.
- 读操作委托给
originalMap
,写操作委托给temporaryMap
。
rollback()
浅拷贝originalMap
到temporaryMap
,commit()
反之亦然。由于从不克隆键和值,因此只保留引用并且不会“浪费”内存
package com.example;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import lombok.NonNull;
import lombok.ToString;
@ToString
public class TransactionalMap<K, V> implements Map<K, V> {
private Map<K, V> originalMap;
private Map<K, V> temporaryMap;
public TransactionalMap() {
this(new HashMap<>());
}
public TransactionalMap(@NonNull Map<K, V> impl) {
if (impl instanceof TransactionalMap) {
throw new IllegalArgumentException("Must provide valid implementation instance");
}
this.originalMap = new HashMap<>(impl);
this.temporaryMap = new HashMap<>(originalMap);
}
@Override
public int size() {
return originalMap.size();
}
@Override
public boolean isEmpty() {
return originalMap.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return originalMap.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return originalMap.containsValue(value);
}
@Override
public V get(Object key) {
return originalMap.get(key);
}
@Override
public V put(K key, V value) {
return temporaryMap.put(key, value);
}
@Override
public V remove(Object key) {
return temporaryMap.remove(key);
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
temporaryMap.putAll(m);
}
@Override
public void clear() {
temporaryMap.clear();
}
@Override
public Set<K> keySet() {
return originalMap.keySet();
}
@Override
public Collection<V> values() {
return originalMap.values();
}
@Override
public Set<Entry<K, V>> entrySet() {
return originalMap.entrySet();
}
private void sync(Map<K, V> src, Map<K, V> tgt) {
tgt.putAll(src);
tgt.forEach((k, v) -> {
if (!src.containsKey(k)) tgt.remove(k);
});
}
public void commit() {
sync(temporaryMap, originalMap);
}
public void rollback() {
sync(originalMap, temporaryMap);
}
}
使用lombok生成样板代码