-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.Subclasses.cs
More file actions
167 lines (133 loc) · 4.75 KB
/
Data.Subclasses.cs
File metadata and controls
167 lines (133 loc) · 4.75 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.Linq;
namespace DataKeeper
{
public sealed partial class Data
{
private readonly Dictionary<string, DataInfo> data;
private readonly ISet<string> removedKeys;
private static volatile Data instance;
private static object syncRoot = new object();
private Data()
{
data = new Dictionary<string, DataInfo>();
removedKeys = new SortedSet<string>();
}
private bool IsKeyRemoved(string key) => removedKeys.Contains(key);
private class DataInfo
{
private string Key { get; }
public dynamic Value { get; set; }
public SortedSet<Trigger> BindedTriggers { get; }
public SortedSet<Constraint> BindedConstraints { get; }
public DataInfo(string key)
{
Key = key;
BindedConstraints = new SortedSet<Constraint>();
BindedTriggers = new SortedSet<Trigger>();
}
public void AddConstraint(Constraint constraint)
{
if(BindedConstraints.FirstOrDefault(c => c.Id == constraint.Id) != null)
throw new SameIdExistsException(constraint.Id,
$"Constraint with the same id \"{constraint.Id}\" already exists constraints by \"{Key}\" key!");
BindedConstraints.Add(constraint);
}
public void AddTrigger(Trigger trigger)
{
if(BindedTriggers.FirstOrDefault(t => t.Id == trigger.Id) != null)
throw new SameIdExistsException(trigger.Id,
$"Trigger with the same id \"{trigger.Id}\" already exists in triggers by \"{Key}\" key!");
BindedTriggers.Add(trigger);
}
public bool HasAnyTrigger()
{
return BindedTriggers.Count > 0;
}
}
private Constraint FindConstarintById(string constraintId)
{
foreach (var info in data.Values)
{
foreach (var constraint in info.BindedConstraints)
{
if (constraint.Id == constraintId)
return constraint;
}
}
return null;
}
private Trigger FindTriggerById(string triggerId)
{
foreach (var info in data.Values)
{
foreach (var trigger in info.BindedTriggers)
{
if (trigger.Id == triggerId)
return trigger;
}
}
return null;
}
public DataKeeperElement FindDataKeeperElementById(string id)
{
return (DataKeeperElement) FindTriggerById(id) ?? FindConstarintById(id);
}
}
public abstract class DataKeeperElement : ICloneable
{
public string Id { get; }
public abstract object Clone();
protected string idPrefix { get; }
public DataKeeperElement(string id)
{
if (id == null)
throw new ArgumentNullException(nameof(id), $"Parameter \"{nameof(id)}\" can't be null!");
this.idPrefix = id;
Id = Ids.Container.ReturnId(id);
}
}
public abstract class DataKeeperPropertyElement : ICloneable
{
public ActivityStatus ActivityStatus { get; private set; }
private int _position = DefaultPosition;
public int Position
{
get { return _position; }
private set { _position = NormilizePosition(value); }
}
#region CONSTRUCTORS
public DataKeeperPropertyElement(ActivityStatus activityStatus)
{
ActivityStatus = activityStatus;
Position = DefaultPosition;
}
public DataKeeperPropertyElement(ActivityStatus activityStatus, int position)
{
ActivityStatus = activityStatus;
Position = position;
}
#endregion
public abstract object Clone();
private static int NormilizePosition(int value)
{
if (value < LowestPosition)
return LowestPosition;
if (value > LargestPosition)
return LargestPosition;
return value;
}
private const int LowestPosition = 0;
private const int LargestPosition = 20;
private const int DefaultPosition = 10;
public void SetPosition(int newPosition)
{
Position = newPosition;
}
public void SetActivityStatus(ActivityStatus newStatus)
{
ActivityStatus = newStatus;
}
}
}