Applying the new design to DirtySet (formerly DirtyDataSet) as well

This commit is contained in:
nossr50 2020-09-17 18:46:30 -07:00
parent 0bfb3b23b6
commit 934ba24b17
5 changed files with 206 additions and 165 deletions

View File

@ -1,129 +0,0 @@
package com.gmail.nossr50.datatypes.dirtydata;
import com.gmail.nossr50.datatypes.mutableprimitives.MutableBoolean;
import com.google.common.base.Objects;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class DirtyDataSet<E> {
private final @NotNull MutableBoolean dirtyFlag; //Can be pointed at a reference
private @NotNull Set<E> dataSet;
public DirtyDataSet(@NotNull Set<E> data, @NotNull MutableBoolean referenceFlag) {
this.dataSet = data;
this.dirtyFlag = referenceFlag;
}
public boolean isDirty() {
return dirtyFlag.getImmutableCopy();
}
private void setDirty() {
dirtyFlag.setBoolean(true);
}
public void setData(@NotNull Set<E> dataSet) {
this.dataSet = dataSet;
setDirty();
}
public @NotNull Set<E> getDataSet() {
setDirty();
return dataSet;
}
/* Set Interface Delegates */
public int size() {
return dataSet.size();
}
public boolean isEmpty() {
return dataSet.isEmpty();
}
public boolean contains(Object o) {
return dataSet.contains(o);
}
public Iterator<E> iterator() {
return dataSet.iterator();
}
public Object[] toArray() {
return dataSet.toArray();
}
public <T> T[] toArray(@NotNull T[] ts) {
return dataSet.toArray(ts);
}
public boolean add(E e) {
return dataSet.add(e);
}
public boolean remove(Object o) {
return dataSet.remove(o);
}
public boolean containsAll(@NotNull Collection<? extends E> collection) {
return dataSet.containsAll(collection);
}
public boolean addAll(@NotNull Collection<? extends E> collection) {
return dataSet.addAll(collection);
}
public boolean retainAll(@NotNull Collection<? extends E> collection) {
return dataSet.retainAll(collection);
}
public boolean removeAll(@NotNull Collection<? extends E> collection) {
return dataSet.removeAll(collection);
}
public void clear() {
dataSet.clear();
}
public Spliterator<E> spliterator() {
return dataSet.spliterator();
}
public boolean removeIf(Predicate<? super E> filter) {
return dataSet.removeIf(filter);
}
public Stream<E> stream() {
return dataSet.stream();
}
public Stream<E> parallelStream() {
return dataSet.parallelStream();
}
public void forEach(Consumer<? super E> action) {
dataSet.forEach(action);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DirtyDataSet<?> that = (DirtyDataSet<?>) o;
return Objects.equal(getDataSet(), that.getDataSet());
}
@Override
public int hashCode() {
return Objects.hashCode(getDataSet());
}
}

View File

