comparison 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
comparison
equal deleted inserted replaced
357:50d0d3b39a0b 358:dbe7ccb1e557
1 // This file (AbstractRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
2 //
3 // 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.
4 using System;
5
6 namespace IBBoard.WarFoundry.API.Objects.Requirement
7 {
8 public abstract class AbstractRequirement : IRequirement
9 {
10 public override bool Equals (object obj)
11 {
12 if (obj == null)
13 {
14 return false;
15 }
16 else if (obj.GetType().Equals(this.GetType()))
17 {
18 return TypeEquals(obj);
19 }
20 else
21 {
22 return false;
23 }
24 }
25
26 /// <summary>
27 /// Type-specific equality checking - must be implemented by each class
28 /// </summary>
29 /// <returns>
30 /// <code>true</code> if this object is equal to <code>obj</code>, else <code>false</code>
31 /// </returns>
32 /// <param name='obj'>
33 /// The object to compare to
34 /// </param>
35 protected abstract bool TypeEquals(object obj);
36
37 protected virtual bool IsApplicable(WarFoundryObject toObjectAdded, Army toArmy)
38 {
39 return IsApplicable(toArmy) || IsApplicable(toObjectAdded);
40 }
41
42 protected virtual bool IsApplicable(Army toArmy)
43 {
44 return true;
45 }
46
47 protected virtual bool IsApplicable(WarFoundryObject toObject)
48 {
49 return true;
50 }
51
52
53 public string GetValidationMessage (Army army)
54 {
55 string message = "";
56
57 var obj = ValidatesArmy(army);
58 if (!Validates.AsOkay(obj))
59 {
60 message = GetValidationFailedMessage(army);
61 }
62
63 return message;
64 }
65
66 protected abstract string GetValidationFailedMessage (Army army);
67
68 public abstract Validation AllowsAdding (WarFoundryObject wfObject, Army toArmy);
69
70 public abstract Validation ValidatesArmy (Army army);
71 }
72 }
73