#region License // Copyright (c) 2007 James Newton-King // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. #endregion using System; using System.Collections.Generic; #if !HAVE_LINQ using LC.Newtonsoft.Json.Utilities.LinqBridge; #else using System.Linq; #endif using LC.Newtonsoft.Json.Utilities; using System.Collections; namespace LC.Newtonsoft.Json.Linq { /// /// Represents a collection of objects. /// /// The type of token. public readonly struct JEnumerable : IJEnumerable, IEquatable> where T : JToken { /// /// An empty collection of objects. /// public static readonly JEnumerable Empty = new JEnumerable(Enumerable.Empty()); private readonly IEnumerable _enumerable; /// /// Initializes a new instance of the struct. /// /// The enumerable. public JEnumerable(IEnumerable enumerable) { ValidationUtils.ArgumentNotNull(enumerable, nameof(enumerable)); _enumerable = enumerable; } /// /// Returns an enumerator that can be used to iterate through the collection. /// /// /// A that can be used to iterate through the collection. /// public IEnumerator GetEnumerator() { return (_enumerable ?? Empty).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } /// /// Gets the of with the specified key. /// /// public IJEnumerable this[object key] { get { if (_enumerable == null) { return JEnumerable.Empty; } return new JEnumerable(_enumerable.Values(key)); } } /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// public bool Equals(JEnumerable other) { return Equals(_enumerable, other._enumerable); } /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// public override bool Equals(object obj) { if (obj is JEnumerable enumerable) { return Equals(enumerable); } return false; } /// /// Returns a hash code for this instance. /// /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public override int GetHashCode() { if (_enumerable == null) { return 0; } return _enumerable.GetHashCode(); } } }