comparison API/Objects/Requirement/AbstractUnitRequirement.cs @ 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 680db2462e34
children 7b9ff7b1df24
comparison
equal deleted inserted replaced
461:f0594621e4a0 462:159dc9be36c2
34 { 34 {
35 return false; 35 return false;
36 } 36 }
37 else if (obj.GetType().Equals(this.GetType())) 37 else if (obj.GetType().Equals(this.GetType()))
38 { 38 {
39 return TypeEquals(obj); 39 return TypeEquals((AbstractUnitRequirement<OBJECT_TYPE>)obj);
40 } 40 }
41 else 41 else
42 { 42 {
43 return false; 43 return false;
44 } 44 }
45 } 45 }
46 46
47 public override abstract int GetHashCode(); 47 protected virtual bool TypeEquals(AbstractUnitRequirement<OBJECT_TYPE> obj)
48 {
49 AbstractUnitRequirement<OBJECT_TYPE> otherReq = obj as AbstractUnitRequirement<OBJECT_TYPE>;
50 if (!this.AllowedObject.Equals(otherReq.AllowedObject))
51 {
52 return false;
53 }
54 else if (!Collections.Collections.AreEqual(ConstraintTypes, otherReq.ConstraintTypes))
55 {
56 return false;
57 }
58 else
59 {
60 return true;
61 }
62 }
48 63
49 /// <summary> 64 public override int GetHashCode()
50 /// Type-specific equality checking - must be implemented by each class 65 {
51 /// </summary> 66 int hash = GetType().FullName.GetHashCode() + AllowedObject.GetHashCode();
52 /// <returns> 67
53 /// <code>true</code> if this object is equal to <code>obj</code>, else <code>false</code> 68 foreach (UnitCountRequirementData limit in ConstraintTypes)
54 /// </returns> 69 {
55 /// <param name='obj'> 70 hash += limit.GetHashCode();
56 /// The object to compare to 71 }
57 /// </param> 72
58 protected abstract bool TypeEquals(object obj); 73 return hash;
74 }
59 75
60 protected virtual bool IsApplicable(IWarFoundryObject toObjectAdded, Army toArmy) 76 protected virtual bool IsApplicable(IWarFoundryObject toObjectAdded, Army toArmy)
61 { 77 {
62 return IsApplicable(toArmy) || IsApplicable(toObjectAdded); 78 return IsApplicable(toArmy) || IsApplicable(toObjectAdded);
63 } 79 }