Working on the setter pattern some more. The inited check is now built into the entity changed method.

This commit is contained in:
Olof Larsson 2013-04-25 08:34:10 +02:00
parent 26af25f1c5
commit 8e59017cb1
2 changed files with 175 additions and 106 deletions

View File

@ -49,9 +49,9 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
this.setPowerBoost(that.powerBoost); this.setPowerBoost(that.powerBoost);
this.setOpen(that.open); this.setOpen(that.open);
this.setInvitedPlayerIds(that.invitedPlayerIds); this.setInvitedPlayerIds(that.invitedPlayerIds);
this.setRelationWishes(that.relationWish); this.setRelationWishes(that.relationWishes);
this.setFlags(that.flagOverrides); this.setFlags(that.flags);
this.setPerms(that.permOverrides); this.setPerms(that.perms);
return this; return this;
} }
@ -97,15 +97,18 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
// The keys in this map are factionIds. // The keys in this map are factionIds.
// Null means no special relation whishes. // Null means no special relation whishes.
private Map<String, Rel> relationWish = null; @SerializedName("relationWish")
private Map<String, Rel> relationWishes = null;
// The flag overrides are modifications to the default values. // The flag overrides are modifications to the default values.
// Null means default for the universe. // Null means default for the universe.
private Map<FFlag, Boolean> flagOverrides = null; @SerializedName("flagOverrides")
private Map<FFlag, Boolean> flags = null;
// The perm overrides are modifications to the default values. // The perm overrides are modifications to the default values.
// Null means default for the universe. // Null means default for the universe.
private Map<FPerm, Set<Rel>> permOverrides = null; @SerializedName("permOverrides")
private Map<FPerm, Set<Rel>> perms = null;
// -------------------------------------------- // // -------------------------------------------- //
// FIELD: id // FIELD: id
@ -142,15 +145,18 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
return ret; return ret;
} }
public void setName(String str) public void setName(String name)
{ {
UConf uconf = UConf.get(this); // Clean input
if (uconf != null && UConf.get(this).factionNameForceUpperCase) String target = name;
{
str = str.toUpperCase(); // Detect Nochange
} if (MUtil.equals(this.name, target)) return;
this.name = str; // Apply
this.name = target;
// Mark as changed
this.changed(); this.changed();
} }
@ -191,16 +197,25 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
public void setDescription(String description) 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. // 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(); this.changed();
} }
@ -236,7 +251,16 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
public void setHome(PS home) 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(); this.changed();
} }
@ -255,8 +279,18 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
public void setPowerBoost(Double powerBoost) public void setPowerBoost(Double powerBoost)
{ {
if (powerBoost == null || powerBoost == 0) powerBoost = null; // Clean input
this.powerBoost = powerBoost; 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(); this.changed();
} }
@ -280,7 +314,16 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
public void setOpen(boolean open) 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(); this.changed();
} }
@ -299,19 +342,28 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
public void setInvitedPlayerIds(Collection<String> invitedPlayerIds) public void setInvitedPlayerIds(Collection<String> invitedPlayerIds)
{ {
// Clean input
TreeSet<String> target;
if (invitedPlayerIds == null || invitedPlayerIds.isEmpty()) if (invitedPlayerIds == null || invitedPlayerIds.isEmpty())
{ {
this.invitedPlayerIds = null; target = null;
} }
else else
{ {
TreeSet<String> target = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); target = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
for (String invitedPlayerId : invitedPlayerIds) for (String invitedPlayerId : invitedPlayerIds)
{ {
target.add(invitedPlayerId.toLowerCase()); 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(); this.changed();
} }
@ -358,20 +410,30 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
public Map<String, Rel> getRelationWishes() public Map<String, Rel> getRelationWishes()
{ {
Map<String, Rel> ret = new LinkedHashMap<String, Rel>(); Map<String, Rel> ret = new LinkedHashMap<String, Rel>();
if (this.relationWish != null) ret.putAll(this.relationWish); if (this.relationWishes != null) ret.putAll(this.relationWishes);
return ret; return ret;
} }
public void setRelationWishes(Map<String, Rel> relationWishes) public void setRelationWishes(Map<String, Rel> relationWishes)
{ {
// Clean input
Map<String, Rel> target;
if (relationWishes == null || relationWishes.isEmpty()) if (relationWishes == null || relationWishes.isEmpty())
{ {
this.relationWish = null; target = null;
} }
else else
{ {
this.relationWish = relationWishes; target = new LinkedHashMap<String, Rel>(relationWishes);
} }
// Detect Nochange
if (MUtil.equals(this.relationWishes, target)) return;
// Apply
this.relationWishes = target;
// Mark as changed
this.changed(); this.changed();
} }
@ -447,9 +509,9 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
ret.put(fflag, fflag.getDefault(this)); ret.put(fflag, fflag.getDefault(this));
} }
if (this.flagOverrides != null) if (this.flags != null)
{ {
for (Entry<FFlag, Boolean> entry : this.flagOverrides.entrySet()) for (Entry<FFlag, Boolean> entry : this.flags.entrySet())
{ {
ret.put(entry.getKey(), entry.getValue()); ret.put(entry.getKey(), entry.getValue());
} }
@ -460,34 +522,39 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
public void setFlags(Map<FFlag, Boolean> flags) public void setFlags(Map<FFlag, Boolean> flags)
{ {
Map<FFlag, Boolean> target = new LinkedHashMap<FFlag, Boolean>(); // Clean input
Map<FFlag, Boolean> target;
if (flags != null) if (flags == null)
{ {
target.putAll(flags); target = null;
}
if (this.attached() && Factions.get().isDatabaseInitialized())
{
Iterator<Entry<FFlag, Boolean>> iter = target.entrySet().iterator();
while (iter.hasNext())
{
Entry<FFlag, Boolean> entry = iter.next();
if (entry.getKey().getDefault(this) == entry.getValue())
{
iter.remove();
}
}
}
if (target == null || target.isEmpty())
{
this.flagOverrides = null;
} }
else else
{ {
this.flagOverrides = target; target = new LinkedHashMap<FFlag, Boolean>(flags);
if (this.attached() && Factions.get().isDatabaseInitialized())
{
Iterator<Entry<FFlag, Boolean>> iter = target.entrySet().iterator();
while (iter.hasNext())
{
Entry<FFlag, Boolean> 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(); this.changed();
} }
@ -519,9 +586,9 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
ret.put(fperm, fperm.getDefault(this)); ret.put(fperm, fperm.getDefault(this));
} }
if (this.permOverrides != null) if (this.perms != null)
{ {
for (Entry<FPerm, Set<Rel>> entry : this.permOverrides.entrySet()) for (Entry<FPerm, Set<Rel>> entry : this.perms.entrySet())
{ {
ret.put(entry.getKey(), new LinkedHashSet<Rel>(entry.getValue())); ret.put(entry.getKey(), new LinkedHashSet<Rel>(entry.getValue()));
} }
@ -532,37 +599,43 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
public void setPerms(Map<FPerm, Set<Rel>> perms) public void setPerms(Map<FPerm, Set<Rel>> perms)
{ {
Map<FPerm, Set<Rel>> target = new LinkedHashMap<FPerm, Set<Rel>>(); // Clean input
Map<FPerm, Set<Rel>> target;
if (perms != null) if (perms == null)
{ {
target = null;
}
else
{
target = new LinkedHashMap<FPerm, Set<Rel>>();
for (Entry<FPerm, Set<Rel>> entry : perms.entrySet()) for (Entry<FPerm, Set<Rel>> entry : perms.entrySet())
{ {
target.put(entry.getKey(), new LinkedHashSet<Rel>(entry.getValue())); target.put(entry.getKey(), new LinkedHashSet<Rel>(entry.getValue()));
} }
}
if (this.attached() && Factions.get().isDatabaseInitialized())
if (this.attached() && Factions.get().isDatabaseInitialized())
{
Iterator<Entry<FPerm, Set<Rel>>> iter = target.entrySet().iterator();
while (iter.hasNext())
{ {
Entry<FPerm, Set<Rel>> entry = iter.next(); Iterator<Entry<FPerm, Set<Rel>>> iter = target.entrySet().iterator();
if (entry.getKey().getDefault(this).equals(entry.getValue())) while (iter.hasNext())
{ {
iter.remove(); Entry<FPerm, Set<Rel>> entry = iter.next();
if (entry.getKey().getDefault(this).equals(entry.getValue()))
{
iter.remove();
}
} }
if (target.isEmpty()) target = null;
} }
} }
if (target == null || target.isEmpty()) // Detect Nochange
{ if (MUtil.equals(this.perms, target)) return;
this.permOverrides = null;
} // Apply
else this.perms = target;
{
this.permOverrides = target; // Mark as changed
}
this.changed(); this.changed();
} }

View File

@ -20,6 +20,7 @@ import com.massivecraft.mcore.mixin.Mixin;
import com.massivecraft.mcore.ps.PS; import com.massivecraft.mcore.ps.PS;
import com.massivecraft.mcore.ps.PSFormatSlug; import com.massivecraft.mcore.ps.PSFormatSlug;
import com.massivecraft.mcore.store.SenderEntity; import com.massivecraft.mcore.store.SenderEntity;
import com.massivecraft.mcore.util.MUtil;
import com.massivecraft.mcore.util.SenderUtil; import com.massivecraft.mcore.util.SenderUtil;
@ -191,14 +192,17 @@ public class UPlayer extends SenderEntity<UPlayer> implements EconomyParticipato
// This setter is so long because it search for default/null case and takes care of updating the faction member index // 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) public void setFactionId(String factionId)
{ {
// Clean input
String target = factionId;
// Detect Nochange // Detect Nochange
if (this.factionId == factionId) return; if (MUtil.equals(this.factionId, target)) return;
// Get the raw old value // Get the raw old value
String oldFactionId = this.factionId; String oldFactionId = this.factionId;
// Apply // Apply
this.factionId = factionId; this.factionId = target;
// Must be attached and initialized // Must be attached and initialized
if (!this.attached()) return; if (!this.attached()) return;
@ -239,15 +243,14 @@ public class UPlayer extends SenderEntity<UPlayer> implements EconomyParticipato
public void setRole(Rel role) public void setRole(Rel role)
{ {
// Clean input
Rel target = role;
// Detect Nochange // Detect Nochange
if (this.role == role) return; if (MUtil.equals(this.role, target)) return;
// Apply // Apply
this.role = role; this.role = target;
// Must be attached and initialized
if (!this.attached()) return;
if (!Factions.get().isDatabaseInitialized()) return;
// Mark as changed // Mark as changed
this.changed(); this.changed();
@ -271,24 +274,21 @@ public class UPlayer extends SenderEntity<UPlayer> implements EconomyParticipato
public void setTitle(String title) public void setTitle(String title)
{ {
// Clean input // Clean input
if (title != null) String target = title;
if (target != null)
{ {
title = title.trim(); target = target.trim();
if (title.length() == 0) if (target.length() == 0)
{ {
title = null; target = null;
} }
} }
// Detect Nochange // Detect Nochange
if (this.title == title) return; if (MUtil.equals(this.title, target)) return;
// Apply // Apply
this.title = title; this.title = target;
// Must be attached and initialized
if (!this.attached()) return;
if (!Factions.get().isDatabaseInitialized()) return;
// Mark as changed // Mark as changed
this.changed(); this.changed();
@ -308,17 +308,14 @@ public class UPlayer extends SenderEntity<UPlayer> implements EconomyParticipato
public void setPowerBoost(Double powerBoost) public void setPowerBoost(Double powerBoost)
{ {
// Clean input // Clean input
if (powerBoost == null || powerBoost == 0) powerBoost = null; Double target = powerBoost;
if (target == null || target == 0) target = null;
// Detect Nochange // Detect Nochange
if (this.powerBoost == powerBoost) return; if (MUtil.equals(this.powerBoost, target)) return;
// Apply // Apply
this.powerBoost = powerBoost; this.powerBoost = target;
// Must be attached and initialized
if (!this.attached()) return;
if (!Factions.get().isDatabaseInitialized()) return;
// Mark as changed // Mark as changed
this.changed(); this.changed();
@ -386,15 +383,14 @@ public class UPlayer extends SenderEntity<UPlayer> implements EconomyParticipato
public void setPower(Double power) public void setPower(Double power)
{ {
// Clean input
Double target = power;
// Detect Nochange // Detect Nochange
if (this.power == power) return; if (MUtil.equals(this.power, target)) return;
// Apply // Apply
this.power = power; this.power = target;
// Must be attached and initialized
if (!this.attached()) return;
if (!Factions.get().isDatabaseInitialized()) return;
// Mark as changed // Mark as changed
this.changed(); this.changed();