From ad5d682a936affb24bacee1a680603736aa5b15e Mon Sep 17 00:00:00 2001 From: kronic Date: Sun, 30 Jun 2024 01:34:19 +0300 Subject: [PATCH] use null check pattern (#911) Co-authored-by: Roland Pheasant --- .../Cache/Internal/ExpireAfter.ForSource.cs | 12 ++++++------ .../Cache/Internal/ExpireAfter.ForStream.cs | 12 ++++++------ src/DynamicData/Cache/Internal/FilterImmutable.cs | 2 +- src/DynamicData/Cache/Internal/OfType.cs | 2 +- .../Cache/Internal/ToObservableChangeSet.cs | 10 +++++----- .../Cache/Internal/ToObservableOptional.cs | 2 +- src/DynamicData/List/Internal/ExpireAfter.cs | 12 ++++++------ .../List/Internal/ToObservableChangeSet.cs | 10 +++++----- src/DynamicData/List/Internal/Transformer.cs | 2 +- 9 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/DynamicData/Cache/Internal/ExpireAfter.ForSource.cs b/src/DynamicData/Cache/Internal/ExpireAfter.ForSource.cs index bf8006e83..690d61249 100644 --- a/src/DynamicData/Cache/Internal/ExpireAfter.ForSource.cs +++ b/src/DynamicData/Cache/Internal/ExpireAfter.ForSource.cs @@ -25,7 +25,7 @@ public static IObservable>> Create( source.ThrowArgumentNullExceptionIfNull(nameof(source)); timeSelector.ThrowArgumentNullExceptionIfNull(nameof(timeSelector)); - return Observable.Create>>(observer => (pollingInterval is TimeSpan pollingIntervalValue) + return Observable.Create>>(observer => (pollingInterval is { } pollingIntervalValue) ? new PollingSubscription( observer: observer, pollingInterval: pollingIntervalValue, @@ -137,7 +137,7 @@ private void OnEditingSource(ISourceUpdater updater) lock (SynchronizationGate) { // The scheduler only promises "best effort" to cancel scheduled operations, so we need to make sure. - if (_nextScheduledManagement is not ScheduledManagement thisScheduledManagement) + if (_nextScheduledManagement is not { } thisScheduledManagement) return; _nextScheduledManagement = null; @@ -194,11 +194,11 @@ private void OnExpirationsChanged() _proposedExpirationsQueue.RemoveRange(0, removeToIndex); // Check if we need to re-schedule the next management operation - if (GetNextManagementDueTime() is DateTimeOffset nextManagementDueTime) + if (GetNextManagementDueTime() is { } nextManagementDueTime) { if (_nextScheduledManagement?.DueTime != nextManagementDueTime) { - if (_nextScheduledManagement is ScheduledManagement nextScheduledManagement) + if (_nextScheduledManagement is { } nextScheduledManagement) nextScheduledManagement.Cancellation.Dispose(); _nextScheduledManagement = new() @@ -251,7 +251,7 @@ private void OnSourceNext(IChangeSet changes) { case ChangeReason.Add: { - if (_timeSelector.Invoke(change.Current) is TimeSpan expireAfter) + if (_timeSelector.Invoke(change.Current) is { } expireAfter) { haveExpirationsChanged |= TrySetExpiration( key: change.Key, @@ -267,7 +267,7 @@ private void OnSourceNext(IChangeSet changes) case ChangeReason.Update: { - if (_timeSelector.Invoke(change.Current) is TimeSpan expireAfter) + if (_timeSelector.Invoke(change.Current) is { } expireAfter) { haveExpirationsChanged = TrySetExpiration( key: change.Key, diff --git a/src/DynamicData/Cache/Internal/ExpireAfter.ForStream.cs b/src/DynamicData/Cache/Internal/ExpireAfter.ForStream.cs index 9d5b42ce2..18a363100 100644 --- a/src/DynamicData/Cache/Internal/ExpireAfter.ForStream.cs +++ b/src/DynamicData/Cache/Internal/ExpireAfter.ForStream.cs @@ -25,7 +25,7 @@ public static IObservable> Create( source.ThrowArgumentNullExceptionIfNull(nameof(source)); timeSelector.ThrowArgumentNullExceptionIfNull(nameof(timeSelector)); - return Observable.Create>(observer => (pollingInterval is TimeSpan pollingIntervalValue) + return Observable.Create>(observer => (pollingInterval is { } pollingIntervalValue) ? new PollingSubscription( observer: observer, pollingInterval: pollingIntervalValue, @@ -113,7 +113,7 @@ private void ManageExpirations() lock (SynchronizationGate) { // The scheduler only promises "best effort" to cancel scheduled operations, so we need to make sure. - if (_nextScheduledManagement is not ScheduledManagement thisScheduledManagement) + if (_nextScheduledManagement is not { } thisScheduledManagement) return; _nextScheduledManagement = null; @@ -172,11 +172,11 @@ private void OnExpirationsChanged() } // Check if we need to re-schedule the next management operation - if (GetNextManagementDueTime() is DateTimeOffset nextManagementDueTime) + if (GetNextManagementDueTime() is { } nextManagementDueTime) { if (_nextScheduledManagement?.DueTime != nextManagementDueTime) { - if (_nextScheduledManagement is ScheduledManagement nextScheduledManagement) + if (_nextScheduledManagement is { } nextScheduledManagement) nextScheduledManagement.Cancellation.Dispose(); _nextScheduledManagement = new() @@ -234,7 +234,7 @@ private void OnSourceNext(IChangeSet upstreamChanges) { case ChangeReason.Add: { - if (_timeSelector.Invoke(change.Current) is TimeSpan expireAfter) + if (_timeSelector.Invoke(change.Current) is { } expireAfter) { haveExpirationsChanged |= TrySetExpiration( key: change.Key, @@ -257,7 +257,7 @@ private void OnSourceNext(IChangeSet upstreamChanges) case ChangeReason.Update: { - if (_timeSelector.Invoke(change.Current) is TimeSpan expireAfter) + if (_timeSelector.Invoke(change.Current) is { } expireAfter) { haveExpirationsChanged = TrySetExpiration( key: change.Key, diff --git a/src/DynamicData/Cache/Internal/FilterImmutable.cs b/src/DynamicData/Cache/Internal/FilterImmutable.cs index b7a5ddfd1..1fa57d033 100644 --- a/src/DynamicData/Cache/Internal/FilterImmutable.cs +++ b/src/DynamicData/Cache/Internal/FilterImmutable.cs @@ -71,7 +71,7 @@ public IObservable> Run() : null }; - if (downstreamReason is ChangeReason reason) + if (downstreamReason is { } reason) { // Do not propagate indexes, we can't guarantee them to be correct, because we aren't caching items. downstreamChanges.Add(new( diff --git a/src/DynamicData/Cache/Internal/OfType.cs b/src/DynamicData/Cache/Internal/OfType.cs index 7ca8e939a..c8662afd2 100644 --- a/src/DynamicData/Cache/Internal/OfType.cs +++ b/src/DynamicData/Cache/Internal/OfType.cs @@ -46,7 +46,7 @@ public IObservable> Run() => _ => default, }; - if (transformedChange is Change c) + if (transformedChange is { } c) { // Do not propagate indexes, we can't guarantee them to be correct, because we aren't caching items. downstreamChanges.Add(c); diff --git a/src/DynamicData/Cache/Internal/ToObservableChangeSet.cs b/src/DynamicData/Cache/Internal/ToObservableChangeSet.cs index f7e84a27d..21b197c88 100644 --- a/src/DynamicData/Cache/Internal/ToObservableChangeSet.cs +++ b/src/DynamicData/Cache/Internal/ToObservableChangeSet.cs @@ -147,7 +147,7 @@ public Subscription( _ => 0 }; - var changeSet = new ChangeSet(capacity: (_evictionState is EvictionState evictionState) + var changeSet = new ChangeSet(capacity: (_evictionState is { } evictionState) ? Math.Max(itemCount + evictionState.Queue.Count - evictionState.LimitSizeTo, 0) : itemCount); @@ -227,7 +227,7 @@ private void HandleIncomingItem( : null as ItemState?; // Perform processing for eviction behavior, if applicable - if (_evictionState is EvictionState evictionState) + if (_evictionState is { } evictionState) { // Backwards compatibility if (evictionState.LimitSizeTo is 0) @@ -260,11 +260,11 @@ private void HandleIncomingItem( // Perform processing for expiration behavior, if applicable var expireAt = null as DateTimeOffset?; - if (_expirationState is ExpirationState expirationState) + if (_expirationState is { } expirationState) { var previousExpireAt = previousItemState?.ExpireAt; var expireAfter = expirationState.ExpireAfter.Invoke(item); - if (expireAfter is TimeSpan resolvedExpireAfter) + if (expireAfter is { } resolvedExpireAfter) { // Truncate to milliseconds to promote batching expirations together. var expireAtTicks = now.UtcTicks + resolvedExpireAfter.Ticks; @@ -372,7 +372,7 @@ private void OnExpirationQueueChanged() { // If there's already a scheduled operation, and it doesn't match the current next-item-to-expire time, wipe it out and re-schedule it. var nextExpireAt = expirationState.Queue[0].ExpireAt; - if (_scheduledExpiration is ScheduledExpiration scheduledExpiration) + if (_scheduledExpiration is { } scheduledExpiration) { if (scheduledExpiration.DueTime != nextExpireAt) { diff --git a/src/DynamicData/Cache/Internal/ToObservableOptional.cs b/src/DynamicData/Cache/Internal/ToObservableOptional.cs index 15e8f8684..2da60c4a5 100644 --- a/src/DynamicData/Cache/Internal/ToObservableOptional.cs +++ b/src/DynamicData/Cache/Internal/ToObservableOptional.cs @@ -27,7 +27,7 @@ public IObservable> Run() => Observable.Create change) => change switch { - { Key: TKey thekey } when !thekey.Equals(_key) => false, + { Key: { } thekey } when !thekey.Equals(_key) => false, { Reason: ChangeReason.Add } => true, { Reason: ChangeReason.Remove } => true, { Reason: ChangeReason.Update, Previous.HasValue: false } => true, diff --git a/src/DynamicData/List/Internal/ExpireAfter.cs b/src/DynamicData/List/Internal/ExpireAfter.cs index 46a51f077..a0f9096a2 100644 --- a/src/DynamicData/List/Internal/ExpireAfter.cs +++ b/src/DynamicData/List/Internal/ExpireAfter.cs @@ -22,7 +22,7 @@ public static IObservable> Create( source.ThrowArgumentNullExceptionIfNull(nameof(source)); timeSelector.ThrowArgumentNullExceptionIfNull(nameof(timeSelector)); - return Observable.Create>(observer => (pollingInterval is TimeSpan pollingIntervalValue) + return Observable.Create>(observer => (pollingInterval is { } pollingIntervalValue) ? new PollingSubscription( observer: observer, pollingInterval: pollingIntervalValue, @@ -104,7 +104,7 @@ protected object SynchronizationGate foreach (var dueTime in _expirationDueTimes) { - if ((dueTime is DateTimeOffset value) && ((result is null) || (value < result))) + if ((dueTime is { } value) && ((result is null) || (value < result))) result = value; } @@ -134,7 +134,7 @@ private void OnEditingSource(IExtendedList updater) lock (SynchronizationGate) { // The scheduler only promises "best effort" to cancel scheduled operations, so we need to make sure. - if (_nextScheduledManagement is not ScheduledManagement thisScheduledManagement) + if (_nextScheduledManagement is not { } thisScheduledManagement) return; _nextScheduledManagement = null; @@ -148,7 +148,7 @@ private void OnEditingSource(IExtendedList updater) // Buffer removals, so we can eliminate the need for index adjustments as we update the source for (var i = 0; i < _expirationDueTimes.Count; ++i) { - if ((_expirationDueTimes[i] is DateTimeOffset dueTime) && (dueTime <= now)) + if ((_expirationDueTimes[i] is { } dueTime) && (dueTime <= now)) { _expiringIndexesBuffer.Add(i); @@ -187,11 +187,11 @@ private void OnEditingSource(IExtendedList updater) private void OnExpirationDueTimesChanged() { // Check if we need to re-schedule the next management operation - if (GetNextManagementDueTime() is DateTimeOffset nextManagementDueTime) + if (GetNextManagementDueTime() is { } nextManagementDueTime) { if (_nextScheduledManagement?.DueTime != nextManagementDueTime) { - if (_nextScheduledManagement is ScheduledManagement nextScheduledManagement) + if (_nextScheduledManagement is { } nextScheduledManagement) nextScheduledManagement.Cancellation.Dispose(); _nextScheduledManagement = new() diff --git a/src/DynamicData/List/Internal/ToObservableChangeSet.cs b/src/DynamicData/List/Internal/ToObservableChangeSet.cs index 50cd11c85..1b8992f41 100644 --- a/src/DynamicData/List/Internal/ToObservableChangeSet.cs +++ b/src/DynamicData/List/Internal/ToObservableChangeSet.cs @@ -136,7 +136,7 @@ public Subscription( _ => 0 }; - var changeSet = new ChangeSet(capacity: (_evictionState is EvictionState evictionState) + var changeSet = new ChangeSet(capacity: (_evictionState is { } evictionState) ? Math.Max(itemCount + evictionState.Queue.Count - evictionState.LimitSizeTo, 0) : itemCount); @@ -207,7 +207,7 @@ private void HandleIncomingItem( ref bool hasExpirationQueueChanged) { // Perform processing for eviction behavior, if applicable - if (_evictionState is EvictionState evictionState) + if (_evictionState is { } evictionState) { // Backwards compatibility if (evictionState.LimitSizeTo is 0) @@ -252,10 +252,10 @@ private void HandleIncomingItem( } // Perform processing for expiration behavior, if applicable - if (_expirationState is ExpirationState expirationState) + if (_expirationState is { } expirationState) { var expireAfter = expirationState.ExpireAfter.Invoke(item); - if (expireAfter is TimeSpan resolvedExpireAfter) + if (expireAfter is { } resolvedExpireAfter) { // Truncate to milliseconds to promote batching expirations together. var expireAtTicks = now.UtcTicks + resolvedExpireAfter.Ticks; @@ -371,7 +371,7 @@ private void OnExpirationQueueChanged() { // If there's already a scheduled operation, and it doesn't match the current next-item-to-expire time, wipe it out and re-schedule it. var nextExpireAt = expirationState.Queue[0].ExpireAt; - if (_scheduledExpiration is ScheduledExpiration scheduledExpiration) + if (_scheduledExpiration is { } scheduledExpiration) { if (scheduledExpiration.DueTime != nextExpireAt) { diff --git a/src/DynamicData/List/Internal/Transformer.cs b/src/DynamicData/List/Internal/Transformer.cs index 240082477..d43c18d3b 100644 --- a/src/DynamicData/List/Internal/Transformer.cs +++ b/src/DynamicData/List/Internal/Transformer.cs @@ -83,7 +83,7 @@ private void Transform(ChangeAwareList transformed, IC var current = transformed.FirstOrDefault(x => x.Source.Equals(change.Current)); index = current switch { - TransformedItemContainer tic when transformed.IndexOf(tic) is int i and (>= 0) => i, + { } tic when transformed.IndexOf(tic) is int i and (>= 0) => i, _ => throw new UnspecifiedIndexException($"Cannot find index of {change.Current}"), }; }