Advertisement
Guest User

CacheHandler

a guest
Oct 25th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace Cache
  6. {
  7.     public class CacheHandler<T> : IEnumerator, IEnumerable
  8.     {
  9.         private int _index = -1;
  10.         private Func<List<T>> _refresh { get; set; }
  11.  
  12.         public List<T> Items { get; set; }
  13.  
  14.         public CacheHandler(Func<List<T>> refresh)
  15.         {
  16.             _refresh = refresh;
  17.         }
  18.  
  19.         public void Refresh()
  20.         {
  21.             Items = _refresh();
  22.         }
  23.  
  24.         public bool MoveNext()
  25.         {
  26.             _index += 1;
  27.             return _index < Items.Count;
  28.         }
  29.  
  30.         public void Reset()
  31.         {
  32.             _index = 0;
  33.         }
  34.  
  35.         public IEnumerator GetEnumerator()
  36.         {
  37.             return this;
  38.         }
  39.  
  40.         public T this[int index]
  41.         {
  42.             get
  43.             {
  44.                 if (Items == null)
  45.                     Refresh();
  46.  
  47.                 if (Items == null || index < 0 || Items.Count == index)
  48.                     return default(T);
  49.  
  50.                 return Items[index];
  51.             }
  52.         }
  53.  
  54.         public int Count => (Items ?? new List<T>()).Count;
  55.  
  56.         public int Length => Count;
  57.  
  58.         public object Current
  59.         {
  60.             get
  61.             {
  62.                 return Items[_index];
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement