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 wrap: on
line diff
--- 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<OBJECT_TYPE>)obj);
 			}
 			else
 			{
@@ -44,18 +44,34 @@
 			}
 		}
 
-		public override abstract int GetHashCode();
+		protected virtual bool TypeEquals(AbstractUnitRequirement<OBJECT_TYPE> obj)
+		{
+			AbstractUnitRequirement<OBJECT_TYPE> otherReq = obj as AbstractUnitRequirement<OBJECT_TYPE>;
+			if (!this.AllowedObject.Equals(otherReq.AllowedObject))
+			{
+				return false;
+			}
+			else if (!Collections.Collections.AreEqual(ConstraintTypes, otherReq.ConstraintTypes))
+			{
+				return false;
+			}
+			else
+			{
+				return true;
+			}
+		}
 
-		/// <summary>
-		/// Type-specific equality checking - must be implemented by each class
-		/// </summary>
-		/// <returns>
-		/// <code>true</code> if this object is equal to <code>obj</code>, else <code>false</code>
-		/// </returns>
-		/// <param name='obj'>
-		/// The object to compare to
-		/// </param>
-		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)
 		{
--- 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<OBJECT_TYPE> otherReq = obj as RequiresAtLeastNUnitsRequirement<OBJECT_TYPE>;
-			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;
-		}
-
 		/// <summary>
 		/// Checks whether the supplied WarFoundryObject can be added to the supplied army.
 		/// </summary>
--- 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<OBJECT_TYPE> otherReq = (RequiresNUnitsForMObjectsRequirement<OBJECT_TYPE>)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());
--- 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<OBJECT_TYPE> other = (RequiresNoMoreThanNOfUnitTypeRequirement<OBJECT_TYPE>)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