changeset 462:159dc9be36c2

Re #379: Fix requirements * Move equality and hashcode up to AbstractUnitRequirement for consistency
author IBBoard <dev@ibboard.co.uk>
date Tue, 06 Mar 2012 20:31:31 +0000
parents f0594621e4a0
children cbeee87dc2d3
files API/Objects/Requirement/AbstractUnitRequirement.cs API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs
diffstat 4 files changed, 28 insertions(+), 68 deletions(-) [+]
line diff
     1.1 --- a/API/Objects/Requirement/AbstractUnitRequirement.cs	Mon Mar 05 19:52:54 2012 +0000
     1.2 +++ b/API/Objects/Requirement/AbstractUnitRequirement.cs	Tue Mar 06 20:31:31 2012 +0000
     1.3 @@ -36,7 +36,7 @@
     1.4  			}
     1.5  			else if (obj.GetType().Equals(this.GetType()))
     1.6  			{
     1.7 -				return TypeEquals(obj);
     1.8 +				return TypeEquals((AbstractUnitRequirement<OBJECT_TYPE>)obj);
     1.9  			}
    1.10  			else
    1.11  			{
    1.12 @@ -44,18 +44,34 @@
    1.13  			}
    1.14  		}
    1.15  
    1.16 -		public override abstract int GetHashCode();
    1.17 +		protected virtual bool TypeEquals(AbstractUnitRequirement<OBJECT_TYPE> obj)
    1.18 +		{
    1.19 +			AbstractUnitRequirement<OBJECT_TYPE> otherReq = obj as AbstractUnitRequirement<OBJECT_TYPE>;
    1.20 +			if (!this.AllowedObject.Equals(otherReq.AllowedObject))
    1.21 +			{
    1.22 +				return false;
    1.23 +			}
    1.24 +			else if (!Collections.Collections.AreEqual(ConstraintTypes, otherReq.ConstraintTypes))
    1.25 +			{
    1.26 +				return false;
    1.27 +			}
    1.28 +			else
    1.29 +			{
    1.30 +				return true;
    1.31 +			}
    1.32 +		}
    1.33  
    1.34 -		/// <summary>
    1.35 -		/// Type-specific equality checking - must be implemented by each class
    1.36 -		/// </summary>
    1.37 -		/// <returns>
    1.38 -		/// <code>true</code> if this object is equal to <code>obj</code>, else <code>false</code>
    1.39 -		/// </returns>
    1.40 -		/// <param name='obj'>
    1.41 -		/// The object to compare to
    1.42 -		/// </param>
    1.43 -		protected abstract bool TypeEquals(object obj);
    1.44 +		public override int GetHashCode()
    1.45 +		{
    1.46 +			int hash = GetType().FullName.GetHashCode() + AllowedObject.GetHashCode();
    1.47 +
    1.48 +			foreach (UnitCountRequirementData limit in ConstraintTypes)
    1.49 +			{
    1.50 +				hash += limit.GetHashCode();
    1.51 +			}
    1.52 +
    1.53 +			return hash;
    1.54 +		}
    1.55  
    1.56  		protected virtual bool IsApplicable(IWarFoundryObject toObjectAdded, Army toArmy)
    1.57  		{
     2.1 --- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Mon Mar 05 19:52:54 2012 +0000
     2.2 +++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Tue Mar 06 20:31:31 2012 +0000
     2.3 @@ -30,31 +30,6 @@
     2.4  			}
     2.5  		}
     2.6  
     2.7 -		protected override bool TypeEquals(object obj)
     2.8 -		{
     2.9 -			RequiresAtLeastNUnitsRequirement<OBJECT_TYPE> otherReq = obj as RequiresAtLeastNUnitsRequirement<OBJECT_TYPE>;
    2.10 -			if (!Collections.Collections.AreEqual(ConstraintTypes, otherReq.ConstraintTypes))
    2.11 -			{
    2.12 -				return false;
    2.13 -			}
    2.14 -			else
    2.15 -			{
    2.16 -				return true;
    2.17 -			}
    2.18 -		}
    2.19 -
    2.20 -		public override int GetHashCode()
    2.21 -		{
    2.22 -			int hash = 0;
    2.23 -
    2.24 -			foreach (UnitCountRequirementData req in ConstraintTypes)
    2.25 -			{
    2.26 -				hash += req.UnitType.GetHashCode();
    2.27 -			}
    2.28 -
    2.29 -			return hash;
    2.30 -		}
    2.31 -
    2.32  		/// <summary>
    2.33  		/// Checks whether the supplied WarFoundryObject can be added to the supplied army.
    2.34  		/// </summary>
     3.1 --- a/API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs	Mon Mar 05 19:52:54 2012 +0000
     3.2 +++ b/API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs	Tue Mar 06 20:31:31 2012 +0000
     3.3 @@ -26,19 +26,6 @@
     3.4  			return hash;
     3.5  		}
     3.6  
     3.7 -		protected override bool TypeEquals(object obj)
     3.8 -		{
     3.9 -			RequiresNUnitsForMObjectsRequirement<OBJECT_TYPE> otherReq = (RequiresNUnitsForMObjectsRequirement<OBJECT_TYPE>)obj;
    3.10 -			if (!Collections.Collections.AreEqual(ConstraintTypes, otherReq.ConstraintTypes))
    3.11 -			{
    3.12 -				return false;
    3.13 -			}
    3.14 -			else
    3.15 -			{
    3.16 -				return true;
    3.17 -			}
    3.18 -		}
    3.19 -
    3.20  		protected override string GetFailedAddingRequirementsString(IWarFoundryObject toAdd, Army toArmy)
    3.21  		{
    3.22  			return String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray());
     4.1 --- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Mon Mar 05 19:52:54 2012 +0000
     4.2 +++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Tue Mar 06 20:31:31 2012 +0000
     4.3 @@ -163,24 +163,6 @@
     4.4  			return canAdd;
     4.5  		}
     4.6  
     4.7 -		protected override bool TypeEquals(object obj)
     4.8 -		{
     4.9 -			RequiresNoMoreThanNOfUnitTypeRequirement<OBJECT_TYPE> other = (RequiresNoMoreThanNOfUnitTypeRequirement<OBJECT_TYPE>)obj;
    4.10 -			return Collections.Collections.AreEqual(ConstraintTypes, other.ConstraintTypes);
    4.11 -		}
    4.12 -
    4.13 -		public override int GetHashCode()
    4.14 -		{
    4.15 -			int hash = 0;
    4.16 -
    4.17 -			foreach (UnitCountRequirementData limit in ConstraintTypes)
    4.18 -			{
    4.19 -				hash += limit.UnitType.GetHashCode();
    4.20 -			}
    4.21 -
    4.22 -			return hash;
    4.23 -		}
    4.24 -
    4.25  		protected override string ValidationFailedMessage
    4.26  		{
    4.27  			get