Advertisement
Guest User

CacheHandler

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