@ -54,10 +54,12 @@ public class DirtyMap<K, V> implements Map<K, V> {
return map.get(key);
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@ -72,21 +74,25 @@ public class DirtyMap<K, V> implements Map<K, V> {
return map.containsValue(value);
}
@Override
public V put(K key, V value) {
setDirty();
return map.put(key, value);
}
@Override
public V remove(Object key) {
setDirty();
return map.remove(key);
}
@Override
public void putAll(@NotNull Map<? extends K, ? extends V> m) {
setDirty();
map.putAll(m);
}
@Override
public void clear() {
setDirty();
map.clear();

View File

@ -0,0 +1,164 @@
package com.gmail.nossr50.datatypes.dirtydata;
import com.gmail.nossr50.datatypes.mutableprimitives.MutableBoolean;
import com.google.common.base.Objects;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class DirtySet<E> implements Set<E> {
private final @NotNull MutableBoolean dirtyFlag; //Can be pointed at a reference
private @NotNull Set<E> set;
public DirtySet(@NotNull Set<E> data, @NotNull MutableBoolean referenceFlag) {
this.set = data;
this.dirtyFlag = referenceFlag;
}
public boolean isDirty() {
return dirtyFlag.getImmutableCopy();
}
private void setDirty() {
dirtyFlag.setBoolean(true);
}
/**
* Assign the inner wrapped set to a new value
* @param dataSet the new value to assign the inner wrapped set
*/
public void setSet(@NotNull Set<E> dataSet) {
this.set = dataSet;
setDirty();
}
/**
* Get the wrapped set of this DirtySet
* @return the inner wrapped Set of this DirtySet
*/
public @NotNull Set<E> unwrapSet() {
setDirty();
return set;
}
/* Set Interface Delegates */
@Override
public int size() {
return set.size();
}
@Override
public boolean isEmpty() {
return set.isEmpty();
}
@Override
public boolean contains(Object o) {
return set.contains(o);
}
@Override
public @NotNull Iterator<E> iterator() {
return set.iterator();
}
@Override
public Object[] toArray() {
return set.toArray();
}
@Override
public <T> T[] toArray(@NotNull T[] ts) {
return set.toArray(ts);
}
@Override
public boolean add(E e) {
setDirty();
return set.add(e);
}
@Override
public boolean remove(Object o) {
setDirty();
return set.remove(o);
}
@Override
public boolean containsAll(@NotNull Collection<?> collection) {
return set.containsAll(collection);
}
@Override
public boolean addAll(@NotNull Collection<? extends E> collection) {
setDirty();
return set.addAll(collection);
}
@Override
public boolean retainAll(@NotNull Collection<?> collection) {
setDirty();
return set.retainAll(collection);
}
@Override
public boolean removeAll(@NotNull Collection<?> collection) {
setDirty();
return set.removeAll(collection);
}
@Override
public void clear() {
setDirty();
set.clear();
}
@Override
public Spliterator<E> spliterator() {
setDirty();
return set.spliterator();
}
@Override
public boolean removeIf(Predicate<? super E> filter) {
return set.removeIf(filter);
}
@Override
public Stream<E> stream() {
return set.stream();
}
@Override
public Stream<E> parallelStream() {
return set.parallelStream();
}
@Override
public void forEach(Consumer<? super E> action) {
set.forEach(action);
}
/* Equals & Hash Overrides */
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DirtySet<?> dirtySet = (DirtySet<?>) o;
return Objects.equal(set, dirtySet.set);
}
@Override
public int hashCode() {
return Objects.hashCode(set);
}
}

View File

@ -1,7 +1,7 @@
package com.gmail.nossr50.datatypes.party;
import com.gmail.nossr50.datatypes.dirtydata.DirtyData;
import com.gmail.nossr50.datatypes.dirtydata.DirtyDataSet;
import com.gmail.nossr50.datatypes.dirtydata.DirtySet;
import com.gmail.nossr50.datatypes.mutableprimitives.MutableBoolean;
import com.gmail.nossr50.datatypes.mutableprimitives.MutableString;
import com.google.common.base.Objects;
@ -14,13 +14,13 @@ public class PersistentPartyData {
private final @NotNull MutableBoolean dirtyFlag; //Dirty values in this class will change this flag as needed
private final @NotNull DirtyData<MutableString> partyName;
private final @NotNull DirtyDataSet<PartyMember> partyMembers; //TODO: Add cache for subsets
private final @NotNull DirtySet<PartyMember> partyMembers; //TODO: Add cache for subsets
public PersistentPartyData(@NotNull String partyName,
@NotNull Set<PartyMember> partyMembers) {
dirtyFlag = new MutableBoolean(false);
this.partyName = new DirtyData<>(new MutableString(partyName), dirtyFlag);
this.partyMembers = new DirtyDataSet<>(new HashSet<>(partyMembers), dirtyFlag);
this.partyMembers = new DirtySet<>(new HashSet<>(partyMembers), dirtyFlag);
}
public String getPartyName() {
@ -28,7 +28,7 @@ public class PersistentPartyData {
}
public Set<PartyMember> getPartyMembers() {
return partyMembers.getDataSet();
return partyMembers;
}
public boolean isDataDirty() {

View File

@ -5,7 +5,7 @@ import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.MobHealthBarType;
import com.gmail.nossr50.datatypes.dirtydata.DirtyData;
import com.gmail.nossr50.datatypes.dirtydata.DirtyDataMap;
import com.gmail.nossr50.datatypes.dirtydata.DirtyMap;
import com.gmail.nossr50.datatypes.mutableprimitives.MutableBoolean;
import com.gmail.nossr50.datatypes.mutableprimitives.MutableInteger;
import com.gmail.nossr50.datatypes.mutableprimitives.MutableLong;
@ -38,11 +38,11 @@ public class PersistentPlayerData {
private final @NotNull DirtyData<MobHealthBarType> mobHealthBarType;
/* Skill Data */
private final @NotNull DirtyDataMap<PrimarySkillType, Integer> skillLevelValues;
private final @NotNull DirtyDataMap<PrimarySkillType, Float> skillExperienceValues;
private final @NotNull DirtyDataMap<SuperAbilityType, Integer> abilityDeactivationTimestamps; // Ability & Cooldown
private final @NotNull DirtyDataMap<UniqueDataType, Integer> uniquePlayerData; //Misc data that doesn't fit into other categories (chimaera wing, etc..)
private final @NotNull DirtyDataMap<PrimarySkillType, MMOExperienceBarManager.BarState> barStateMap;
private final @NotNull DirtyMap<PrimarySkillType, Integer> skillLevelValues;
private final @NotNull DirtyMap<PrimarySkillType, Float> skillExperienceValues;
private final @NotNull DirtyMap<SuperAbilityType, Integer> abilityDeactivationTimestamps; // Ability & Cooldown
private final @NotNull DirtyMap<UniqueDataType, Integer> uniquePlayerData; //Misc data that doesn't fit into other categories (chimaera wing, etc..)
private final @NotNull DirtyMap<PrimarySkillType, MMOExperienceBarManager.BarState> barStateMap;
/* Special Flags */
private final @NotNull DirtyData<MutableBoolean> partyChatSpying;
@ -66,10 +66,10 @@ public class PersistentPlayerData {
this.playerUUID = playerUUID;
this.playerName = new DirtyData<>(new MutableString(playerName), dirtyFlag);
this.skillLevelValues = new DirtyDataMap<>(new EnumMap<>(PrimarySkillType.class), dirtyFlag);
this.skillExperienceValues = new DirtyDataMap<>(new EnumMap<>(PrimarySkillType.class), dirtyFlag);
this.abilityDeactivationTimestamps = new DirtyDataMap<>(new EnumMap<>(SuperAbilityType.class), dirtyFlag);
this.uniquePlayerData = new DirtyDataMap<>(new EnumMap<>(UniqueDataType.class), dirtyFlag);
this.skillLevelValues = new DirtyMap<>(new EnumMap<>(PrimarySkillType.class), dirtyFlag);
this.skillExperienceValues = new DirtyMap<>(new EnumMap<>(PrimarySkillType.class), dirtyFlag);
this.abilityDeactivationTimestamps = new DirtyMap<>(new EnumMap<>(SuperAbilityType.class), dirtyFlag);
this.uniquePlayerData = new DirtyMap<>(new EnumMap<>(UniqueDataType.class), dirtyFlag);
this.mobHealthBarType = new DirtyData<>(Config.getInstance().getMobHealthbarDefault(), dirtyFlag);
this.scoreboardTipsShown = new DirtyData<>(new MutableInteger(0), dirtyFlag);
@ -88,7 +88,7 @@ public class PersistentPlayerData {
this.partyChatSpying = new DirtyData<>(new MutableBoolean(false), dirtyFlag);
this.barStateMap = new DirtyDataMap<>(MMOExperienceBarManager.generateDefaultBarStateMap(), dirtyFlag);
this.barStateMap = new DirtyMap<>(MMOExperienceBarManager.generateDefaultBarStateMap(), dirtyFlag);
this.lastLogin = new DirtyData<>(new MutableLong(0), dirtyFlag); //Value of 0 will represent that the user hasn't been seen online
this.leaderBoardExclusion = new DirtyData<>(new MutableBoolean(false), dirtyFlag);
}
@ -128,22 +128,22 @@ public class PersistentPlayerData {
this.dirtyFlag = new MutableBoolean(false); //Set this one first
validateMap(skillLevelValues);
this.skillLevelValues = new DirtyDataMap<>(skillLevelValues, dirtyFlag);
this.skillLevelValues = new DirtyMap<>(skillLevelValues, dirtyFlag);
validateMap(skillExperienceValues);
this.skillExperienceValues = new DirtyDataMap<>(skillExperienceValues, dirtyFlag);
this.skillExperienceValues = new DirtyMap<>(skillExperienceValues, dirtyFlag);
validateMap(abilityDeactivationTimestamps);
this.abilityDeactivationTimestamps = new DirtyDataMap<>(abilityDeactivationTimestamps, dirtyFlag);
this.abilityDeactivationTimestamps = new DirtyMap<>(abilityDeactivationTimestamps, dirtyFlag);
this.uniquePlayerData = new DirtyDataMap<>(uniquePlayerData, dirtyFlag);
this.uniquePlayerData = new DirtyMap<>(uniquePlayerData, dirtyFlag);
this.scoreboardTipsShown = new DirtyData<>(new MutableInteger(scoreboardTipsShown), dirtyFlag);
this.mobHealthBarType = new DirtyData<>(mobHealthBarType, dirtyFlag);
this.playerUUID = playerUUID;
this.playerName = new DirtyData<>(new MutableString(playerName), dirtyFlag);
this.barStateMap = new DirtyDataMap<>(barStateMap, dirtyFlag);
this.barStateMap = new DirtyMap<>(barStateMap, dirtyFlag);
this.partyChatSpying = new DirtyData<>(new MutableBoolean(partyChatSpying), dirtyFlag);
this.lastLogin = new DirtyData<>(new MutableLong(lastLogin), dirtyFlag);
@ -347,30 +347,30 @@ public class PersistentPlayerData {
* @return the bar state map for this player
*/
public @NotNull Map<PrimarySkillType, MMOExperienceBarManager.BarState> getBarStateMap() {
return barStateMap.getDataMap();
}
/**
* Get the {@link DirtyDataMap} for the related {@link com.gmail.nossr50.util.experience.MMOExperienceBarManager.BarState}'s of this player
* @return the dirty bar state map for this player
*/
public @NotNull DirtyDataMap<PrimarySkillType, MMOExperienceBarManager.BarState> getDirtyBarStateMap() {
return barStateMap;
}
/**
* Get the {@link DirtyDataMap} for the skill levels of this player
* Get the {@link DirtyMap} for the related {@link com.gmail.nossr50.util.experience.MMOExperienceBarManager.BarState}'s of this player
* @return the dirty bar state map for this player
*/
public @NotNull DirtyMap<PrimarySkillType, MMOExperienceBarManager.BarState> getDirtyBarStateMap() {
return barStateMap;
}
/**
* Get the {@link DirtyMap} for the skill levels of this player
* @return the dirty skill level map for this player
*/
public @NotNull DirtyDataMap<PrimarySkillType, Integer> getDirtySkillLevelMap() {
public @NotNull DirtyMap<PrimarySkillType, Integer> getDirtySkillLevelMap() {
return skillLevelValues;
}
/**
* Get the {@link DirtyDataMap} for the skill experience values of this player
* Get the {@link DirtyMap} for the skill experience values of this player
* @return the dirty skill experience values map for this player
*/
public @NotNull DirtyDataMap<PrimarySkillType, Float> getDirtyExperienceValueMap() {
public @NotNull DirtyMap<PrimarySkillType, Float> getDirtyExperienceValueMap() {
return skillExperienceValues;
}
@ -387,7 +387,7 @@ public class PersistentPlayerData {
* @return the map of skill levels for this player
*/
public @NotNull Map<PrimarySkillType, Integer> getSkillLevelsMap() {
return skillLevelValues.getDataMap();
return skillLevelValues;
}
/**
@ -395,7 +395,7 @@ public class PersistentPlayerData {
* @return the experience values map for this player
*/
public @NotNull Map<PrimarySkillType, Float> getSkillsExperienceMap() {
return skillExperienceValues.getDataMap();
return skillExperienceValues;
}
/**
@ -403,7 +403,7 @@ public class PersistentPlayerData {
* @return the ability deactivation timestamps map for this player
*/
public @NotNull Map<SuperAbilityType, Integer> getAbilityDeactivationTimestamps() {
return abilityDeactivationTimestamps.getDataMap();
return abilityDeactivationTimestamps;
}
/**
@ -411,7 +411,7 @@ public class PersistentPlayerData {
* @return a map of unique data for this player
*/
public @NotNull Map<UniqueDataType, Integer> getUniquePlayerData() {
return uniquePlayerData.getDataMap();
return uniquePlayerData;
}
/**