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 
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 
35 namespace Newtonsoft.Json.Linq
36 {
37   /// <summary>
38   /// Represents a collection of <see cref="JToken"/> objects.
39   /// </summary>
40   /// <typeparam name="T">The type of token</typeparam>
41   public struct JEnumerable<T> : IJEnumerable<T> where T : JToken
42   {
43     /// <summary>
44     /// An empty collection of <see cref="JToken"/> objects.
45     /// </summary>
46     public static readonly JEnumerable<T> Empty = new JEnumerable<T>(Enumerable.Empty<T>());
47 
48     private readonly IEnumerable<T> _enumerable;
49 
50     /// <summary>
51     /// Initializes a new instance of the <see cref="JEnumerable{T}"/> struct.
52     /// </summary>
53     /// <param name="enumerable">The enumerable.</param>
JEnumerableNewtonsoft.Json.Linq.JEnumerable54     public JEnumerable(IEnumerable<T> enumerable)
55     {
56       ValidationUtils.ArgumentNotNull(enumerable, "enumerable");
57 
58       _enumerable = enumerable;
59     }
60 
61     /// <summary>
62     /// Returns an enumerator that iterates through the collection.
63     /// </summary>
64     /// <returns>
65     /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
66     /// </returns>
GetEnumeratorNewtonsoft.Json.Linq.JEnumerable67     public IEnumerator<T> GetEnumerator()
68     {
69       return _enumerable.GetEnumerator();
70     }
71 
72     /// <summary>
73     /// Returns an enumerator that iterates through a collection.
74     /// </summary>
75     /// <returns>
76     /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
77     /// </returns>
IEnumerable.GetEnumeratorNewtonsoft.Json.Linq.JEnumerable78     IEnumerator IEnumerable.GetEnumerator()
79     {
80       return GetEnumerator();
81     }
82 
83     /// <summary>
84     /// Gets the <see cref="IJEnumerable{JToken}"/> with the specified key.
85     /// </summary>
86     /// <value></value>
87     public IJEnumerable<JToken> this[object key]
88     {
89       get { return new JEnumerable<JToken>(Extensions.Values<T, JToken>(_enumerable, key)); }
90     }
91 
92     /// <summary>
93     /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
94     /// </summary>
95     /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
96     /// <returns>
97     /// 	<c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
98     /// </returns>
EqualsNewtonsoft.Json.Linq.JEnumerable99     public override bool Equals(object obj)
100     {
101       if (obj is JEnumerable<T>)
102         return _enumerable.Equals(((JEnumerable<T>)obj)._enumerable);
103 
104       return false;
105     }
106 
107     /// <summary>
108     /// Returns a hash code for this instance.
109     /// </summary>
110     /// <returns>
111     /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
112     /// </returns>
GetHashCodeNewtonsoft.Json.Linq.JEnumerable113     public override int GetHashCode()
114     {
115       return _enumerable.GetHashCode();
116     }
117   }
118 }
119