d0829e4daffc073dc8e3cb7d9fe80c252e48dcee
[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             ReadOnlyCollection<T> snap = _snapshot;
79             if (snap == null)
80             {
81                 lock (_list)
82                     _snapshot = snap = new ReadOnlyCollection<T>(_list.ToList());
83             }
84
85             return snap?.GetEnumerator();
86         }
87
88         public int IndexOf(T item)
89         {
90             lock (_list)
91                 return _list.IndexOf(item);
92         }
93
94         public void Insert(int index, T item)
95         {
96             lock (_list)
97             {
98                 _list.Insert(index, item);
99                 _snapshot = null;
100             }
101         }
102
103         public T this[int index]
104         {
105             get
106             {
107                 lock (_list)
108                 {
109                     ReadOnlyCollection<T> snap = _snapshot;
110                     if (snap != null)
111                         return snap[index];
112
113                     return _list[index];
114                 }
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 }