Merge pull request #164 from LogosBible/AppSettingsFileChanges
[mono.git] / mcs / class / System.IO.Compression / SharpCompress / LazyReadOnlyCollection.cs
1 using System;
2 using System.Collections.Generic;
3
4 namespace SharpCompress
5 {
6     internal class LazyReadOnlyCollection<T> : ICollection<T>
7     {
8         private readonly List<T> backing = new List<T>();
9         private readonly IEnumerator<T> source;
10         private bool fullyLoaded;
11
12         public LazyReadOnlyCollection(IEnumerable<T> source)
13         {
14             this.source = source.GetEnumerator();
15         }
16
17         private class LazyLoader : IEnumerator<T>
18         {
19             private readonly LazyReadOnlyCollection<T> lazyReadOnlyCollection;
20             private bool disposed;
21             private int index = -1;
22
23             internal LazyLoader(LazyReadOnlyCollection<T> lazyReadOnlyCollection)
24             {
25                 this.lazyReadOnlyCollection = lazyReadOnlyCollection;
26             }
27
28             #region IEnumerator<T> Members
29
30             public T Current
31             {
32                 get { return lazyReadOnlyCollection.backing[index]; }
33             }
34
35             #endregion
36
37             #region IDisposable Members
38
39             public void Dispose()
40             {
41                 if (!disposed)
42                 {
43                     disposed = true;
44                 }
45             }
46
47             #endregion
48
49             #region IEnumerator Members
50
51             object System.Collections.IEnumerator.Current
52             {
53                 get { return Current; }
54             }
55
56             public bool MoveNext()
57             {
58                 if (index + 1 < lazyReadOnlyCollection.backing.Count)
59                 {
60                     index++;
61                     return true;
62                 }
63                 if (!lazyReadOnlyCollection.fullyLoaded && lazyReadOnlyCollection.source.MoveNext())
64                 {
65                     lazyReadOnlyCollection.backing.Add(lazyReadOnlyCollection.source.Current);
66                     index++;
67                     return true;
68                 }
69                 lazyReadOnlyCollection.fullyLoaded = true;
70                 return false;
71             }
72
73             public void Reset()
74             {
75                 throw new NotImplementedException();
76             }
77
78             #endregion
79         }
80
81         internal void EnsureFullyLoaded()
82         {
83             if (!fullyLoaded)
84             {
85                 this.ForEach(x => { });
86                 fullyLoaded = true;
87             }
88         }
89
90         internal IEnumerable<T> GetLoaded()
91         {
92             return backing;
93         }
94
95         #region ICollection<T> Members
96
97         public void Add(T item)
98         {
99             throw new NotImplementedException();
100         }
101
102         public void Clear()
103         {
104             throw new NotImplementedException();
105         }
106
107         public bool Contains(T item)
108         {
109             EnsureFullyLoaded();
110             return backing.Contains(item);
111         }
112
113         public void CopyTo(T[] array, int arrayIndex)
114         {
115             EnsureFullyLoaded();
116             backing.CopyTo(array, arrayIndex);
117         }
118
119         public int Count
120         {
121             get
122             {
123                 EnsureFullyLoaded();
124                 return backing.Count;
125             }
126         }
127
128         public bool IsReadOnly
129         {
130             get { return true; }
131         }
132
133         public bool Remove(T item)
134         {
135             throw new NotImplementedException();
136         }
137
138         #endregion
139
140         #region IEnumerable<T> Members
141
142         //TODO check for concurrent access
143         public IEnumerator<T> GetEnumerator()
144         {
145             return new LazyLoader(this);
146         }
147
148         #endregion
149
150         #region IEnumerable Members
151
152         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
153         {
154             return GetEnumerator();
155         }
156
157         #endregion
158     }
159 }