-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriggers.cs
More file actions
151 lines (118 loc) · 4.38 KB
/
Triggers.cs
File metadata and controls
151 lines (118 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
namespace DataKeeper
{
public class Trigger : DataKeeperElement, IComparable<Trigger>, IComparable
{
public TriggerProperty Property { get; set; }
private Action BindedAction { get; }
#region CONSTRUCTORS
public Trigger(Action action)
: this(Ids.Container.GenerateTriggerId(), action) { }
public Trigger(Action action, TriggerProperty property)
: this(Ids.Container.GenerateTriggerId(), action, property) { }
public Trigger(string id, Action action)
: this(id, action, null) { }
public Trigger(string id, Action action, TriggerProperty property) : base(id)
{
if (action == null)
throw new ArgumentNullException(nameof(action),
$"Parameter \"{nameof(action)}\" can't be null!");
BindedAction = action;
if (property == null)
property = TriggerProperty.DefaultAfterUpdate;
Property = property;
}
#endregion
public bool IsOnUpdate => Property.BindType.BindTypeHasFlag(BindType.OnUpdate);
public bool IsOnRemove => Property.BindType.BindTypeHasFlag(BindType.OnRemove);
public bool IsAfter => Property.TriggerType.TriggerHasFlag(TriggerType.After);
public bool IsBefore => Property.TriggerType.TriggerHasFlag(TriggerType.Before);
public void Apply()
{
if (Property.ActivityStatus == ActivityStatus.Active)
BindedAction?.Invoke();
}
public Trigger Copy()
{
Trigger result = new Trigger(idPrefix,
(Action) BindedAction.Clone(),
Property.Copy());
return result;
}
public override object Clone()
{
return this.Copy();
}
#region COMPARERS (IDE GENERATED)
public int CompareTo(Trigger other)
{
var result = this.Property.Position - other.Property.Position;
return result == 0 ? 1 : result;
}
public int CompareTo(object obj)
{
if (ReferenceEquals(null, obj)) return 1;
if (ReferenceEquals(this, obj)) return 0;
if (!(obj is Trigger))
throw new ArgumentException($"Object must be of type {nameof(Trigger)}");
return CompareTo((Trigger) obj);
}
public static bool operator <(Trigger left, Trigger right)
{
return Comparer<Trigger>.Default.Compare(left, right) < 0;
}
public static bool operator >(Trigger left, Trigger right)
{
return Comparer<Trigger>.Default.Compare(left, right) > 0;
}
public static bool operator <=(Trigger left, Trigger right)
{
return Comparer<Trigger>.Default.Compare(left, right) <= 0;
}
public static bool operator >=(Trigger left, Trigger right)
{
return Comparer<Trigger>.Default.Compare(left, right) >= 0;
}
#endregion
}
public class TriggerProperty : DataKeeperPropertyElement
{
public BindType BindType { get; private set; }
public TriggerType TriggerType { get; private set; }
#region CONSTRUCTORS
public TriggerProperty(BindType bindType, TriggerType triggerType)
: base(ActivityStatus.Active)
{
this.BindType = bindType;
this.TriggerType = triggerType;
}
public TriggerProperty(
BindType bindType,
TriggerType triggerType,
ActivityStatus activityStatus,
int position
) : base(activityStatus, position)
{
this.BindType = bindType;
this.TriggerType = triggerType;
}
#endregion
public TriggerProperty Copy()
{
return new TriggerProperty(BindType, TriggerType, ActivityStatus, Position);
}
public override object Clone()
{
return this.Copy();
}
public static TriggerProperty DefaultAfterUpdate
{
get { return new TriggerProperty(BindType.OnUpdate, TriggerType.After); }
}
public static TriggerProperty DefaultBeforeRemove
{
get { return new TriggerProperty(BindType.OnRemove, TriggerType.Before); }
}
}
}