All files
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json / Linq / JEnumerable.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Newtonsoft.Json.Utilities;
6 using System.Collections;
7
8 namespace Newtonsoft.Json.Linq
9 {
10   /// <summary>
11   /// Represents a collection of <see cref="JToken"/> objects.
12   /// </summary>
13   /// <typeparam name="T">The type of token</typeparam>
14   public struct JEnumerable<T> : IJEnumerable<T> where T : JToken
15   {
16     /// <summary>
17     /// An empty collection of <see cref="JToken"/> objects.
18     /// </summary>
19     public static readonly JEnumerable<T> Empty = new JEnumerable<T>(Enumerable.Empty<T>());
20
21     private IEnumerable<T> _enumerable;
22
23     /// <summary>
24     /// Initializes a new instance of the <see cref="JEnumerable{T}"/> struct.
25     /// </summary>
26     /// <param name="enumerable">The enumerable.</param>
27     public JEnumerable(IEnumerable<T> enumerable)
28     {
29       ValidationUtils.ArgumentNotNull(enumerable, "enumerable");
30
31       _enumerable = enumerable;
32     }
33
34     /// <summary>
35     /// Returns an enumerator that iterates through the collection.
36     /// </summary>
37     /// <returns>
38     /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
39     /// </returns>
40     public IEnumerator<T> GetEnumerator()
41     {
42       return _enumerable.GetEnumerator();
43     }
44
45     /// <summary>
46     /// Returns an enumerator that iterates through a collection.
47     /// </summary>
48     /// <returns>
49     /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
50     /// </returns>
51     IEnumerator IEnumerable.GetEnumerator()
52     {
53       return GetEnumerator();
54     }
55
56     /// <summary>
57     /// Gets the <see cref="IJEnumerable{JToken}"/> with the specified key.
58     /// </summary>
59     /// <value></value>
60     public IJEnumerable<JToken> this[object key]
61     {
62       get { return new JEnumerable<JToken>(Extensions.Values<T, JToken>(_enumerable, key)); }
63     }
64
65     /// <summary>
66     /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
67     /// </summary>
68     /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
69     /// <returns>
70     ///         <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
71     /// </returns>
72     public override bool Equals(object obj)
73     {
74       if (obj is JEnumerable<T>)
75         return _enumerable.Equals(((JEnumerable<T>)obj)._enumerable);
76
77       return false;
78     }
79
80     /// <summary>
81     /// Returns a hash code for this instance.
82     /// </summary>
83     /// <returns>
84     /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
85     /// </returns>
86     public override int GetHashCode()
87     {
88       return _enumerable.GetHashCode();
89     }
90   }
91 }