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