using System; using System.Threading; namespace org.hwo.contracts { public delegate void operDelegate(); public delegate T assignDelegate(); public class Conditional { T valSuccess, valFail; bool success; public Conditional(T valSuccess,T valFail) { this.valSuccess = valSuccess; this.valFail = valFail; this.success = true; } public Conditional requires(bool condition){ if (success){ success = condition; } return this; } public Conditional sets(ref ST target,ST value){ if (success){ target = value; } return this; } public Conditional does(operDelegate operation){ if (this.success){ operation(); } return this; } public Conditional assigns(ref AT target,assignDelegate assigner) { if (success){ target = assigner(); } return this; } public Conditional requiresValid(ref AT target,assignDelegate assigner){ if (success){ target = assigner(); success = target != null; } return this; } public bool isSuccess(){ return this.success; } public Conditional throws(string message) where E: Exception{ if (!success){ E ex = (E)typeof(E).GetConstructor(new Type[]{typeof(string)}).Invoke(new object[]{message}); throw ex; } return this; } public Conditional throws() where E: Exception{ if (!success){ E ex = (E)typeof(E).GetConstructor(new Type[]{}).Invoke(new object[]{}); throw ex; } return this; } } public class BooleanConditional : Conditional{ public BooleanConditional() :base(true,false){ } } }