public sealed class VirtualServiceCollection<T> : IList<T>, IList
{
private readonly Func<int, int, T[]> dataFunction;
private readonly Func<int> countFunction;
private readonly int pageSize;
private readonly List<T> data;
private int currentPage;
public VirtualServiceCollection(Func<int,int,T[]> dataFunction, Func<int> countFunction, int pageSize)
{
this.dataFunction = dataFunction;
this.countFunction = countFunction;
this.pageSize = pageSize;
data = new List<T>(dataFunction(0, pageSize));
}
public IEnumerator<T> GetEnumerator()
{
var count = countFunction();
for (var i = 0; i < count; i++)
yield return this[i];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(T item)
{
throw new NotImplementedException();
}
public int Add(object value)
{
throw new NotImplementedException();
}
public bool Contains(object value)
{
throw new NotImplementedException();
}
void IList.Clear()
{
DoClear();
}
public int IndexOf(object value)
{
return data.IndexOf((T)value);
}
public void Insert(int index, object value)
{
throw new NotImplementedException();
}
public void Remove(object value)
{
throw new NotImplementedException();
}
void IList.RemoveAt(int index)
{
throw new NotImplementedException();
}
private T GetItem(int index)
{
var bot = currentPage * pageSize;
var top = Math.Min(bot + pageSize, countFunction());
if (index >= bot && index < top)
return data[index - bot];
currentPage = (int)Math.Floor(index / (double)pageSize);
data.Clear();
data.AddRange(dataFunction(currentPage, pageSize));
return data[index - (currentPage * pageSize)];
}
object IList.this[int index]
{
get { return GetItem(index); }
set { throw new NotImplementedException(); }
}
bool IList.IsReadOnly
{
get { return false; }
}
public bool IsFixedSize
{
get { return false; }
}
private void DoClear()
{
currentPage = 0;
data.Clear();
}
void ICollection<T>.Clear()
{
DoClear();
}
public bool Contains(T item)
{
throw new NotImplementedException();
}
public void CopyTo(T[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public bool Remove(T item)
{
throw new NotImplementedException();
}
public void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}
int ICollection.Count
{
get { return countFunction(); }
}
public object SyncRoot
{
get { return this; }
}
public bool IsSynchronized
{
get { return false; }
}
int ICollection<T>.Count
{
get { return countFunction(); }
}
bool ICollection<T>.IsReadOnly
{
get { return false; }
}
public int IndexOf(T item)
{
return data.IndexOf(item);
}
public void Insert(int index, T item)
{
throw new NotImplementedException();
}
void IList<T>.RemoveAt(int index)
{
throw new NotImplementedException();
}
public T this[int index]
{
get { return GetItem(index); }
set { throw new NotImplementedException(); }
}
}