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;
2using System.Collections;
3using System.Collections.Generic;
4using System.Threading.Tasks;
5
6namespace Waher.Persistence
7{
13 where T : class
14 {
15 private readonly IPage<T> firstPage;
16 private IPage<T> currentPage;
17 private IEnumerator<T> currentPageEnumerator;
18 private IAsyncEnumerator currentPageEnumeratorAsync;
19
24 public PaginatedEnumerator(IPage<T> FirstPage)
25 {
26 this.currentPage = this.firstPage = FirstPage;
27 this.currentPageEnumerator = this.currentPage.Items.GetEnumerator();
28 this.currentPageEnumeratorAsync = this.currentPageEnumerator as IAsyncEnumerator;
29 }
30
35 public PaginatedEnumerator(IEnumerable<T> FixedSetOfItems)
36 {
37 this.firstPage = null;
38 this.currentPage = null;
39 this.currentPageEnumerator = FixedSetOfItems.GetEnumerator();
40 this.currentPageEnumeratorAsync = this.currentPageEnumerator as IAsyncEnumerator;
41 }
42
46 public T Current => this.currentPageEnumerator.Current;
47
51 object IEnumerator.Current => this.currentPageEnumerator.Current;
52
56 public void Dispose()
57 {
58 if (this.currentPage is IDisposable Disposable)
59 Disposable.Dispose();
60
61 this.currentPageEnumerator.Dispose();
62 }
63
68 public bool MoveNext()
69 {
70 while (true)
71 {
72 if (this.currentPageEnumerator.MoveNext())
73 return true;
74
75 if (this.currentPage is null)
76 return false;
77
78 if (!this.currentPage.More)
79 return false;
80
81 this.currentPage = Database.FindNext(this.currentPage).Result;
82 this.currentPageEnumerator = this.currentPage.Items.GetEnumerator();
83 this.currentPageEnumeratorAsync = this.currentPageEnumerator as IAsyncEnumerator;
84 }
85 }
86
91 public async Task<bool> MoveNextAsync()
92 {
93 while (true)
94 {
95 if (await this.MoveNextOnCurrentPage())
96 return true;
97
98 if (this.currentPage is null)
99 return false;
100
101 if (!this.currentPage.More)
102 return false;
103
104 this.currentPage = await Database.FindNext(this.currentPage);
105 this.currentPageEnumerator = this.currentPage.Items.GetEnumerator();
106 this.currentPageEnumeratorAsync = this.currentPageEnumerator as IAsyncEnumerator;
107 }
108 }
109
110 private Task<bool> MoveNextOnCurrentPage()
111 {
112 if (this.currentPageEnumeratorAsync is null)
113 return Task.FromResult(this.currentPageEnumerator.MoveNext());
114 else
115 return this.currentPageEnumeratorAsync.MoveNextAsync();
116 }
117
121 public void Reset()
122 {
123 if (this.firstPage is null)
124 this.currentPageEnumerator.Reset();
125 else
126 {
127 this.currentPage = this.firstPage;
128 this.currentPageEnumerator = this.currentPage.Items.GetEnumerator();
129 this.currentPageEnumeratorAsync = this.currentPageEnumerator as IAsyncEnumerator;
130 }
131 }
132 }
133}
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static Task< IPage< object > > FindNext(IPage< object > Page)
Finds the next page of objects in a given collection.
Definition: Database.cs:446
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 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