Skip to content

Instantly share code, notes, and snippets.

@qluana7
Last active July 5, 2021 02:12
Show Gist options
  • Save qluana7/855adf4f110417ea8cf85675e66ff164 to your computer and use it in GitHub Desktop.
Save qluana7/855adf4f110417ea8cf85675e66ff164 to your computer and use it in GitHub Desktop.
특정 아이템을 맨 앞이나 맨 뒤로 보낼 수 있는 Collection입니다. Recent Files 같은 최근 시스템을 만들때 사용됩니다
public class RecentCollection<T> : ICollection<T>
{
public RecentCollection()
{
_collection = new List<T>();
}
public RecentCollection(int capacity)
{
_collection = new List<T>(capacity);
}
public RecentCollection(IEnumerable<T> collection)
{
_collection = new List<T>(collection);
}
private ICollection<T> _collection;
public int Count => _collection.Count;
public bool IsReadOnly => _collection.IsReadOnly;
public void Add(T item)
=> _collection.Add(item);
public void Clear()
=> _collection.Clear();
public bool Contains(T item)
=> _collection.Contains(item);
public void CopyTo(T[] array, int arrayIndex)
=> _collection.CopyTo(array, arrayIndex);
public IEnumerator<T> GetEnumerator()
=> _collection.GetEnumerator();
public bool Remove(T item)
=> _collection.Remove(item);
public void Top(int index)
{
var item = _collection.ElementAt(index);
_collection.Remove(item);
var list = new List<T>(_collection);
list.Insert(0, item);
_collection = list;
}
public void Bottom(int index)
{
var item = _collection.ElementAt(index);
_collection.Remove(item);
_collection.Add(item);
}
IEnumerator IEnumerable.GetEnumerator()
=> ((IEnumerable)_collection).GetEnumerator();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment