From 8e59017cb17e0cb5ab6fa78685d8895e6c2277f4 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Thu, 25 Apr 2013 08:34:10 +0200 Subject: [PATCH] Working on the setter pattern some more. The inited check is now built into the entity changed method. --- .../massivecraft/factions/entity/Faction.java | 223 ++++++++++++------ .../massivecraft/factions/entity/UPlayer.java | 58 +++-- 2 files changed, 175 insertions(+), 106 deletions(-) diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index 74f9fbeb..0aa58670 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -49,9 +49,9 @@ public class Faction extends Entity implements EconomyParticipator this.setPowerBoost(that.powerBoost); this.setOpen(that.open); this.setInvitedPlayerIds(that.invitedPlayerIds); - this.setRelationWishes(that.relationWish); - this.setFlags(that.flagOverrides); - this.setPerms(that.permOverrides); + this.setRelationWishes(that.relationWishes); + this.setFlags(that.flags); + this.setPerms(that.perms); return this; } @@ -97,15 +97,18 @@ public class Faction extends Entity implements EconomyParticipator // The keys in this map are factionIds. // Null means no special relation whishes. - private Map relationWish = null; + @SerializedName("relationWish") + private Map relationWishes = null; // The flag overrides are modifications to the default values. // Null means default for the universe. - private Map flagOverrides = null; + @SerializedName("flagOverrides") + private Map flags = null; // The perm overrides are modifications to the default values. // Null means default for the universe. - private Map> permOverrides = null; + @SerializedName("permOverrides") + private Map> perms = null; // -------------------------------------------- // // FIELD: id @@ -142,15 +145,18 @@ public class Faction extends Entity implements EconomyParticipator return ret; } - public void setName(String str) + public void setName(String name) { - UConf uconf = UConf.get(this); - if (uconf != null && UConf.get(this).factionNameForceUpperCase) - { - str = str.toUpperCase(); - } + // Clean input + String target = name; + + // Detect Nochange + if (MUtil.equals(this.name, target)) return; - this.name = str; + // Apply + this.name = target; + + // Mark as changed this.changed(); } @@ -191,16 +197,25 @@ public class Faction extends Entity implements EconomyParticipator public void setDescription(String description) { - if (description != null) + // Clean input + String target = description; + if (target != null) { - description = description.trim(); + target = target.trim(); // This code should be kept for a while to clean out the previous default text that was actually stored in the database. - if (description.length() == 0 || description.equalsIgnoreCase("Default faction description :(")) + if (target.length() == 0 || target.equals("Default faction description :(")) { - description = null; + target = null; } } - this.description = description; + + // Detect Nochange + if (MUtil.equals(this.description, target)) return; + + // Apply + this.description = target; + + // Mark as changed this.changed(); } @@ -236,7 +251,16 @@ public class Faction extends Entity implements EconomyParticipator public void setHome(PS home) { - this.home = home; + // Clean input + PS target = home; + + // Detect Nochange + if (MUtil.equals(this.home, target)) return; + + // Apply + this.home = target; + + // Mark as changed this.changed(); } @@ -255,8 +279,18 @@ public class Faction extends Entity implements EconomyParticipator public void setPowerBoost(Double powerBoost) { - if (powerBoost == null || powerBoost == 0) powerBoost = null; - this.powerBoost = powerBoost; + // Clean input + Double target = powerBoost; + + if (target == null || target == 0) target = null; + + // Detect Nochange + if (MUtil.equals(this.powerBoost, target)) return; + + // Apply + this.powerBoost = target; + + // Mark as changed this.changed(); } @@ -280,7 +314,16 @@ public class Faction extends Entity implements EconomyParticipator public void setOpen(boolean open) { - this.open = open; + // Clean input + boolean target = open; + + // Detect Nochange + if (MUtil.equals(this.open, target)) return; + + // Apply + this.open = target; + + // Mark as changed this.changed(); } @@ -299,19 +342,28 @@ public class Faction extends Entity implements EconomyParticipator public void setInvitedPlayerIds(Collection invitedPlayerIds) { + // Clean input + TreeSet target; if (invitedPlayerIds == null || invitedPlayerIds.isEmpty()) { - this.invitedPlayerIds = null; + target = null; } else { - TreeSet target = new TreeSet(String.CASE_INSENSITIVE_ORDER); + target = new TreeSet(String.CASE_INSENSITIVE_ORDER); for (String invitedPlayerId : invitedPlayerIds) { target.add(invitedPlayerId.toLowerCase()); } - this.invitedPlayerIds = target; } + + // Detect Nochange + if (MUtil.equals(this.invitedPlayerIds, target)) return; + + // Apply + this.invitedPlayerIds = target; + + // Mark as changed this.changed(); } @@ -358,20 +410,30 @@ public class Faction extends Entity implements EconomyParticipator public Map getRelationWishes() { Map ret = new LinkedHashMap(); - if (this.relationWish != null) ret.putAll(this.relationWish); + if (this.relationWishes != null) ret.putAll(this.relationWishes); return ret; } public void setRelationWishes(Map relationWishes) { + // Clean input + Map target; if (relationWishes == null || relationWishes.isEmpty()) { - this.relationWish = null; + target = null; } else { - this.relationWish = relationWishes; + target = new LinkedHashMap(relationWishes); } + + // Detect Nochange + if (MUtil.equals(this.relationWishes, target)) return; + + // Apply + this.relationWishes = target; + + // Mark as changed this.changed(); } @@ -447,9 +509,9 @@ public class Faction extends Entity implements EconomyParticipator ret.put(fflag, fflag.getDefault(this)); } - if (this.flagOverrides != null) + if (this.flags != null) { - for (Entry entry : this.flagOverrides.entrySet()) + for (Entry entry : this.flags.entrySet()) { ret.put(entry.getKey(), entry.getValue()); } @@ -460,34 +522,39 @@ public class Faction extends Entity implements EconomyParticipator public void setFlags(Map flags) { - Map target = new LinkedHashMap(); - - if (flags != null) + // Clean input + Map target; + if (flags == null) { - target.putAll(flags); - } - - if (this.attached() && Factions.get().isDatabaseInitialized()) - { - Iterator> iter = target.entrySet().iterator(); - while (iter.hasNext()) - { - Entry entry = iter.next(); - if (entry.getKey().getDefault(this) == entry.getValue()) - { - iter.remove(); - } - } - } - - if (target == null || target.isEmpty()) - { - this.flagOverrides = null; + target = null; } else { - this.flagOverrides = target; + target = new LinkedHashMap(flags); + + if (this.attached() && Factions.get().isDatabaseInitialized()) + { + Iterator> iter = target.entrySet().iterator(); + while (iter.hasNext()) + { + Entry entry = iter.next(); + if (entry.getKey().getDefault(this) == entry.getValue()) + { + iter.remove(); + } + } + + if (target.isEmpty()) target = null; + } } + + // Detect Nochange + if (MUtil.equals(this.flags, target)) return; + + // Apply + this.flags = target; + + // Mark as changed this.changed(); } @@ -519,9 +586,9 @@ public class Faction extends Entity implements EconomyParticipator ret.put(fperm, fperm.getDefault(this)); } - if (this.permOverrides != null) + if (this.perms != null) { - for (Entry> entry : this.permOverrides.entrySet()) + for (Entry> entry : this.perms.entrySet()) { ret.put(entry.getKey(), new LinkedHashSet(entry.getValue())); } @@ -532,37 +599,43 @@ public class Faction extends Entity implements EconomyParticipator public void setPerms(Map> perms) { - Map> target = new LinkedHashMap>(); - - if (perms != null) + // Clean input + Map> target; + if (perms == null) { + target = null; + } + else + { + target = new LinkedHashMap>(); for (Entry> entry : perms.entrySet()) { target.put(entry.getKey(), new LinkedHashSet(entry.getValue())); } - } - - if (this.attached() && Factions.get().isDatabaseInitialized()) - { - Iterator>> iter = target.entrySet().iterator(); - while (iter.hasNext()) + + if (this.attached() && Factions.get().isDatabaseInitialized()) { - Entry> entry = iter.next(); - if (entry.getKey().getDefault(this).equals(entry.getValue())) + Iterator>> iter = target.entrySet().iterator(); + while (iter.hasNext()) { - iter.remove(); + Entry> entry = iter.next(); + if (entry.getKey().getDefault(this).equals(entry.getValue())) + { + iter.remove(); + } } + + if (target.isEmpty()) target = null; } } - if (target == null || target.isEmpty()) - { - this.permOverrides = null; - } - else - { - this.permOverrides = target; - } + // Detect Nochange + if (MUtil.equals(this.perms, target)) return; + + // Apply + this.perms = target; + + // Mark as changed this.changed(); } diff --git a/src/com/massivecraft/factions/entity/UPlayer.java b/src/com/massivecraft/factions/entity/UPlayer.java index 861eae7e..ea99d7b7 100644 --- a/src/com/massivecraft/factions/entity/UPlayer.java +++ b/src/com/massivecraft/factions/entity/UPlayer.java @@ -20,6 +20,7 @@ import com.massivecraft.mcore.mixin.Mixin; import com.massivecraft.mcore.ps.PS; import com.massivecraft.mcore.ps.PSFormatSlug; import com.massivecraft.mcore.store.SenderEntity; +import com.massivecraft.mcore.util.MUtil; import com.massivecraft.mcore.util.SenderUtil; @@ -191,14 +192,17 @@ public class UPlayer extends SenderEntity implements EconomyParticipato // This setter is so long because it search for default/null case and takes care of updating the faction member index public void setFactionId(String factionId) { + // Clean input + String target = factionId; + // Detect Nochange - if (this.factionId == factionId) return; + if (MUtil.equals(this.factionId, target)) return; // Get the raw old value String oldFactionId = this.factionId; // Apply - this.factionId = factionId; + this.factionId = target; // Must be attached and initialized if (!this.attached()) return; @@ -239,15 +243,14 @@ public class UPlayer extends SenderEntity implements EconomyParticipato public void setRole(Rel role) { + // Clean input + Rel target = role; + // Detect Nochange - if (this.role == role) return; + if (MUtil.equals(this.role, target)) return; // Apply - this.role = role; - - // Must be attached and initialized - if (!this.attached()) return; - if (!Factions.get().isDatabaseInitialized()) return; + this.role = target; // Mark as changed this.changed(); @@ -271,24 +274,21 @@ public class UPlayer extends SenderEntity implements EconomyParticipato public void setTitle(String title) { // Clean input - if (title != null) + String target = title; + if (target != null) { - title = title.trim(); - if (title.length() == 0) + target = target.trim(); + if (target.length() == 0) { - title = null; + target = null; } } // Detect Nochange - if (this.title == title) return; + if (MUtil.equals(this.title, target)) return; // Apply - this.title = title; - - // Must be attached and initialized - if (!this.attached()) return; - if (!Factions.get().isDatabaseInitialized()) return; + this.title = target; // Mark as changed this.changed(); @@ -308,17 +308,14 @@ public class UPlayer extends SenderEntity implements EconomyParticipato public void setPowerBoost(Double powerBoost) { // Clean input - if (powerBoost == null || powerBoost == 0) powerBoost = null; + Double target = powerBoost; + if (target == null || target == 0) target = null; // Detect Nochange - if (this.powerBoost == powerBoost) return; + if (MUtil.equals(this.powerBoost, target)) return; // Apply - this.powerBoost = powerBoost; - - // Must be attached and initialized - if (!this.attached()) return; - if (!Factions.get().isDatabaseInitialized()) return; + this.powerBoost = target; // Mark as changed this.changed(); @@ -386,15 +383,14 @@ public class UPlayer extends SenderEntity implements EconomyParticipato public void setPower(Double power) { + // Clean input + Double target = power; + // Detect Nochange - if (this.power == power) return; + if (MUtil.equals(this.power, target)) return; // Apply - this.power = power; - - // Must be attached and initialized - if (!this.attached()) return; - if (!Factions.get().isDatabaseInitialized()) return; + this.power = target; // Mark as changed this.changed();