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;
6
8{
12 [ArchivingTime(nameof(ArchivingTime))]
13 public sealed class GenericObject : ICollection<KeyValuePair<string, object>>
14 {
15 private IEnumerable<KeyValuePair<string, object>> properties = new ChunkedList<KeyValuePair<string, object>>();
16 private Dictionary<string, object> propertiesByName = null;
17 private string collectionName = null;
18 private string typeName = null;
19 private int archivingTime = 0;
20 private bool propertiesUpdated = false;
21 private Guid objectId = Guid.Empty;
22
26 internal GenericObject()
27 {
28 }
29
37 public GenericObject(string CollectionName, string TypeName, Guid ObjectId, params KeyValuePair<string, object>[] Properties)
38 {
39 this.collectionName = CollectionName;
40 this.typeName = TypeName;
41 this.objectId = ObjectId;
42 this.properties = Properties;
43 }
44
52 public GenericObject(string CollectionName, string TypeName, Guid ObjectId, IEnumerable<KeyValuePair<string, object>> Properties)
53 {
54 this.collectionName = CollectionName;
55 this.typeName = TypeName;
56 this.objectId = ObjectId;
57 this.properties = Properties;
58 }
59
63 public string CollectionName
64 {
65 get => this.collectionName;
66 set => this.collectionName = value;
67 }
68
72 public string TypeName
73 {
74 get => this.typeName;
75 set => this.typeName = value;
76 }
77
81 public Guid ObjectId
82 {
83 get => this.objectId;
84 set => this.objectId = value;
85 }
86
90 [DefaultValue(0)]
91 public int ArchivingTime
92 {
93 get => this.archivingTime;
94 set => this.archivingTime = value;
95 }
96
100 public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
101 {
102 if (this.propertiesUpdated)
103 this.BuildEnumerable();
104
105 return this.properties.GetEnumerator();
106 }
107
111 IEnumerator IEnumerable.GetEnumerator()
112 {
113 if (this.propertiesUpdated)
114 this.BuildEnumerable();
115
116 return this.properties.GetEnumerator();
117 }
118
122 public Dictionary<string,object>.KeyCollection Keys
123 {
124 get
125 {
126 if (this.propertiesByName is null)
127 this.BuildDictionary();
128
129 return this.propertiesByName.Keys;
130 }
131 }
132
136 public Dictionary<string, object>.ValueCollection Values
137 {
138 get
139 {
140 if (this.propertiesByName is null)
141 this.BuildDictionary();
142
143 return this.propertiesByName.Values;
144 }
145 }
146
152 public object this[string PropertyName]
153 {
154 get
155 {
156 if (this.propertiesByName is null)
157 this.BuildDictionary();
158
159 if (this.propertiesByName.TryGetValue(PropertyName, out object Result))
160 return Result;
161 else
162 return null;
163 }
164
165 set
166 {
167 if (this.propertiesByName is null)
168 this.BuildDictionary();
169
170 this.propertiesByName[PropertyName] = value;
171 this.propertiesUpdated = true;
172 }
173 }
174
181 public bool TryGetFieldValue(string PropertyName, out object Value)
182 {
183 if (this.propertiesByName is null)
184 this.BuildDictionary();
185
186 return this.propertiesByName.TryGetValue(PropertyName, out Value);
187 }
188
194 public bool Remove(string PropertyName)
195 {
196 if (this.propertiesByName is null)
197 this.BuildDictionary();
198
199 if (!this.propertiesByName.Remove(PropertyName))
200 return false;
201
202 this.propertiesUpdated = true;
203
204 return true;
205 }
206
207 private void BuildDictionary()
208 {
209 this.propertiesByName = this.properties as Dictionary<string, object>;
210 if (this.propertiesByName is null)
211 {
212 this.propertiesByName = new Dictionary<string, object>();
213
214 foreach (KeyValuePair<string, object> P in this.properties)
215 this.propertiesByName[P.Key] = P.Value;
216 }
217 }
218
219 private void BuildEnumerable()
220 {
222 Dictionary<string, bool> Added = new Dictionary<string, bool>();
223
224 foreach (KeyValuePair<string, object> P in this.properties)
225 {
226 if (!this.propertiesByName.TryGetValue(P.Key, out object Value))
227 continue;
228
229 List.Add(new KeyValuePair<string, object>(P.Key, Value));
230 Added[P.Key] = true;
231 }
232
233 foreach (KeyValuePair<string, object> P in this.propertiesByName)
234 {
235 if (Added.ContainsKey(P.Key))
236 continue;
237
238 List.Add(P);
239 }
240
241 this.properties = List;
242 }
243
247 public IEnumerable<KeyValuePair<string, object>> Properties
248 {
249 get
250 {
251 if (this.propertiesUpdated)
252 this.BuildEnumerable();
253
254 return this.properties;
255 }
256 }
257
261 public int Count
262 {
263 get
264 {
265 if (this.propertiesByName is null)
266 this.BuildDictionary();
267
268 return this.propertiesByName.Count;
269 }
270 }
271
275 public bool IsReadOnly
276 {
277 get
278 {
279 return false;
280 }
281 }
282
286 public void Add(KeyValuePair<string, object> item)
287 {
288 this[item.Key] = item.Value;
289 }
290
294 public void Clear()
295 {
296 if (this.propertiesByName is null)
297 this.propertiesByName = new Dictionary<string, object>();
298 else
299 this.propertiesByName.Clear();
300
301 this.propertiesUpdated = true;
302 }
303
307 public bool Contains(KeyValuePair<string, object> item)
308 {
309 if (this.propertiesByName is null)
310 this.BuildDictionary();
311
312 if (!this.propertiesByName.TryGetValue(item.Key, out object Value))
313 return false;
314
315 if (Value is null)
316 return item.Value is null;
317
318 return Value.Equals(item.Value);
319 }
320
324 public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
325 {
326 foreach (KeyValuePair<string, object> P in this)
327 array[arrayIndex++] = P;
328 }
329
333 public bool Remove(KeyValuePair<string, object> item)
334 {
335 if (this.propertiesByName is null)
336 this.BuildDictionary();
337
338 if (!this.propertiesByName.TryGetValue(item.Key, out object Value))
339 return false;
340
341 if (Value is null)
342 {
343 if (!(item.Value is null))
344 return false;
345 }
346 else if (!Value.Equals(item.Value))
347 return false;
348
349 this.propertiesByName.Remove(item.Key);
350 this.propertiesUpdated = true;
351
352 return true;
353 }
354
356 public override bool Equals(object obj)
357 {
358 if (!(obj is GenericObject GenObj) ||
359 this.collectionName != GenObj.collectionName ||
360 this.typeName != GenObj.typeName ||
361 this.objectId != GenObj.objectId)
362 {
363 return false;
364 }
365
366 if (this.propertiesByName is null)
367 this.BuildDictionary();
368
369 if (GenObj.propertiesByName is null)
370 GenObj.BuildDictionary();
371
372 if (this.propertiesByName.Count != GenObj.propertiesByName.Count)
373 return false;
374
375 foreach (KeyValuePair<string, object> P in this.propertiesByName)
376 {
377 if (!GenObj.propertiesByName.TryGetValue(P.Key, out object Value))
378 return false;
379
380 if (!PropertyEquals(Value, P.Value))
381 return false;
382 }
383
384 return true;
385 }
386
387 private static bool PropertyEquals(object Value1, object Value2)
388 {
389 if (Value1 is null ^ Value2 is null)
390 return false;
391
392 if (Value1 is null || Value1.Equals(Value2))
393 return true;
394
395 if (Value1 is Array A && Value2 is Array B)
396 {
397 int i, c = A.Length;
398
399 if (c != B.Length)
400 return false;
401
402 for (i = 0; i < c; i++)
403 {
404 if (!PropertyEquals(A.GetValue(i), B.GetValue(i)))
405 return false;
406 }
407
408 return true;
409 }
410 else
411 return false;
412 }
413
415 public override int GetHashCode()
416 {
417 if (this.propertiesByName is null)
418 this.BuildDictionary();
419
420 int Result = this.objectId.GetHashCode();
421 Result ^= Result << 5 ^ this.typeName.GetHashCode();
422 Result ^= Result << 5 ^ this.collectionName.GetHashCode();
423
424 foreach (KeyValuePair<string, object> P in this.propertiesByName)
425 {
426 Result ^= Result << 5 ^ P.Key.GetHashCode();
427 Result ^= Result << 5 ^ P.Value.GetHashCode();
428 }
429
430 return Result;
431 }
432
438 public bool HasProperty(string PropertyName)
439 {
440 if (this.propertiesByName is null)
441 this.BuildDictionary();
442
443 return this.propertiesByName.ContainsKey(PropertyName);
444 }
445
451 public object GetProperty(string PropertyName)
452 {
453 return this[PropertyName];
454 }
455
456 }
457}
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.
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
bool Remove(T Item)
Removes an element from the collection.
Definition: ChunkedList.cs:357
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272