[NUI] SVACE error fix (#258)
[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 { return _list.Count; }
46                 }
47
48                 bool ICollection<T>.IsReadOnly
49                 {
50                         get { return false; }
51                 }
52
53                 public bool Remove(T item)
54                 {
55                         lock (_list)
56                         {
57                                 if (_list.Remove(item))
58                                 {
59                                         _snapshot = null;
60                                         return true;
61                                 }
62
63                                 return false;
64                         }
65                 }
66
67                 IEnumerator IEnumerable.GetEnumerator()
68                 {
69                         return GetEnumerator();
70                 }
71
72                 public IEnumerator<T> GetEnumerator()
73                 {
74                         ReadOnlyCollection<T> snap = _snapshot;
75                         if (snap == null)
76                         {
77                                 lock (_list)
78                                         _snapshot = snap = new ReadOnlyCollection<T>(_list.ToList());
79                         }
80
81                         return snap?.GetEnumerator();
82                 }
83
84                 public int IndexOf(T item)
85                 {
86                         lock (_list)
87                                 return _list.IndexOf(item);
88                 }
89
90                 public void Insert(int index, T item)
91                 {
92                         lock (_list)
93                         {
94                                 _list.Insert(index, item);
95                                 _snapshot = null;
96                         }
97                 }
98
99                 public T this[int index]
100                 {
101                         get
102                         {
103                                 ReadOnlyCollection<T> snap = _snapshot;
104                                 if (snap != null)
105                                         return snap[index];
106
107                                 lock (_list)
108                                         return _list[index];
109                         }
110
111                         set
112                         {
113                                 lock (_list)
114                                 {
115                                         _list[index] = value;
116                                         _snapshot = null;
117                                 }
118                         }
119                 }
120
121                 public void RemoveAt(int index)
122                 {
123                         lock (_list)
124                         {
125                                 _list.RemoveAt(index);
126                                 _snapshot = null;
127                         }
128                 }
129         }
130 }