From 482576f16dbea582c2c1d8ef7485d062e33d735f Mon Sep 17 00:00:00 2001 From: Archi Date: Wed, 21 Feb 2024 13:18:23 +0100 Subject: [PATCH] Add extra constructors also to other public collections --- .../Collections/ConcurrentHashSet.cs | 2 ++ ArchiSteamFarm/Collections/ConcurrentList.cs | 20 +++++++++++---- .../ObservableConcurrentDictionary.cs | 25 ++++++++++++++++++- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/ArchiSteamFarm/Collections/ConcurrentHashSet.cs b/ArchiSteamFarm/Collections/ConcurrentHashSet.cs index 394cb0e54..01598bb8e 100644 --- a/ArchiSteamFarm/Collections/ConcurrentHashSet.cs +++ b/ArchiSteamFarm/Collections/ConcurrentHashSet.cs @@ -24,6 +24,7 @@ using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; +using System.Text.Json.Serialization; using JetBrains.Annotations; namespace ArchiSteamFarm.Collections; @@ -37,6 +38,7 @@ public sealed class ConcurrentHashSet : IReadOnlySet, ISet where T : no private readonly ConcurrentDictionary BackingCollection; + [JsonConstructor] public ConcurrentHashSet() => BackingCollection = new ConcurrentDictionary(); public ConcurrentHashSet(IEnumerable collection) { diff --git a/ArchiSteamFarm/Collections/ConcurrentList.cs b/ArchiSteamFarm/Collections/ConcurrentList.cs index 4cfe6667c..9d5ae73db 100644 --- a/ArchiSteamFarm/Collections/ConcurrentList.cs +++ b/ArchiSteamFarm/Collections/ConcurrentList.cs @@ -22,6 +22,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Text.Json.Serialization; using JetBrains.Annotations; using Nito.AsyncEx; @@ -31,9 +32,7 @@ public sealed class ConcurrentList : IList, IReadOnlyList where T : not [PublicAPI] public event EventHandler? OnModified; - public bool IsReadOnly => false; - - internal int Count { + public int Count { get { using (Lock.ReaderLock()) { return BackingCollection.Count; @@ -41,7 +40,9 @@ public sealed class ConcurrentList : IList, IReadOnlyList where T : not } } - private readonly List BackingCollection = []; + public bool IsReadOnly => false; + + private readonly List BackingCollection; private readonly AsyncReaderWriterLock Lock = new(); int ICollection.Count => Count; @@ -65,6 +66,15 @@ public sealed class ConcurrentList : IList, IReadOnlyList where T : not } } + [JsonConstructor] + public ConcurrentList() => BackingCollection = []; + + public ConcurrentList(IEnumerable collection) { + ArgumentNullException.ThrowIfNull(collection); + + BackingCollection = [..collection]; + } + public void Add(T item) { ArgumentNullException.ThrowIfNull(item); @@ -147,7 +157,7 @@ public sealed class ConcurrentList : IList, IReadOnlyList where T : not IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - internal void ReplaceWith(IEnumerable collection) { + public void ReplaceWith(IEnumerable collection) { ArgumentNullException.ThrowIfNull(collection); using (Lock.WriterLock()) { diff --git a/ArchiSteamFarm/Collections/ObservableConcurrentDictionary.cs b/ArchiSteamFarm/Collections/ObservableConcurrentDictionary.cs index 395587ac2..55a4d8e6a 100644 --- a/ArchiSteamFarm/Collections/ObservableConcurrentDictionary.cs +++ b/ArchiSteamFarm/Collections/ObservableConcurrentDictionary.cs @@ -23,6 +23,7 @@ using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Text.Json.Serialization; using JetBrains.Annotations; namespace ArchiSteamFarm.Collections; @@ -41,7 +42,7 @@ public sealed class ObservableConcurrentDictionary : IDictionary Keys => BackingDictionary.Keys; - private readonly ConcurrentDictionary BackingDictionary = new(); + private readonly ConcurrentDictionary BackingDictionary; int ICollection>.Count => BackingDictionary.Count; int IReadOnlyCollection>.Count => BackingDictionary.Count; @@ -65,6 +66,28 @@ public sealed class ObservableConcurrentDictionary : IDictionary BackingDictionary = new ConcurrentDictionary(); + + public ObservableConcurrentDictionary(IEnumerable> collection) { + ArgumentNullException.ThrowIfNull(collection); + + BackingDictionary = new ConcurrentDictionary(collection); + } + + public ObservableConcurrentDictionary(IEqualityComparer comparer) { + ArgumentNullException.ThrowIfNull(comparer); + + BackingDictionary = new ConcurrentDictionary(comparer); + } + + public ObservableConcurrentDictionary(IEnumerable> collection, IEqualityComparer comparer) { + ArgumentNullException.ThrowIfNull(collection); + ArgumentNullException.ThrowIfNull(comparer); + + BackingDictionary = new ConcurrentDictionary(collection, comparer); + } + public void Add(KeyValuePair item) { (TKey key, TValue value) = item;