Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- namespace rocket_bot
- {
- public class Channel<T> where T : class
- {
- private readonly List<T> list = new List<T>();
- public T this[int index]
- {
- get
- {
- lock (list)
- {
- return list.Count <= index || index < 0 ? null : list[index];
- }
- }
- set
- {
- lock (list)
- {
- if (Count > index && index >= 0)
- {
- list[index] = value;
- list.RemoveRange(index + 1, list.Count - index - 1);
- }
- else if (index == list.Count)
- list.Add(value);
- }
- }
- }
- public T LastItem()
- {
- lock (list)
- {
- return list.Count == 0 ? null : list[list.Count - 1];
- }
- }
- public void AppendIfLastItemIsUnchanged(T item, T knownLastItem)
- {
- lock (item)
- {
- if (Equals(LastItem(), knownLastItem)) list.Add(item);
- }
- }
- public int Count
- {
- get
- {
- lock (list)
- {
- return list.Count;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment