2 using System.Collections.Generic;
4 namespace SharpCompress
6 internal class LazyReadOnlyCollection<T> : ICollection<T>
8 private readonly List<T> backing = new List<T>();
9 private readonly IEnumerator<T> source;
10 private bool fullyLoaded;
12 public LazyReadOnlyCollection(IEnumerable<T> source)
14 this.source = source.GetEnumerator();
17 private class LazyLoader : IEnumerator<T>
19 private readonly LazyReadOnlyCollection<T> lazyReadOnlyCollection;
20 private bool disposed;
21 private int index = -1;
23 internal LazyLoader(LazyReadOnlyCollection<T> lazyReadOnlyCollection)
25 this.lazyReadOnlyCollection = lazyReadOnlyCollection;
28 #region IEnumerator<T> Members
32 get { return lazyReadOnlyCollection.backing[index]; }
37 #region IDisposable Members
49 #region IEnumerator Members
51 object System.Collections.IEnumerator.Current
53 get { return Current; }
56 public bool MoveNext()
58 if (index + 1 < lazyReadOnlyCollection.backing.Count)
63 if (!lazyReadOnlyCollection.fullyLoaded && lazyReadOnlyCollection.source.MoveNext())
65 lazyReadOnlyCollection.backing.Add(lazyReadOnlyCollection.source.Current);
69 lazyReadOnlyCollection.fullyLoaded = true;
75 throw new NotImplementedException();
81 internal void EnsureFullyLoaded()
85 this.ForEach(x => { });
90 internal IEnumerable<T> GetLoaded()
95 #region ICollection<T> Members
97 public void Add(T item)
99 throw new NotImplementedException();
104 throw new NotImplementedException();
107 public bool Contains(T item)
110 return backing.Contains(item);
113 public void CopyTo(T[] array, int arrayIndex)
116 backing.CopyTo(array, arrayIndex);
124 return backing.Count;
128 public bool IsReadOnly
133 public bool Remove(T item)
135 throw new NotImplementedException();
140 #region IEnumerable<T> Members
142 //TODO check for concurrent access
143 public IEnumerator<T> GetEnumerator()
145 return new LazyLoader(this);
150 #region IEnumerable Members
152 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
154 return GetEnumerator();