You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.8 KiB

  1. #region License
  2. // Copyright (c) 2007 James Newton-King
  3. //
  4. // Permission is hereby granted, free of charge, to any person
  5. // obtaining a copy of this software and associated documentation
  6. // files (the "Software"), to deal in the Software without
  7. // restriction, including without limitation the rights to use,
  8. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following
  11. // conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. // OTHER DEALINGS IN THE SOFTWARE.
  24. #endregion
  25. using System;
  26. using System.Collections.Generic;
  27. #if NET20
  28. using Newtonsoft.Json.Utilities.LinqBridge;
  29. #else
  30. using System.Linq;
  31. #endif
  32. using Newtonsoft.Json.Utilities;
  33. using System.Collections;
  34. namespace Newtonsoft.Json.Linq
  35. {
  36. /// <summary>
  37. /// Represents a collection of <see cref="JToken"/> objects.
  38. /// </summary>
  39. /// <typeparam name="T">The type of token.</typeparam>
  40. internal readonly struct JEnumerable<T> : IJEnumerable<T>, IEquatable<JEnumerable<T>> where T : JToken
  41. {
  42. /// <summary>
  43. /// An empty collection of <see cref="JToken"/> objects.
  44. /// </summary>
  45. public static readonly JEnumerable<T> Empty = new JEnumerable<T>(Enumerable.Empty<T>());
  46. private readonly IEnumerable<T> _enumerable;
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="JEnumerable{T}"/> struct.
  49. /// </summary>
  50. /// <param name="enumerable">The enumerable.</param>
  51. public JEnumerable(IEnumerable<T> enumerable)
  52. {
  53. ValidationUtils.ArgumentNotNull(enumerable, nameof(enumerable));
  54. _enumerable = enumerable;
  55. }
  56. /// <summary>
  57. /// Returns an enumerator that can be used to iterate through the collection.
  58. /// </summary>
  59. /// <returns>
  60. /// A <see cref="IEnumerator{T}"/> that can be used to iterate through the collection.
  61. /// </returns>
  62. public IEnumerator<T> GetEnumerator()
  63. {
  64. return (_enumerable ?? Empty).GetEnumerator();
  65. }
  66. IEnumerator IEnumerable.GetEnumerator()
  67. {
  68. return GetEnumerator();
  69. }
  70. /// <summary>
  71. /// Gets the <see cref="IJEnumerable{T}"/> of <see cref="JToken"/> with the specified key.
  72. /// </summary>
  73. /// <value></value>
  74. public IJEnumerable<JToken> this[object key]
  75. {
  76. get
  77. {
  78. if (_enumerable == null)
  79. {
  80. return JEnumerable<JToken>.Empty;
  81. }
  82. return new JEnumerable<JToken>(_enumerable.Values<T, JToken>(key));
  83. }
  84. }
  85. /// <summary>
  86. /// Determines whether the specified <see cref="JEnumerable{T}"/> is equal to this instance.
  87. /// </summary>
  88. /// <param name="other">The <see cref="JEnumerable{T}"/> to compare with this instance.</param>
  89. /// <returns>
  90. /// <c>true</c> if the specified <see cref="JEnumerable{T}"/> is equal to this instance; otherwise, <c>false</c>.
  91. /// </returns>
  92. public bool Equals(JEnumerable<T> other)
  93. {
  94. return Equals(_enumerable, other._enumerable);
  95. }
  96. /// <summary>
  97. /// Determines whether the specified <see cref="Object"/> is equal to this instance.
  98. /// </summary>
  99. /// <param name="obj">The <see cref="Object"/> to compare with this instance.</param>
  100. /// <returns>
  101. /// <c>true</c> if the specified <see cref="Object"/> is equal to this instance; otherwise, <c>false</c>.
  102. /// </returns>
  103. public override bool Equals(object obj)
  104. {
  105. if (obj is JEnumerable<T> enumerable)
  106. {
  107. return Equals(enumerable);
  108. }
  109. return false;
  110. }
  111. /// <summary>
  112. /// Returns a hash code for this instance.
  113. /// </summary>
  114. /// <returns>
  115. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
  116. /// </returns>
  117. public override int GetHashCode()
  118. {
  119. if (_enumerable == null)
  120. {
  121. return 0;
  122. }
  123. return _enumerable.GetHashCode();
  124. }
  125. }
  126. }