PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/Atlassian.Jira/PagedQueryResult.cs

https://bitbucket.org/farmas/atlassian.net-sdk
C# | 97 lines | 60 code | 10 blank | 27 comment | 4 complexity | 38453afd75018cf6d94eae6577678d28 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Newtonsoft.Json.Linq;
  4. namespace Atlassian.Jira
  5. {
  6. /// <summary>
  7. /// PagedQueryResult that can be deserialized from default JIRA paging response.
  8. /// </summary>
  9. internal class PagedQueryResult<T> : IPagedQueryResult<T>
  10. {
  11. private readonly IEnumerable<T> _enumerable;
  12. private readonly int _startAt;
  13. private readonly int _itemsPerPage;
  14. private readonly int _totalItems;
  15. /// <summary>
  16. /// Create a new instance of PagedQueryResult with all metadata provided.
  17. /// </summary>
  18. /// <param name="enumerable">Enumerable to wrap.</param>
  19. /// <param name="startAt">Index within the total items where this page's paged result starts.</param>
  20. /// <param name="itemsPerPage">Number of items returned per page.</param>
  21. /// <param name="totalItems">Number of total items available on the server.</param>
  22. public PagedQueryResult(IEnumerable<T> enumerable, int startAt, int itemsPerPage, int totalItems)
  23. {
  24. _enumerable = enumerable;
  25. _startAt = startAt;
  26. _itemsPerPage = itemsPerPage;
  27. _totalItems = totalItems;
  28. }
  29. /// <summary>
  30. /// Create an instance of PagedQueryResult taking metadata from a JSON object.
  31. /// </summary>
  32. /// <param name="pagedJson">JSON object with JIRA paged metadata.</param>
  33. /// <param name="items">Enumerable to wrap.</param>
  34. public static PagedQueryResult<T> FromJson(JObject pagedJson, IEnumerable<T> items)
  35. {
  36. return new PagedQueryResult<T>(
  37. items,
  38. GetPropertyOrDefault<int>(pagedJson, "startAt"),
  39. GetPropertyOrDefault<int>(pagedJson, "maxResults"),
  40. GetPropertyOrDefault<int>(pagedJson, "total"));
  41. }
  42. /// <summary>
  43. /// Index within the total items where this page's paged result starts.
  44. /// </summary>
  45. public int StartAt
  46. {
  47. get { return _startAt; }
  48. }
  49. /// <summary>
  50. /// Number of items returned per page.
  51. /// </summary>
  52. public int ItemsPerPage
  53. {
  54. get { return _itemsPerPage; }
  55. }
  56. /// <summary>
  57. /// Number of total items available on the server.
  58. /// </summary>
  59. public int TotalItems
  60. {
  61. get { return _totalItems; }
  62. }
  63. /// <summary>
  64. /// Returns an enumerator that iterates through the collection.
  65. /// </summary>
  66. public IEnumerator<T> GetEnumerator()
  67. {
  68. return _enumerable.GetEnumerator();
  69. }
  70. IEnumerator IEnumerable.GetEnumerator()
  71. {
  72. return _enumerable.GetEnumerator();
  73. }
  74. private static TValue GetPropertyOrDefault<TValue>(JObject json, string property)
  75. {
  76. var val = json[property];
  77. if (val == null || val.Type == JTokenType.Null)
  78. {
  79. return default(TValue);
  80. }
  81. else
  82. {
  83. return val.Value<TValue>();
  84. }
  85. }
  86. }
  87. }