# HG changeset patch # User IBBoard # Date 1331065891 0 # Node ID 159dc9be36c285a588dacafeae158b1e433bb31d # Parent f0594621e4a0c7b450eca443e78950b03882a60e Re #379: Fix requirements * Move equality and hashcode up to AbstractUnitRequirement for consistency diff -r f0594621e4a0 -r 159dc9be36c2 API/Objects/Requirement/AbstractUnitRequirement.cs --- a/API/Objects/Requirement/AbstractUnitRequirement.cs Mon Mar 05 19:52:54 2012 +0000 +++ b/API/Objects/Requirement/AbstractUnitRequirement.cs Tue Mar 06 20:31:31 2012 +0000 @@ -36,7 +36,7 @@ } else if (obj.GetType().Equals(this.GetType())) { - return TypeEquals(obj); + return TypeEquals((AbstractUnitRequirement)obj); } else { @@ -44,18 +44,34 @@ } } - public override abstract int GetHashCode(); + protected virtual bool TypeEquals(AbstractUnitRequirement obj) + { + AbstractUnitRequirement otherReq = obj as AbstractUnitRequirement; + if (!this.AllowedObject.Equals(otherReq.AllowedObject)) + { + return false; + } + else if (!Collections.Collections.AreEqual(ConstraintTypes, otherReq.ConstraintTypes)) + { + return false; + } + else + { + return true; + } + } - /// - /// Type-specific equality checking - must be implemented by each class - /// - /// - /// true if this object is equal to obj, else false - /// - /// - /// The object to compare to - /// - protected abstract bool TypeEquals(object obj); + public override int GetHashCode() + { + int hash = GetType().FullName.GetHashCode() + AllowedObject.GetHashCode(); + + foreach (UnitCountRequirementData limit in ConstraintTypes) + { + hash += limit.GetHashCode(); + } + + return hash; + } protected virtual bool IsApplicable(IWarFoundryObject toObjectAdded, Army toArmy) { diff -r f0594621e4a0 -r 159dc9be36c2 API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs --- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs Mon Mar 05 19:52:54 2012 +0000 +++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs Tue Mar 06 20:31:31 2012 +0000 @@ -30,31 +30,6 @@ } } - protected override bool TypeEquals(object obj) - { - RequiresAtLeastNUnitsRequirement otherReq = obj as RequiresAtLeastNUnitsRequirement; - if (!Collections.Collections.AreEqual(ConstraintTypes, otherReq.ConstraintTypes)) - { - return false; - } - else - { - return true; - } - } - - public override int GetHashCode() - { - int hash = 0; - - foreach (UnitCountRequirementData req in ConstraintTypes) - { - hash += req.UnitType.GetHashCode(); - } - - return hash; - } - /// /// Checks whether the supplied WarFoundryObject can be added to the supplied army. /// diff -r f0594621e4a0 -r 159dc9be36c2 API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs --- a/API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs Mon Mar 05 19:52:54 2012 +0000 +++ b/API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs Tue Mar 06 20:31:31 2012 +0000 @@ -26,19 +26,6 @@ return hash; } - protected override bool TypeEquals(object obj) - { - RequiresNUnitsForMObjectsRequirement otherReq = (RequiresNUnitsForMObjectsRequirement)obj; - if (!Collections.Collections.AreEqual(ConstraintTypes, otherReq.ConstraintTypes)) - { - return false; - } - else - { - return true; - } - } - protected override string GetFailedAddingRequirementsString(IWarFoundryObject toAdd, Army toArmy) { return String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray()); diff -r f0594621e4a0 -r 159dc9be36c2 API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs --- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs Mon Mar 05 19:52:54 2012 +0000 +++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs Tue Mar 06 20:31:31 2012 +0000 @@ -163,24 +163,6 @@ return canAdd; } - protected override bool TypeEquals(object obj) - { - RequiresNoMoreThanNOfUnitTypeRequirement other = (RequiresNoMoreThanNOfUnitTypeRequirement)obj; - return Collections.Collections.AreEqual(ConstraintTypes, other.ConstraintTypes); - } - - public override int GetHashCode() - { - int hash = 0; - - foreach (UnitCountRequirementData limit in ConstraintTypes) - { - hash += limit.UnitType.GetHashCode(); - } - - return hash; - } - protected override string ValidationFailedMessage { get