Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SingletonCursor.cs
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Threading.Tasks;
6
8{
13 internal class SingletonCursor<T> : ICursor<T>
14 {
15 private readonly T value;
16 private readonly IObjectSerializer serializer;
17 private readonly ObjectSerializer objectSerializer;
18 private readonly Guid objectId;
19 private bool isCurrent = false;
20
27 internal SingletonCursor(T Value, IObjectSerializer Serializer, Guid ObjectId)
28 {
29 this.value = Value;
30 this.serializer = Serializer;
31 this.objectSerializer = Serializer as ObjectSerializer;
32 this.objectId = ObjectId;
33 }
34
40 public T Current
41 {
42 get
43 {
44 if (this.isCurrent)
45 return this.value;
46 else
47 throw new InvalidOperationException("Enumeration not started. Call MoveNext() first.");
48 }
49 }
50
54 public IObjectSerializer CurrentSerializer
55 {
56 get
57 {
58 if (this.isCurrent)
59 return this.serializer;
60 else
61 throw new InvalidOperationException("Enumeration not started. Call MoveNext() first.");
62 }
63 }
64
69 public bool CurrentTypeCompatible => true;
70
76 public Guid CurrentObjectId
77 {
78 get
79 {
80 if (this.isCurrent)
81 return this.objectId;
82 else
83 throw new InvalidOperationException("Enumeration not started. Call MoveNext() first.");
84 }
85 }
86
90 public void Dispose()
91 {
92 }
93
101 Task<bool> IAsyncEnumerator.MoveNextAsync() => this.MoveNextAsyncLocked();
102
108 object IEnumerator.Current => this.Current;
109
117 public bool MoveNext() => this.MoveNextAsyncLocked().Result;
118
122 public void Reset() => this.isCurrent = false;
123
130 public Task<bool> MoveNextAsyncLocked()
131 {
132 this.isCurrent = !this.isCurrent;
133 return Task.FromResult(this.isCurrent);
134 }
135
142 public Task<bool> MovePreviousAsyncLocked()
143 {
144 return this.MoveNextAsyncLocked(); // Ordering only in one direction.
145 }
146
147 public IEnumerator<T> GetEnumerator()
148 {
149 T[] A = new T[] { this.value };
150 return (IEnumerator<T>)A.GetEnumerator();
151 }
152
160 public bool SameSortOrder(string[] ConstantFields, string[] SortOrder)
161 {
162 if (SortOrder is null || SortOrder.Length != 1 || this.objectSerializer is null)
163 return false;
164
165 string s = this.objectSerializer.ObjectIdMemberName;
166 return (SortOrder[0] == s || SortOrder[0] == "+" + s);
167 }
168
176 public bool ReverseSortOrder(string[] ConstantFields, string[] SortOrder)
177 {
178 if (SortOrder is null || SortOrder.Length != 1 || this.objectSerializer is null)
179 return false;
180
181 string s = this.objectSerializer.ObjectIdMemberName;
182 return (SortOrder[0] == "-" + s);
183 }
184
189 public Task ContinueAfterLocked(T LastItem)
190 {
191 this.isCurrent = true;
192 return Task.CompletedTask;
193 }
194
199 public Task ContinueBeforeLocked(T LastItem)
200 {
201 this.isCurrent = true;
202 return Task.CompletedTask;
203 }
204 }
205}
Serializes a class, taking into account attributes defined in Attributes.
Interface for asynchronous enumerators.
Task< bool > MoveNextAsync()
Advances the enumerator to the next element of the collection.