DirtyMap (renamed from DirtyDataMap) treated as the map instead of a container

This commit is contained in:
nossr50 2020-09-17 18:23:01 -07:00
parent 5aaa749907
commit 0bfb3b23b6
3 changed files with 49 additions and 40 deletions

View File

@ -11,13 +11,13 @@ import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
public class DirtyDataMap<K, V> implements Map<K, V> {
public class DirtyMap<K, V> implements Map<K, V> {
private final @NotNull MutableBoolean dirtyFlag; //Can be pointed at a reference
private @NotNull Map<K, V> dataMap;
private @NotNull Map<K, V> map;
public DirtyDataMap(@NotNull Map<K, V> data, @NotNull MutableBoolean referenceFlag) {
this.dataMap = data;
public DirtyMap(@NotNull Map<K, V> data, @NotNull MutableBoolean referenceFlag) {
this.map = data;
this.dirtyFlag = referenceFlag;
}
@ -29,154 +29,164 @@ public class DirtyDataMap<K, V> implements Map<K, V> {
dirtyFlag.setBoolean(true);
}
public void setData(@NotNull Map<K, V> dataMap) {
this.dataMap = dataMap;
/**
* Change the map contained in this wrapper
* @param dataMap the map to wrap around instead of the current map
*/
public void setMap(@NotNull Map<K, V> dataMap) {
this.map = dataMap;
setDirty();
}
public @NotNull Map<K, V> getDataMap() {
/**
* Get the inner map that this DirtyMap is wrapping
* @return the inner map of this DirtyMap
*/
public @NotNull Map<K, V> unwrapMap() {
setDirty();
return dataMap;
return map;
}
/* Map Interface Delegates */
@Override
public V get(Object key) {
return dataMap.get(key);
return map.get(key);
}
public int size() {
return dataMap.size();
return map.size();
}
public boolean isEmpty() {
return dataMap.isEmpty();
return map.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return dataMap.containsKey(key);
return map.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return dataMap.containsValue(value);
return map.containsValue(value);
}
public V put(K key, V value) {
setDirty();
return dataMap.put(key, value);
return map.put(key, value);
}
public V remove(Object key) {
setDirty();
return dataMap.remove(key);
return map.remove(key);
}
public void putAll(@NotNull Map<? extends K, ? extends V> m) {
setDirty();
dataMap.putAll(m);
map.putAll(m);
}
public void clear() {
setDirty();
dataMap.clear();
map.clear();
}
@Override
public @NotNull Set<K> keySet() {
setDirty();
return dataMap.keySet();
return map.keySet();
}
@Override
public @NotNull Collection<V> values() {
setDirty();
return dataMap.values();
return map.values();
}
@Override
public @NotNull Set<Map.Entry<K, V>> entrySet() {
setDirty();
return dataMap.entrySet();
return map.entrySet();
}
@Override
public V getOrDefault(Object key, V defaultValue) {
return dataMap.getOrDefault(key, defaultValue);
return map.getOrDefault(key, defaultValue);
}
@Override
public void forEach(BiConsumer<? super K, ? super V> action) {
setDirty();
dataMap.forEach(action);
map.forEach(action);
}
@Override
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
setDirty();
dataMap.replaceAll(function);
map.replaceAll(function);
}
@Override
public V putIfAbsent(K key, V value) {
setDirty();
return dataMap.putIfAbsent(key, value);
return map.putIfAbsent(key, value);
}
@Override
public boolean remove(Object key, Object value) {
setDirty();
return dataMap.remove(key, value);
return map.remove(key, value);
}
@Override
public boolean replace(K key, V oldValue, V newValue) {
setDirty();
return dataMap.replace(key, oldValue, newValue);
return map.replace(key, oldValue, newValue);
}
@Override
public V replace(K key, V value) {
setDirty();
return dataMap.replace(key, value);
return map.replace(key, value);
}
@Override
public V computeIfAbsent(K key, @NotNull Function<? super K, ? extends V> mappingFunction) {
setDirty();
return dataMap.computeIfAbsent(key, mappingFunction);
return map.computeIfAbsent(key, mappingFunction);
}
@Override
public V computeIfPresent(K key, @NotNull BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
setDirty();
return dataMap.computeIfPresent(key, remappingFunction);
return map.computeIfPresent(key, remappingFunction);
}
@Override
public V compute(K key, @NotNull BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
setDirty();
return dataMap.compute(key, remappingFunction);
return map.compute(key, remappingFunction);
}
@Override
public V merge(K key, @NotNull V value, @NotNull BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
setDirty();
return dataMap.merge(key, value, remappingFunction);
return map.merge(key, value, remappingFunction);
}
/* Override for equals and hash */
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DirtyDataMap<?, ?> that = (DirtyDataMap<?, ?>) o;
return Objects.equal(getDataMap(), that.getDataMap());
DirtyMap<?, ?> that = (DirtyMap<?, ?>) o;
return Objects.equal(map, that.map);
}
@Override
public int hashCode() {
return Objects.hashCode(getDataMap());
return Objects.hashCode(map);
}
}

View File

@ -120,7 +120,7 @@ public class McMMOPlayer extends PlayerProfile {
superAbilityManager = new SuperAbilityManager(this);
abilityActivationProcessor = new AbilityActivationProcessor(this);
experienceBarManager = new MMOExperienceBarManager(this, persistentPlayerData.getDirtyBarStateMap());
experienceBarManager = new MMOExperienceBarManager(this, persistentPlayerData.getBarStateMap());
//Update last login
updateLastLogin();

View File

@ -1,7 +1,6 @@
package com.gmail.nossr50.util.experience;
import com.gmail.nossr50.config.experience.ExperienceConfig;
import com.gmail.nossr50.datatypes.dirtydata.DirtyDataMap;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.mcMMO;
@ -23,13 +22,13 @@ public class MMOExperienceBarManager {
int delaySeconds = 3;
private @NotNull final DirtyDataMap<PrimarySkillType, BarState> barStateMapRef;
private @NotNull final Map<PrimarySkillType, BarState> barStateMapRef;
private @NotNull EnumMap<PrimarySkillType, ExperienceBarWrapper> experienceBars;
private @NotNull EnumMap<PrimarySkillType, ExperienceBarHideTask> experienceBarHideTaskHashMap;
private @NotNull final EnumMap<PrimarySkillType, ExperienceBarWrapper> experienceBars;
private @NotNull final EnumMap<PrimarySkillType, ExperienceBarHideTask> experienceBarHideTaskHashMap;
public MMOExperienceBarManager(@NotNull McMMOPlayer mmoPlayer, @NotNull DirtyDataMap<PrimarySkillType, BarState> barStateMapRef)
public MMOExperienceBarManager(@NotNull McMMOPlayer mmoPlayer, @NotNull Map<PrimarySkillType, BarState> barStateMapRef)
{
this.mmoPlayer = mmoPlayer;
this.barStateMapRef = barStateMapRef;