Jelajahi Sumber

Add LockedList

Dalibor Votruba 2 tahun lalu
induk
melakukan
8bc2336eba

+ 1 - 1
qdr.fnd.core.nlog/qdr.fnd.core.nlog.csproj

@@ -31,7 +31,7 @@
   <ItemGroup>
     <PackageReference Include="NLog" Version="5.2.4" />
     <PackageReference Include="NLog.Extensions.Logging" Version="5.3.4" />
-    <PackageReference Include="qdr.fnd.core" Version="0.0.1-alpha" />
+    <PackageReference Include="qdr.fnd.core" Version="0.0.2-alpha" />
   </ItemGroup>
 
 </Project>

+ 155 - 0
qdr.fnd.core/Collections/LockedList.cs

@@ -0,0 +1,155 @@
+using System.Collections;
+using System.Collections.Generic;
+
+namespace Quadarax.Foundation.Core.Collections
+{
+
+    public class LockedList<TElement> : IList<TElement>
+    {
+
+        #region *** Private Fields ***
+        private readonly List<TElement> _list;
+        #endregion
+
+        #region *** Public Properties ***
+
+        public int Count
+        {
+            get
+            {
+                lock (_list)
+                {
+                    return _list.Count;
+                }
+            }
+        }
+
+        public bool IsReadOnly
+        {
+            get
+            {
+                lock (_list)
+                {
+                    return ((IList<TElement>)_list).IsReadOnly;
+                }
+            }
+        }
+
+        #endregion
+
+        #region *** Indexers ***
+        public TElement this[int index]
+        {
+            get
+            {
+                lock (_list)
+                {
+                    return _list[index];
+                }
+            }
+            set
+            {
+                lock (_list)
+                {
+                    _list[index] = value;
+                }
+            }
+        }
+        #endregion
+
+        #region *** Constructors ***
+        public LockedList()
+        {
+            _list = new List<TElement>();
+        }
+        public LockedList(IEnumerable<TElement> initialItems)
+        {
+            _list = new List<TElement>(initialItems);
+        }
+        public LockedList(int capacity)
+        {
+            _list = new List<TElement>(capacity);
+        }
+        #endregion
+
+        #region *** Public Methods ***
+        public IEnumerator<TElement> GetEnumerator()
+        {
+            lock (_list)
+            {
+                return _list.GetEnumerator();
+            }
+        }
+
+        IEnumerator IEnumerable.GetEnumerator()
+        {
+            return GetEnumerator();
+        }
+
+        public void Add(TElement item)
+        {
+            lock (_list)
+            {
+                _list.Add(item);
+            }
+        }
+
+        public void Clear()
+        {
+            lock (_list)
+            {
+                _list.Clear();
+            }
+        }
+
+        public bool Contains(TElement item)
+        {
+            lock (_list)
+            {
+                return _list.Contains(item);
+            }
+        }
+
+        public void CopyTo(TElement[] array, int arrayIndex)
+        {
+            lock (_list)
+            {
+                _list.CopyTo(array, arrayIndex);
+            }
+        }
+
+        public bool Remove(TElement item)
+        {
+            lock (_list)
+            {
+                return _list.Remove(item);
+            }
+        }
+
+        public int IndexOf(TElement item)
+        {
+            lock (_list)
+            {
+                return _list.IndexOf(item);
+            }
+        }
+
+        public void Insert(int index, TElement item)
+        {
+            lock (_list)
+            {
+                _list.Insert(index, item);
+            }
+        }
+
+        public void RemoveAt(int index)
+        {
+            lock (_list)
+            {
+                _list.RemoveAt(index);
+            }
+        }
+
+        #endregion
+    }
+}

+ 2 - 1
qdr.fnd.core/releasenotes.md

@@ -2,13 +2,14 @@
 Ordered by version descending
 
 ## 0.0.2.0
-Date:__2.10.2023__
+Date:__3.10.2023__
 - extends Thread/LoopWorker to support external ansync iteration body expression
 - fix Thread/ContiguousWorker to support external ansync iteration body expression
 - add Thread/BasicWorkerContext and extends IWorkerContext to stores CancellationToken
 - extends Thread/AbstractWorker to work with default IWorkerContext, make public AbstractWorker.Context
 - add Json/Extensions/JsonDocumentExt.Diff function to compare two JsonDocument
 - extends Logging/ConsoleLogger to support exclude filter in initialization
+- add Collections/LockedList to support thread safe list
 - fix quality CA
 - minor fixes
 ---