diff API/Objects/Requirement/AbstractRequirement.cs @ 358:dbe7ccb1e557

Re #345: Add failure message to requirements * Add abstract requirement class to handle some of the message commonality
author IBBoard <dev@ibboard.co.uk>
date Sun, 01 May 2011 13:56:20 +0000
parents
children 0dd8dbe8afe9
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/API/Objects/Requirement/AbstractRequirement.cs	Sun May 01 13:56:20 2011 +0000
@@ -0,0 +1,73 @@
+// This file (AbstractRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
+// 
+// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
+using System;
+
+namespace IBBoard.WarFoundry.API.Objects.Requirement
+{
+	public abstract class AbstractRequirement : IRequirement
+	{
+		public override bool Equals (object obj)
+		{
+			if (obj == null)
+			{
+				return false;
+			}
+			else if (obj.GetType().Equals(this.GetType()))
+			{
+				return TypeEquals(obj);
+			}
+			else
+			{
+				return false;
+			}
+		}
+
+		/// <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);
+
+		protected virtual bool IsApplicable(WarFoundryObject toObjectAdded, Army toArmy)
+		{
+			return IsApplicable(toArmy) || IsApplicable(toObjectAdded);
+		}
+
+		protected virtual bool IsApplicable(Army toArmy)
+		{
+			return true;
+		}
+
+		protected virtual bool IsApplicable(WarFoundryObject toObject)
+		{
+			return true;
+		}
+
+
+		public string GetValidationMessage (Army army)
+		{
+			string message = "";
+
+			var obj = ValidatesArmy(army);
+			if (!Validates.AsOkay(obj))
+			{
+				message = GetValidationFailedMessage(army);
+			}
+
+			return message;
+		}
+
+		protected abstract string GetValidationFailedMessage (Army army);
+
+		public abstract  Validation AllowsAdding (WarFoundryObject wfObject, Army toArmy);
+
+		public abstract  Validation ValidatesArmy (Army army);
+	}
+}
+