Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PaginatedEnumerator.cs
1using System;
4using System.Threading.Tasks;
5using Waher.Events;
6
7namespace Waher.Persistence
8{
14 where T : class
15 {
16 private readonly IPage<T> firstPage;
17 private IPage<T> currentPage;
18 private IEnumerator<T> currentPageEnumerator;
19 private IAsyncEnumerator currentPageEnumeratorAsync;
20
25 public PaginatedEnumerator(IPage<T> FirstPage)
26 {
27 this.currentPage = this.firstPage = FirstPage;
28 this.currentPageEnumerator = this.currentPage.Items.GetEnumerator();
29 this.currentPageEnumeratorAsync = this.currentPageEnumerator as IAsyncEnumerator;
30 }
31
36 public PaginatedEnumerator(IEnumerable<T> FixedSetOfItems)
37 {
38 this.firstPage = null;
39 this.currentPage = null;
40 this.currentPageEnumerator = FixedSetOfItems.GetEnumerator();
41 this.currentPageEnumeratorAsync = this.currentPageEnumerator as IAsyncEnumerator;
42 }
43
47 public T Current => this.currentPageEnumerator.Current;
48
52 object IEnumerator.Current => this.currentPageEnumerator.Current;
53
57 [Obsolete("Use DisposeAsync() instead.")]
58 public void Dispose()
59 {
60 this.DisposeAsync().Wait();
61 }
62
66 public async Task DisposeAsync()
67 {
68 if (this.currentPage is IDisposableAsync DisposableAsync)
69 await DisposableAsync.DisposeAsync();
70 else if (this.currentPage is IDisposable Disposable)
71 Disposable.Dispose();
72
73 if (this.currentPageEnumerator is IDisposableAsync DisposableAsync2)
74 await DisposableAsync2.DisposeAsync();
75 else
76 this.currentPageEnumerator.Dispose();
77 }
78
83 public bool MoveNext()
84 {
85 while (true)
86 {
87 if (this.currentPageEnumerator.MoveNext())
88 return true;
89
90 if (this.currentPage is null)
91 return false;
92
93 if (!this.currentPage.More)
94 return false;
95
96 this.currentPage = Database.FindNext(this.currentPage).Result;
97 this.currentPageEnumerator = this.currentPage.Items.GetEnumerator();
98 this.currentPageEnumeratorAsync = this.currentPageEnumerator as IAsyncEnumerator;
99 }
100 }
101
106 public async Task<bool> MoveNextAsync()
107 {
108 while (true)
109 {
110 if (await this.MoveNextOnCurrentPage())
111 return true;
112
113 if (this.currentPage is null)
114 return false;
115
116 if (!this.currentPage.More)
117 return false;
118
119 this.currentPage = await Database.FindNext(this.currentPage);
120 this.currentPageEnumerator = this.currentPage.Items.GetEnumerator();
121 this.currentPageEnumeratorAsync = this.currentPageEnumerator as IAsyncEnumerator;
122 }
123 }
124
125 private Task<bool> MoveNextOnCurrentPage()
126 {
127 if (this.currentPageEnumeratorAsync is null)
128 return Task.FromResult(this.currentPageEnumerator.MoveNext());
129 else
130 return this.currentPageEnumeratorAsync.MoveNextAsync();
131 }
132
136 public void Reset()
137 {
138 if (this.firstPage is null)
139 this.currentPageEnumerator.Reset();
140 else
141 {
142 this.currentPage = this.firstPage;
143 this.currentPageEnumerator = this.currentPage.Items.GetEnumerator();
144 this.currentPageEnumeratorAsync = this.currentPageEnumerator as IAsyncEnumerator;
145 }
146 }
147 }
148}
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static Task< IPage< object > > FindNext(IPage< object > Page)
Finds the next page of objects in a given collection.
Definition: Database.cs:437
async Task DisposeAsync()
IDisposableAsync.DisposeAsync
void Reset()
Resets the enumerator.
async Task< bool > MoveNextAsync()
Moves to next item.
PaginatedEnumerator(IEnumerable< T > FixedSetOfItems)
Paginated object enumerator, enumerating a single page of enumerable items.
PaginatedEnumerator(IPage< T > FirstPage)
Paginated object enumerator.
Interface for asynchronously disposable objects.
Interface for asynchronous enumerators.
Task< bool > MoveNextAsync()
Advances the enumerator to the next element of the collection.
Interface for paginated results.
Definition: IPage.cs:11
IEnumerable< T > Items
Items available in the page. The enumeration may be empty.
Definition: IPage.cs:15
bool More
If there may be more pages following this page.
Definition: IPage.cs:20