Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use null check pattern #911

Merged
merged 2 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/DynamicData/Cache/Internal/ExpireAfter.ForSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static IObservable<IEnumerable<KeyValuePair<TKey, TObject>>> Create(
source.ThrowArgumentNullExceptionIfNull(nameof(source));
timeSelector.ThrowArgumentNullExceptionIfNull(nameof(timeSelector));

return Observable.Create<IEnumerable<KeyValuePair<TKey, TObject>>>(observer => (pollingInterval is TimeSpan pollingIntervalValue)
return Observable.Create<IEnumerable<KeyValuePair<TKey, TObject>>>(observer => (pollingInterval is { } pollingIntervalValue)
? new PollingSubscription(
observer: observer,
pollingInterval: pollingIntervalValue,
Expand Down Expand Up @@ -137,7 +137,7 @@ private void OnEditingSource(ISourceUpdater<TObject, TKey> 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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -251,7 +251,7 @@ private void OnSourceNext(IChangeSet<TObject, TKey> changes)
{
case ChangeReason.Add:
{
if (_timeSelector.Invoke(change.Current) is TimeSpan expireAfter)
if (_timeSelector.Invoke(change.Current) is { } expireAfter)
{
haveExpirationsChanged |= TrySetExpiration(
key: change.Key,
Expand All @@ -267,7 +267,7 @@ private void OnSourceNext(IChangeSet<TObject, TKey> changes)

case ChangeReason.Update:
{
if (_timeSelector.Invoke(change.Current) is TimeSpan expireAfter)
if (_timeSelector.Invoke(change.Current) is { } expireAfter)
{
haveExpirationsChanged = TrySetExpiration(
key: change.Key,
Expand Down
12 changes: 6 additions & 6 deletions src/DynamicData/Cache/Internal/ExpireAfter.ForStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static IObservable<IChangeSet<TObject, TKey>> Create(
source.ThrowArgumentNullExceptionIfNull(nameof(source));
timeSelector.ThrowArgumentNullExceptionIfNull(nameof(timeSelector));

return Observable.Create<IChangeSet<TObject, TKey>>(observer => (pollingInterval is TimeSpan pollingIntervalValue)
return Observable.Create<IChangeSet<TObject, TKey>>(observer => (pollingInterval is { } pollingIntervalValue)
? new PollingSubscription(
observer: observer,
pollingInterval: pollingIntervalValue,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -234,7 +234,7 @@ private void OnSourceNext(IChangeSet<TObject, TKey> upstreamChanges)
{
case ChangeReason.Add:
{
if (_timeSelector.Invoke(change.Current) is TimeSpan expireAfter)
if (_timeSelector.Invoke(change.Current) is { } expireAfter)
{
haveExpirationsChanged |= TrySetExpiration(
key: change.Key,
Expand All @@ -257,7 +257,7 @@ private void OnSourceNext(IChangeSet<TObject, TKey> upstreamChanges)

case ChangeReason.Update:
{
if (_timeSelector.Invoke(change.Current) is TimeSpan expireAfter)
if (_timeSelector.Invoke(change.Current) is { } expireAfter)
{
haveExpirationsChanged = TrySetExpiration(
key: change.Key,
Expand Down
2 changes: 1 addition & 1 deletion src/DynamicData/Cache/Internal/FilterImmutable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public IObservable<IChangeSet<TObject, TKey>> 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(
Expand Down
2 changes: 1 addition & 1 deletion src/DynamicData/Cache/Internal/OfType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public IObservable<IChangeSet<TDestination, TKey>> Run() =>
_ => default,
};

if (transformedChange is Change<TDestination, TKey> 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);
Expand Down
10 changes: 5 additions & 5 deletions src/DynamicData/Cache/Internal/ToObservableChangeSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public Subscription(
_ => 0
};

var changeSet = new ChangeSet<TObject, TKey>(capacity: (_evictionState is EvictionState evictionState)
var changeSet = new ChangeSet<TObject, TKey>(capacity: (_evictionState is { } evictionState)
? Math.Max(itemCount + evictionState.Queue.Count - evictionState.LimitSizeTo, 0)
: itemCount);

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/DynamicData/Cache/Internal/ToObservableOptional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public IObservable<Optional<TObject>> Run() => Observable.Create<Optional<TObjec

private bool ShouldEmitChange(Change<TObject, TKey> 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,
Expand Down
12 changes: 6 additions & 6 deletions src/DynamicData/List/Internal/ExpireAfter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static IObservable<IEnumerable<T>> Create(
source.ThrowArgumentNullExceptionIfNull(nameof(source));
timeSelector.ThrowArgumentNullExceptionIfNull(nameof(timeSelector));

return Observable.Create<IEnumerable<T>>(observer => (pollingInterval is TimeSpan pollingIntervalValue)
return Observable.Create<IEnumerable<T>>(observer => (pollingInterval is { } pollingIntervalValue)
? new PollingSubscription(
observer: observer,
pollingInterval: pollingIntervalValue,
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -134,7 +134,7 @@ private void OnEditingSource(IExtendedList<T> 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;
Expand All @@ -148,7 +148,7 @@ private void OnEditingSource(IExtendedList<T> 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);

Expand Down Expand Up @@ -187,11 +187,11 @@ private void OnEditingSource(IExtendedList<T> 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()
Expand Down
10 changes: 5 additions & 5 deletions src/DynamicData/List/Internal/ToObservableChangeSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public Subscription(
_ => 0
};

var changeSet = new ChangeSet<TObject>(capacity: (_evictionState is EvictionState evictionState)
var changeSet = new ChangeSet<TObject>(capacity: (_evictionState is { } evictionState)
? Math.Max(itemCount + evictionState.Queue.Count - evictionState.LimitSizeTo, 0)
: itemCount);

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/DynamicData/List/Internal/Transformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void Transform(ChangeAwareList<TransformedItemContainer> 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}"),
};
}
Expand Down