From 6fc21c3e0706b440a217430ffb3278e46441d732 Mon Sep 17 00:00:00 2001 From: Cory Charlton <ccharlton@pobox.com> Date: Tue, 13 Dec 2016 22:32:51 -0800 Subject: [PATCH 1/2] General code cleanup --- .../Generic/ThreadSafeQueueTest.cs | 2 +- Source/Core/CCSWE.Core.nuspec | 9 +++-- .../Collections/Generic/ThreadSafeQueue`1.cs | 16 ++++++--- .../SynchronizedObservableCollection`1.cs | 33 +++++++++++++++---- ...chronizedReadOnlyObservableCollection`1.cs | 1 + Source/Core/Properties/AssemblyVersion.cs | 4 +-- 6 files changed, 48 insertions(+), 17 deletions(-) diff --git a/Source/Core.UnitTests/Collections/Generic/ThreadSafeQueueTest.cs b/Source/Core.UnitTests/Collections/Generic/ThreadSafeQueueTest.cs index d2c2609..d4fd6b0 100644 --- a/Source/Core.UnitTests/Collections/Generic/ThreadSafeQueueTest.cs +++ b/Source/Core.UnitTests/Collections/Generic/ThreadSafeQueueTest.cs @@ -282,7 +282,7 @@ public void It_throws_exception_when_offset_is_less_than_count() public class When_ICollection_IsSynchronized_is_called { [Test] - public void Itreturns_true() + public void It_returns_true() { Assert.That(((ICollection)new ThreadSafeQueue<string>()).IsSynchronized, Is.True); } diff --git a/Source/Core/CCSWE.Core.nuspec b/Source/Core/CCSWE.Core.nuspec index b6104e7..d4aba7e 100644 --- a/Source/Core/CCSWE.Core.nuspec +++ b/Source/Core/CCSWE.Core.nuspec @@ -9,8 +9,13 @@ <licenseUrl>https://github.com/CoryCharlton/CCSWE.Core/blob/master/LICENSE.md</licenseUrl> <projectUrl>https://github.com/CoryCharlton/CCSWE.Core</projectUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> - <description>Just a bunch of C# .NET classes and extension methods I find useful</description> - <releaseNotes>Minor bug fixes</releaseNotes> + <description>Just a bunch of C# .NET classes and extension methods I find useful.</description> + <releaseNotes> + - Improved ThreadSafeQueue performance + - Fixed a bug where performing the set operation on the SynchronizedObservableCollection indexer might fail + - Added Ensure class + - General code cleanup and documentation improvements + </releaseNotes> <copyright>Copyright 2016</copyright> <tags>Utilities Threading Collections AppSettings Extensions ThreadPool ObservableCollection</tags> </metadata> diff --git a/Source/Core/Collections/Generic/ThreadSafeQueue`1.cs b/Source/Core/Collections/Generic/ThreadSafeQueue`1.cs index 522fe36..28f5cfb 100644 --- a/Source/Core/Collections/Generic/ThreadSafeQueue`1.cs +++ b/Source/Core/Collections/Generic/ThreadSafeQueue`1.cs @@ -73,9 +73,15 @@ public int Count } } - bool ICollection.IsSynchronized => true; + /// <summary>Gets a value indicating whether access to the <see cref="ThreadSafeQueue{T}" /> is synchronized (thread safe).</summary> + /// <returns>true if access to the <see cref="ThreadSafeQueue{T}" /> is synchronized (thread safe); otherwise, false.</returns> + protected bool IsSynchronized => true; - object ICollection.SyncRoot + bool ICollection.IsSynchronized => IsSynchronized; + + /// <summary>Gets an object that can be used to synchronize access to the <see cref="ThreadSafeQueue{T}" />.</summary> + /// <returns>An object that can be used to synchronize access to the <see cref="ThreadSafeQueue{T}" />.</returns> + protected object SyncRoot { get { @@ -87,6 +93,8 @@ object ICollection.SyncRoot return _syncRoot; } } + + object ICollection.SyncRoot => SyncRoot; #endregion #region Protected Methods @@ -224,9 +232,7 @@ public void Enqueue(T item) } /// <summary>Adds the elements of the specified collection to the end of the <see cref="ThreadSafeQueue{T}" />.</summary> - #pragma warning disable 1584,1711,1572,1581,1580 - /// <param name="collection">The collection whose elements should be added to the end of the <see cref="ThreadSafeQueue{T}" />. The collection itself cannot be null, but it can contain elements that are null, if type <paramref name="T" /> is a reference type.</param> - #pragma warning restore 1584,1711,1572,1581,1580 + /// <param name="collection">The collection whose elements should be added to the end of the <see cref="ThreadSafeQueue{T}" />. The collection itself cannot be null, but it can contain elements that are null, if type is a reference type.</param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="collection" /> is null.</exception> [SuppressMessage("ReSharper", "PossibleMultipleEnumeration")] diff --git a/Source/Core/Collections/ObjectModel/SynchronizedObservableCollection`1.cs b/Source/Core/Collections/ObjectModel/SynchronizedObservableCollection`1.cs index c7d39eb..b82cc34 100644 --- a/Source/Core/Collections/ObjectModel/SynchronizedObservableCollection`1.cs +++ b/Source/Core/Collections/ObjectModel/SynchronizedObservableCollection`1.cs @@ -60,13 +60,16 @@ public SynchronizedObservableCollection(IEnumerable<T> collection, Synchronizati #endregion #region Private Fields + [NonSerialized] private readonly SynchronizationContext _context; private bool _isDisposed; private readonly IList<T> _items = new List<T>(); + [NonSerialized] private readonly ReaderWriterLockSlim _itemsLocker = new ReaderWriterLockSlim(); - [NonSerialized] private object _syncRoot; - + [NonSerialized] private readonly SimpleMonitor _monitor = new SimpleMonitor(); + [NonSerialized] + private object _syncRoot; #endregion #region Events @@ -84,15 +87,29 @@ event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged #endregion #region Private Properties - bool IList.IsFixedSize => false; + /// <summary>Gets a value indicating whether the <see cref="SynchronizedObservableCollection{T}" />.</summary> + /// <returns>true if the <see cref="SynchronizedObservableCollection{T}" /> has a fixed size; otherwise, false.</returns> + protected bool IsFixedSize => false; + + bool IList.IsFixedSize => IsFixedSize; + + /// <summary>Gets a value indicating whether the <see cref="SynchronizedObservableCollection{T}" /> is read-only.</summary> + /// <returns>true if the <see cref="SynchronizedObservableCollection{T}" /> is read-only; otherwise, false.</returns> + protected bool IsReadOnly => false; - bool ICollection<T>.IsReadOnly => false; + bool ICollection<T>.IsReadOnly => IsReadOnly; - bool IList.IsReadOnly => false; + bool IList.IsReadOnly => IsReadOnly; - bool ICollection.IsSynchronized => true; + /// <summary>Gets a value indicating whether access to the <see cref="SynchronizedObservableCollection{T}" /> is synchronized (thread safe).</summary> + /// <returns>true if access to the <see cref="SynchronizedObservableCollection{T}" /> is synchronized (thread safe); otherwise, false.</returns> + protected bool IsSynchronized => true; - object ICollection.SyncRoot + bool ICollection.IsSynchronized => IsSynchronized; + + /// <summary>Gets an object that can be used to synchronize access to the <see cref="SynchronizedObservableCollection{T}" />.</summary> + /// <returns>An object that can be used to synchronize access to the <see cref="SynchronizedObservableCollection{T}" />.</returns> + protected object SyncRoot { get { @@ -123,6 +140,8 @@ object ICollection.SyncRoot } } + object ICollection.SyncRoot => SyncRoot; + object IList.this[int index] { get { return this[index]; } diff --git a/Source/Core/Collections/ObjectModel/SynchronizedReadOnlyObservableCollection`1.cs b/Source/Core/Collections/ObjectModel/SynchronizedReadOnlyObservableCollection`1.cs index b20618a..5f180b5 100644 --- a/Source/Core/Collections/ObjectModel/SynchronizedReadOnlyObservableCollection`1.cs +++ b/Source/Core/Collections/ObjectModel/SynchronizedReadOnlyObservableCollection`1.cs @@ -51,6 +51,7 @@ public SynchronizedReadOnlyObservableCollection(SynchronizedObservableCollection #endregion #region Private Fields + [NonSerialized] private readonly SynchronizationContext _context; #endregion diff --git a/Source/Core/Properties/AssemblyVersion.cs b/Source/Core/Properties/AssemblyVersion.cs index b3cdc6f..4953d6f 100644 --- a/Source/Core/Properties/AssemblyVersion.cs +++ b/Source/Core/Properties/AssemblyVersion.cs @@ -5,5 +5,5 @@ using System.Reflection; -[assembly: AssemblyVersion("2.0.348.1311")] -[assembly: AssemblyFileVersion("2.0.348.1311")] +[assembly: AssemblyVersion("2.0.348.3718")] +[assembly: AssemblyFileVersion("2.0.348.3718")] From 6d25ec8892698f197eb66a681cb0bcb4eb80d658 Mon Sep 17 00:00:00 2001 From: Cory Charlton <ccharlton@pobox.com> Date: Tue, 13 Dec 2016 22:39:24 -0800 Subject: [PATCH 2/2] Cleaned up EnsureTest --- Source/Core.UnitTests/EnsureTest.cs | 19 +++++++++++++++---- Source/Core/Properties/AssemblyVersion.cs | 4 ++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Source/Core.UnitTests/EnsureTest.cs b/Source/Core.UnitTests/EnsureTest.cs index 88b3d51..d895120 100644 --- a/Source/Core.UnitTests/EnsureTest.cs +++ b/Source/Core.UnitTests/EnsureTest.cs @@ -51,11 +51,22 @@ public void It_does_not_throw_exception_when_input_is_not_null() } [Test] - public void It_throws_exception_when_input_is_null_or_whitespace() + public void It_throws_exception_when_input_is_empty() { - Assert.Throws<ArgumentException>(() => Ensure.IsNotNullOrWhitespace("It_throws_exception_when_input_is_null_or_whitespace", null)); - Assert.Throws<ArgumentException>(() => Ensure.IsNotNullOrWhitespace("It_throws_exception_when_input_is_null_or_whitespace", " ")); - Assert.Throws<ArgumentException>(() => Ensure.IsNotNullOrWhitespace("It_throws_exception_when_input_is_null_or_whitespace", "\t")); + Assert.Throws<ArgumentException>(() => Ensure.IsNotNullOrWhitespace("It_throws_exception_when_input_is_empty", "")); + } + + [Test] + public void It_throws_exception_when_input_is_null() + { + Assert.Throws<ArgumentException>(() => Ensure.IsNotNullOrWhitespace("It_throws_exception_when_input_is_null", null)); + } + + [Test] + public void It_throws_exception_when_input_is_whitespace() + { + Assert.Throws<ArgumentException>(() => Ensure.IsNotNullOrWhitespace("It_throws_exception_when_input_is_whitespace", " ")); + Assert.Throws<ArgumentException>(() => Ensure.IsNotNullOrWhitespace("It_throws_exception_when_input_is_whitespace", "\t")); } } diff --git a/Source/Core/Properties/AssemblyVersion.cs b/Source/Core/Properties/AssemblyVersion.cs index 4953d6f..5a816e0 100644 --- a/Source/Core/Properties/AssemblyVersion.cs +++ b/Source/Core/Properties/AssemblyVersion.cs @@ -5,5 +5,5 @@ using System.Reflection; -[assembly: AssemblyVersion("2.0.348.3718")] -[assembly: AssemblyFileVersion("2.0.348.3718")] +[assembly: AssemblyVersion("2.0.348.3734")] +[assembly: AssemblyFileVersion("2.0.348.3734")]