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.BiFunction;
import java.util.function.Function; 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 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) { public DirtyMap(@NotNull Map<K, V> data, @NotNull MutableBoolean referenceFlag) {
this.dataMap = data; this.map = data;
this.dirtyFlag = referenceFlag; this.dirtyFlag = referenceFlag;
} }
@ -29,154 +29,164 @@ public class DirtyDataMap<K, V> implements Map<K, V> {
dirtyFlag.setBoolean(true); 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(); 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(); setDirty();
return dataMap; return map;
} }
/* Map Interface Delegates */ /* Map Interface Delegates */
@Override @Override
public V get(Object key) { public V get(Object key) {
return dataMap.get(key); return map.get(key);
} }
public int size() { public int size() {
return dataMap.size(); return map.size();
} }
public boolean isEmpty() { public boolean isEmpty() {
return dataMap.isEmpty(); return map.isEmpty();
} }
@Override @Override
public boolean containsKey(Object key) { public boolean containsKey(Object key) {
return dataMap.containsKey(key); return map.containsKey(key);
} }
@Override @Override
public boolean containsValue(Object value) { public boolean containsValue(Object value) {
return dataMap.containsValue(value); return map.containsValue(value);
} }
public V put(K key, V value) { public V put(K key, V value) {
setDirty(); setDirty();
return dataMap.put(key, value); return map.put(key, value);
} }
public V remove(Object key) { public V remove(Object key) {
setDirty(); setDirty();
return dataMap.remove(key); return map.remove(key);
} }
public void putAll(@NotNull Map<? extends K, ? extends V> m) { public void putAll(@NotNull Map<? extends K, ? extends V> m) {
setDirty(); setDirty();
dataMap.putAll(m); map.putAll(m);
} }
public void clear() { public void clear() {
setDirty(); setDirty();
dataMap.clear(); map.clear();
} }
@Override @Override
public @NotNull Set<K> keySet() { public @NotNull Set<K> keySet() {
setDirty(); setDirty();
return dataMap.keySet(); return map.keySet();
} }
@Override @Override
public @NotNull Collection<V> values() { public @NotNull Collection<V> values() {
setDirty(); setDirty();
return dataMap.values(); return map.values();
} }
@Override @Override
public @NotNull Set<Map.Entry<K, V>> entrySet() { public @NotNull Set<Map.Entry<K, V>> entrySet() {
setDirty(); setDirty();
return dataMap.entrySet(); return map.entrySet();
} }
@Override @Override
public V getOrDefault(Object key, V defaultValue) { public V getOrDefault(Object key, V defaultValue) {
return dataMap.getOrDefault(key, defaultValue); return map.getOrDefault(key, defaultValue);
} }
@Override @Override
public void forEach(BiConsumer<? super K, ? super V> action) { public void forEach(BiConsumer<? super K, ? super V> action) {
setDirty(); setDirty();
dataMap.forEach(action); map.forEach(action);
} }
@Override @Override
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
setDirty(); setDirty();
dataMap.replaceAll(function); map.replaceAll(function);
} }
@Override @Override
public V putIfAbsent(K key, V value) { public V putIfAbsent(K key, V value) {
setDirty(); setDirty();
return dataMap.putIfAbsent(key, value); return map.putIfAbsent(key, value);
} }
@Override @Override
public boolean remove(Object key, Object value) { public boolean remove(Object key, Object value) {
setDirty(); setDirty();
return dataMap.remove(key, value); return map.remove(key, value);
} }
@Override @Override
public boolean replace(K key, V oldValue, V newValue) { public boolean replace(K key, V oldValue, V newValue) {
setDirty(); setDirty();
return dataMap.replace(key, oldValue, newValue); return map.replace(key, oldValue, newValue);
} }
@Override @Override
public V replace(K key, V value) { public V replace(K key, V value) {
setDirty(); setDirty();
return dataMap.replace(key, value); return map.replace(key, value);
} }
@Override @Override
public V computeIfAbsent(K key, @NotNull Function<? super K, ? extends V> mappingFunction) { public V computeIfAbsent(K key, @NotNull Function<? super K, ? extends V> mappingFunction) {
setDirty(); setDirty();
return dataMap.computeIfAbsent(key, mappingFunction); return map.computeIfAbsent(key, mappingFunction);
} }
@Override @Override
public V computeIfPresent(K key, @NotNull BiFunction<? super K, ? super V, ? extends V> remappingFunction) { public V computeIfPresent(K key, @NotNull BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
setDirty(); setDirty();
return dataMap.computeIfPresent(key, remappingFunction); return map.computeIfPresent(key, remappingFunction);
} }
@Override @Override
public V compute(K key, @NotNull BiFunction<? super K, ? super V, ? extends V> remappingFunction) { public V compute(K key, @NotNull BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
setDirty(); setDirty();
return dataMap.compute(key, remappingFunction); return map.compute(key, remappingFunction);
} }
@Override @Override
public V merge(K key, @NotNull V value, @NotNull BiFunction<? super V, ? super V, ? extends V> remappingFunction) { public V merge(K key, @NotNull V value, @NotNull BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
setDirty(); setDirty();
return dataMap.merge(key, value, remappingFunction); return map.merge(key, value, remappingFunction);
} }
/* Override for equals and hash */
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
DirtyDataMap<?, ?> that = (DirtyDataMap<?, ?>) o; DirtyMap<?, ?> that = (DirtyMap<?, ?>) o;
return Objects.equal(getDataMap(), that.getDataMap()); return Objects.equal(map, that.map);
} }
@Override @Override
public int hashCode() { 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); superAbilityManager = new SuperAbilityManager(this);
abilityActivationProcessor = new AbilityActivationProcessor(this); abilityActivationProcessor = new AbilityActivationProcessor(this);
experienceBarManager = new MMOExperienceBarManager(this, persistentPlayerData.getDirtyBarStateMap()); experienceBarManager = new MMOExperienceBarManager(this, persistentPlayerData.getBarStateMap());
//Update last login //Update last login
updateLastLogin(); updateLastLogin();

View File

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