[NUI] Sync with API5 (#617)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / XamlBinding / SynchronizedList.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using System.Linq;
5
6 namespace Tizen.NUI.Binding
7 {
8     internal class SynchronizedList<T> : IList<T>, IReadOnlyList<T>
9     {
10         readonly List<T> _list = new List<T>();
11         ReadOnlyCollection<T> _snapshot;
12
13         public void Add(T item)
14         {
15             lock (_list)
16             {
17                 _list.Add(item);
18                 _snapshot = null;
19             }
20         }
21
22         public void Clear()
23         {
24             lock (_list)
25             {
26                 _list.Clear();
27                 _snapshot = null;
28             }
29         }
30
31         public bool Contains(T item)
32         {
33             lock (_list)
34                 return _list.Contains(item);
35         }
36
37         public void CopyTo(T[] array, int arrayIndex)
38         {
39             lock (_list)
40                 _list.CopyTo(array, arrayIndex);
41         }
42
43         public int Count
44         {
45             get 
46             {
47                 lock (_list)
48                     return _list.Count; 
49             }
50         }
51
52         bool ICollection<T>.IsReadOnly
53         {
54             get { return false; }
55         }
56
57         public bool Remove(T item)
58         {
59             lock (_list)
60             {
61                 if (_list.Remove(item))
62                 {
63                     _snapshot = null;
64                     return true;
65                 }
66
67                 return false;
68             }
69         }
70
71         IEnumerator IEnumerable.GetEnumerator()
72         {
73             return GetEnumerator();
74         }
75
76         public IEnumerator<T> GetEnumerator()
77         {
78             lock (_list)
79             {
80                 ReadOnlyCollection<T> snap = _snapshot;
81                 if (snap == null)
82                 {
83                     _snapshot = snap = new ReadOnlyCollection<T>(_list.ToList());
84                 }
85                 return snap?.GetEnumerator();
86             }
87         }
88
89         public int IndexOf(T item)
90         {
91             lock (_list)
92                 return _list.IndexOf(item);
93         }
94
95         public void Insert(int index, T item)
96         {
97             lock (_list)
98             {
99                 _list.Insert(index, item);
100                 _snapshot = null;
101             }
102         }
103
104         public T this[int index]
105         {
106             get
107             {
108                 lock (_list)
109                 {
110                     ReadOnlyCollection<T> snap = _snapshot;
111                     if (snap != null)
112                         return snap[index];
113
114                     return _list[index];
115                 }
116             }
117
118             set
119             {
120                 lock (_list)
121                 {
122                     _list[index] = value;
123                     _snapshot = null;
124                 }
125             }
126         }
127
128         public void RemoveAt(int index)
129         {
130             lock (_list)
131             {
132                 _list.RemoveAt(index);
133                 _snapshot = null;
134             }
135         }
136     }
137 }