| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.ComponentModel; |
| | | 7 | | using System.Threading; |
| | | 8 | | |
| | | 9 | | namespace Azure |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// A collection of values that may take multiple service requests to |
| | | 13 | | /// iterate over. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <typeparam name="T">The type of the values.</typeparam> |
| | | 16 | | public abstract class Pageable<T> : IEnumerable<T> where T : notnull |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Gets a <see cref="CancellationToken"/> used for requests made while |
| | | 20 | | /// enumerating asynchronously. |
| | | 21 | | /// </summary> |
| | 0 | 22 | | protected virtual CancellationToken CancellationToken { get; } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="Pageable{T}"/> |
| | | 26 | | /// class for mocking. |
| | | 27 | | /// </summary> |
| | 0 | 28 | | protected Pageable() => |
| | 0 | 29 | | CancellationToken = CancellationToken.None; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Initializes a new instance of the <see cref="Pageable{T}"/> |
| | | 33 | | /// class. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="cancellationToken"> |
| | | 36 | | /// The <see cref="CancellationToken"/> used for requests made while |
| | | 37 | | /// enumerating asynchronously. |
| | | 38 | | /// </param> |
| | 0 | 39 | | protected Pageable(CancellationToken cancellationToken) => |
| | 0 | 40 | | CancellationToken = cancellationToken; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Enumerate the values a <see cref="Page{T}"/> at a time. This may |
| | | 44 | | /// make multiple service requests. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="continuationToken"> |
| | | 47 | | /// A continuation token indicating where to resume paging or null to |
| | | 48 | | /// begin paging from the beginning. |
| | | 49 | | /// </param> |
| | | 50 | | /// <param name="pageSizeHint"> |
| | | 51 | | /// The size of <see cref="Page{T}"/>s that should be requested (from |
| | | 52 | | /// service operations that support it). |
| | | 53 | | /// </param> |
| | | 54 | | /// <returns> |
| | | 55 | | /// An async sequence of <see cref="Page{T}"/>s. |
| | | 56 | | /// </returns> |
| | | 57 | | public abstract IEnumerable<Page<T>> AsPages( |
| | | 58 | | string? continuationToken = default, |
| | | 59 | | int? pageSizeHint = default); |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Creates a string representation of an <see cref="Pageable{T}"/>. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <returns> |
| | | 65 | | /// A string representation of an <see cref="Pageable{T}"/>. |
| | | 66 | | /// </returns> |
| | | 67 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | 0 | 68 | | public override string ToString() => base.ToString(); |
| | | 69 | | |
| | | 70 | | IEnumerator IEnumerable.GetEnumerator() |
| | | 71 | | { |
| | 0 | 72 | | return GetEnumerator(); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Enumerate the values in the collection. This may make multiple service requests. |
| | | 77 | | /// </summary> |
| | | 78 | | public virtual IEnumerator<T> GetEnumerator() |
| | | 79 | | { |
| | 0 | 80 | | foreach (Page<T> page in AsPages()) |
| | | 81 | | { |
| | 0 | 82 | | foreach (T value in page.Values) |
| | | 83 | | { |
| | 0 | 84 | | yield return value; |
| | | 85 | | } |
| | | 86 | | } |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Check if two <see cref="Pageable{T}"/> instances are equal. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="obj">The instance to compare to.</param> |
| | | 93 | | /// <returns>True if they're equal, false otherwise.</returns> |
| | | 94 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | 0 | 95 | | public override bool Equals(object? obj) => base.Equals(obj); |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Get a hash code for the <see cref="Pageable{T}"/>. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <returns>Hash code for the <see cref="Pageable{T}"/>.</returns> |
| | | 101 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | 0 | 102 | | public override int GetHashCode() => base.GetHashCode(); |
| | | 103 | | } |
| | | 104 | | } |