-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55eeeae
commit 1ba133c
Showing
2 changed files
with
126 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
namespace Skyline.DataMiner.Utils.DOM.UnitTesting | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using Skyline.DataMiner.Net.Messages.SLDataGateway; | ||
|
||
internal class DomPagingHandler<T> : IDisposable | ||
{ | ||
private readonly IEnumerator<T> _enumerator; | ||
|
||
private bool _hasNext; | ||
private T _nextRow; | ||
|
||
private bool _isDisposed; | ||
|
||
public DomPagingHandler(IEnumerable<T> items) | ||
{ | ||
if (items == null) | ||
{ | ||
throw new ArgumentNullException(nameof(items)); | ||
} | ||
|
||
_enumerator = items.GetEnumerator(); | ||
TryMoveNext(); | ||
} | ||
|
||
public PagingCookie Cookie { get; } = PagingCookie.CreateNew(); | ||
|
||
public List<T> GetNextPage(long pageSize, out bool isLast) | ||
{ | ||
var page = new List<T>(); | ||
|
||
for (int i = 0; i < pageSize && _hasNext; i++) | ||
{ | ||
page.Add(_nextRow); | ||
TryMoveNext(); | ||
} | ||
|
||
if (!_hasNext) | ||
{ | ||
Dispose(); | ||
} | ||
|
||
isLast = !_hasNext; | ||
return page; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_isDisposed) | ||
{ | ||
return; | ||
} | ||
|
||
_enumerator?.Dispose(); | ||
_isDisposed = true; | ||
} | ||
|
||
private void TryMoveNext() | ||
{ | ||
_hasNext = _enumerator.MoveNext(); | ||
_nextRow = _hasNext ? _enumerator.Current : default; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters