Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GenericObject.cs
1using System;
2using System.Collections;
3using System.Collections.Generic;
5
7{
11 [ArchivingTime(nameof(ArchivingTime))]
12 public sealed class GenericObject : ICollection<KeyValuePair<string, object>>
13 {
14 private IEnumerable<KeyValuePair<string, object>> properties = new LinkedList<KeyValuePair<string, object>>();
15 private Dictionary<string, object> propertiesByName = null;
16 private string collectionName = null;
17 private string typeName = null;
18 private int archivingTime = 0;
19 private bool propertiesUpdated = false;
20 private Guid objectId = Guid.Empty;
21
25 internal GenericObject()
26 {
27 }
28
36 public GenericObject(string CollectionName, string TypeName, Guid ObjectId, params KeyValuePair<string, object>[] Properties)
37 {
38 this.collectionName = CollectionName;
39 this.typeName = TypeName;
40 this.objectId = ObjectId;
41 this.properties = Properties;
42 }
43
51 public GenericObject(string CollectionName, string TypeName, Guid ObjectId, IEnumerable<KeyValuePair<string, object>> Properties)
52 {
53 this.collectionName = CollectionName;
54 this.typeName = TypeName;
55 this.objectId = ObjectId;
56 this.properties = Properties;
57 }
58
62 public string CollectionName
63 {
64 get => this.collectionName;
65 set => this.collectionName = value;
66 }
67
71 public string TypeName
72 {
73 get => this.typeName;
74 set => this.typeName = value;
75 }
76
80 public Guid ObjectId
81 {
82 get => this.objectId;
83 set => this.objectId = value;
84 }
85
89 [DefaultValue(0)]
90 public int ArchivingTime
91 {
92 get => this.archivingTime;
93 set => this.archivingTime = value;
94 }
95
99 public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
100 {
101 if (this.propertiesUpdated)
102 this.BuildEnumerable();
103
104 return this.properties.GetEnumerator();
105 }
106
110 IEnumerator IEnumerable.GetEnumerator()
111 {
112 if (this.propertiesUpdated)
113 this.BuildEnumerable();
114
115 return this.properties.GetEnumerator();
116 }
117
121 public Dictionary<string,object>.KeyCollection Keys
122 {
123 get
124 {
125 if (this.propertiesByName is null)
126 this.BuildDictionary();
127
128 return this.propertiesByName.Keys;
129 }
130 }
131
135 public Dictionary<string, object>.ValueCollection Values
136 {
137 get
138 {
139 if (this.propertiesByName is null)
140 this.BuildDictionary();
141
142 return this.propertiesByName.Values;
143 }
144 }
145
151 public object this[string PropertyName]
152 {
153 get
154 {
155 if (this.propertiesByName is null)
156 this.BuildDictionary();
157
158 if (this.propertiesByName.TryGetValue(PropertyName, out object Result))
159 return Result;
160 else
161 return null;
162 }
163
164 set
165 {
166 if (this.propertiesByName is null)
167 this.BuildDictionary();
168
169 this.propertiesByName[PropertyName] = value;
170 this.propertiesUpdated = true;
171 }
172 }
173
180 public bool TryGetFieldValue(string PropertyName, out object Value)
181 {
182 if (this.propertiesByName is null)
183 this.BuildDictionary();
184
185 return this.propertiesByName.TryGetValue(PropertyName, out Value);
186 }
187
193 public bool Remove(string PropertyName)
194 {
195 if (this.propertiesByName is null)
196 this.BuildDictionary();
197
198 if (!this.propertiesByName.Remove(PropertyName))
199 return false;
200
201 this.propertiesUpdated = true;
202
203 return true;
204 }
205
206 private void BuildDictionary()
207 {
208 this.propertiesByName = this.properties as Dictionary<string, object>;
209 if (this.propertiesByName is null)
210 {
211 this.propertiesByName = new Dictionary<string, object>();
212
213 foreach (KeyValuePair<string, object> P in this.properties)
214 this.propertiesByName[P.Key] = P.Value;
215 }
216 }
217
218 private void BuildEnumerable()
219 {
220 LinkedList<KeyValuePair<string, object>> List = new LinkedList<KeyValuePair<string, object>>();
221 Dictionary<string, bool> Added = new Dictionary<string, bool>();
222
223 foreach (KeyValuePair<string, object> P in this.properties)
224 {
225 if (!this.propertiesByName.TryGetValue(P.Key, out object Value))
226 continue;
227
228 List.AddLast(new KeyValuePair<string, object>(P.Key, Value));
229 Added[P.Key] = true;
230 }
231
232 foreach (KeyValuePair<string, object> P in this.propertiesByName)
233 {
234 if (Added.ContainsKey(P.Key))
235 continue;
236
237 List.AddLast(P);
238 }
239
240 this.properties = List;
241 }
242
246 public IEnumerable<KeyValuePair<string, object>> Properties
247 {
248 get
249 {
250 if (this.propertiesUpdated)
251 this.BuildEnumerable();
252
253 return this.properties;
254 }
255 }
256
260 public int Count
261 {
262 get
263 {
264 if (this.propertiesByName is null)
265 this.BuildDictionary();
266
267 return this.propertiesByName.Count;
268 }
269 }
270
274 public bool IsReadOnly
275 {
276 get
277 {
278 return false;
279 }
280 }
281
285 public void Add(KeyValuePair<string, object> item)
286 {
287 this[item.Key] = item.Value;
288 }
289
293 public void Clear()
294 {
295 if (this.propertiesByName is null)
296 this.propertiesByName = new Dictionary<string, object>();
297 else
298 this.propertiesByName.Clear();
299
300 this.propertiesUpdated = true;
301 }
302
306 public bool Contains(KeyValuePair<string, object> item)
307 {
308 if (this.propertiesByName is null)
309 this.BuildDictionary();
310
311 if (!this.propertiesByName.TryGetValue(item.Key, out object Value))
312 return false;
313
314 if (Value is null)
315 return item.Value is null;
316
317 return Value.Equals(item.Value);
318 }
319
323 public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
324 {
325 foreach (KeyValuePair<string, object> P in this)
326 array[arrayIndex++] = P;
327 }
328
332 public bool Remove(KeyValuePair<string, object> item)
333 {
334 if (this.propertiesByName is null)
335 this.BuildDictionary();
336
337 if (!this.propertiesByName.TryGetValue(item.Key, out object Value))
338 return false;
339
340 if (Value is null)
341 {
342 if (!(item.Value is null))
343 return false;
344 }
345 else if (!Value.Equals(item.Value))
346 return false;
347
348 this.propertiesByName.Remove(item.Key);
349 this.propertiesUpdated = true;
350
351 return true;
352 }
353
355 public override bool Equals(object obj)
356 {
357 if (!(obj is GenericObject GenObj) ||
358 this.collectionName != GenObj.collectionName ||
359 this.typeName != GenObj.typeName ||
360 this.objectId != GenObj.objectId)
361 {
362 return false;
363 }
364
365 if (this.propertiesByName is null)
366 this.BuildDictionary();
367
368 if (GenObj.propertiesByName is null)
369 GenObj.BuildDictionary();
370
371 if (this.propertiesByName.Count != GenObj.propertiesByName.Count)
372 return false;
373
374 foreach (KeyValuePair<string, object> P in this.propertiesByName)
375 {
376 if (!GenObj.propertiesByName.TryGetValue(P.Key, out object Value))
377 return false;
378
379 if (!PropertyEquals(Value, P.Value))
380 return false;
381 }
382
383 return true;
384 }
385
386 private static bool PropertyEquals(object Value1, object Value2)
387 {
388 if (Value1 is null ^ Value2 is null)
389 return false;
390
391 if (Value1 is null || Value1.Equals(Value2))
392 return true;
393
394 if (Value1 is Array A && Value2 is Array B)
395 {
396 int i, c = A.Length;
397
398 if (c != B.Length)
399 return false;
400
401 for (i = 0; i < c; i++)
402 {
403 if (!PropertyEquals(A.GetValue(i), B.GetValue(i)))
404 return false;
405 }
406
407 return true;
408 }
409 else
410 return false;
411 }
412
414 public override int GetHashCode()
415 {
416 if (this.propertiesByName is null)
417 this.BuildDictionary();
418
419 int Result = this.objectId.GetHashCode();
420 Result ^= Result << 5 ^ this.typeName.GetHashCode();
421 Result ^= Result << 5 ^ this.collectionName.GetHashCode();
422
423 foreach (KeyValuePair<string, object> P in this.propertiesByName)
424 {
425 Result ^= Result << 5 ^ P.Key.GetHashCode();
426 Result ^= Result << 5 ^ P.Value.GetHashCode();
427 }
428
429 return Result;
430 }
431
437 public bool HasProperty(string PropertyName)
438 {
439 if (this.propertiesByName is null)
440 this.BuildDictionary();
441
442 return this.propertiesByName.ContainsKey(PropertyName);
443 }
444
450 public object GetProperty(string PropertyName)
451 {
452 return this[PropertyName];
453 }
454
455 }
456}
Generic object. Contains a sequence of properties.
IEnumerable< KeyValuePair< string, object > > Properties
Current set of properties.
bool HasProperty(string PropertyName)
If the object has a property with a given name.
bool Remove(string PropertyName)
Removes a named property.
Dictionary< string, object >.KeyCollection Keys
Gets an enumeration of available key names.
bool TryGetFieldValue(string PropertyName, out object Value)
Gets the value of a field or property of the object, given its name.
void Add(KeyValuePair< string, object > item)
ICollection<T>.Add
int ArchivingTime
Archiving time, in days. 0=No archiving. int.MaxValue=No time limit.
bool IsReadOnly
ICollection<T>.IsReadOnly
bool Contains(KeyValuePair< string, object > item)
ICollection<T>.Contains
bool Remove(KeyValuePair< string, object > item)
ICollection<T>.Remove
IEnumerator< KeyValuePair< string, object > > GetEnumerator()
IEnumerable<T>.GetEnumerator()
Dictionary< string, object >.ValueCollection Values
Gets an enumeration of available values.
object GetProperty(string PropertyName)
Gets the value of a given property.
void CopyTo(KeyValuePair< string, object >[] array, int arrayIndex)
ICollection<T>.Contains
GenericObject(string CollectionName, string TypeName, Guid ObjectId, params KeyValuePair< string, object >[] Properties)
Generic object. Contains a sequence of properties.
GenericObject(string CollectionName, string TypeName, Guid ObjectId, IEnumerable< KeyValuePair< string, object > > Properties)
Generic object. Contains a sequence of properties.