Skip to content

Commit

Permalink
Update remaining ArgumentNullException to use extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman committed Dec 5, 2023
1 parent d4b494d commit 6395e7d
Show file tree
Hide file tree
Showing 44 changed files with 482 additions and 2,046 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,8 @@ dotnet_diagnostic.RCS1190.severity=error
dotnet_diagnostic.RCS1195.severity=error
dotnet_diagnostic.RCS1214.severity=error

dotnet_diagnostic.IDE1006.severity=none

# C++ Files
[*.{cpp,h,in}]
curly_bracket_next_line = true
Expand Down
693 changes: 117 additions & 576 deletions src/DynamicData/Cache/ObservableCacheEx.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/DynamicData/Cache/PageResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override bool Equals(object? obj)
return false;
}

if (!(obj is IPageResponse pageResponse))
if (obj is not IPageResponse pageResponse)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/DynamicData/Cache/PagedChangeSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal sealed class PagedChangeSet<TObject, TKey> : ChangeSet<TObject, TKey>,
where TObject : notnull
where TKey : notnull
{
public static readonly new IPagedChangeSet<TObject, TKey> Empty = new PagedChangeSet<TObject, TKey>();
public static new readonly IPagedChangeSet<TObject, TKey> Empty = new PagedChangeSet<TObject, TKey>();

public PagedChangeSet(IKeyValueCollection<TObject, TKey> sortedItems, IEnumerable<Change<TObject, TKey>> updates, IPageResponse response)
: base(updates)
Expand Down
2 changes: 1 addition & 1 deletion src/DynamicData/Cache/SortedChangeSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class SortedChangeSet<TObject, TKey> : ChangeSet<TObject, TKey>, ISorte
where TObject : notnull
where TKey : notnull
{
public static readonly new ISortedChangeSet<TObject, TKey> Empty = new SortedChangeSet<TObject, TKey>();
public static new readonly ISortedChangeSet<TObject, TKey> Empty = new SortedChangeSet<TObject, TKey>();

public SortedChangeSet(IKeyValueCollection<TObject, TKey> sortedItems, IEnumerable<Change<TObject, TKey>> updates)
: base(updates) => SortedItems = sortedItems;
Expand Down
10 changes: 1 addition & 9 deletions src/DynamicData/Cache/SourceCacheEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,5 @@ public static class SourceCacheEx
public static IObservable<IChangeSet<TDestination, TKey>> Cast<TSource, TKey, TDestination>(this IObservableCache<TSource, TKey> source, Func<TSource, TDestination> converter)
where TSource : notnull
where TKey : notnull
where TDestination : notnull
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}

return source.Connect().Cast(converter);
}
where TDestination : notnull => source is null ? throw new ArgumentNullException(nameof(source)) : source.Connect().Cast(converter);
}
2 changes: 1 addition & 1 deletion src/DynamicData/Cache/VirtualChangeSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal sealed class VirtualChangeSet<TObject, TKey> : ChangeSet<TObject, TKey>
where TObject : notnull
where TKey : notnull
{
public static readonly new IVirtualChangeSet<TObject, TKey> Empty = new VirtualChangeSet<TObject, TKey>();
public static new readonly IVirtualChangeSet<TObject, TKey> Empty = new VirtualChangeSet<TObject, TKey>();

public VirtualChangeSet(IEnumerable<Change<TObject, TKey>> items, IKeyValueCollection<TObject, TKey> sortedItems, IVirtualResponse response)
: base(items)
Expand Down
10 changes: 2 additions & 8 deletions src/DynamicData/Diagnostics/DiagnosticOperators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ public static IObservable<ChangeSummary> CollectUpdateStats<TSource, TKey>(this
where TSource : notnull
where TKey : notnull
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
source.ThrowArgumentNullExceptionIfNull(nameof(source));

return source.Scan(
ChangeSummary.Empty,
Expand Down Expand Up @@ -56,10 +53,7 @@ public static IObservable<ChangeSummary> CollectUpdateStats<TSource, TKey>(this
public static IObservable<ChangeSummary> CollectUpdateStats<TSource>(this IObservable<IChangeSet<TSource>> source)
where TSource : notnull
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
source.ThrowArgumentNullExceptionIfNull(nameof(source));

return source.Scan(
ChangeSummary.Empty,
Expand Down
26 changes: 3 additions & 23 deletions src/DynamicData/EnumerableEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,8 @@ public static IObservable<IChangeSet<TObject, TKey>> AsObservableChangeSet<TObje
where TObject : notnull
where TKey : notnull
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);

ArgumentNullException.ThrowIfNull(keySelector);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}

if (keySelector is null)
{
throw new ArgumentNullException(nameof(keySelector));
}
#endif
source.ThrowArgumentNullExceptionIfNull(nameof(source));
keySelector.ThrowArgumentNullExceptionIfNull(nameof(keySelector));

return Observable.Create<IChangeSet<TObject, TKey>>(
obs =>
Expand Down Expand Up @@ -73,14 +60,7 @@ public static IObservable<IChangeSet<TObject, TKey>> AsObservableChangeSet<TObje
public static IObservable<IChangeSet<TObject>> AsObservableChangeSet<TObject>(this IEnumerable<TObject> source, bool completable = false)
where TObject : notnull
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
source.ThrowArgumentNullExceptionIfNull(nameof(source));

return Observable.Create<IChangeSet<TObject>>(
obs =>
Expand Down
5 changes: 1 addition & 4 deletions src/DynamicData/Experimental/ExperimentalEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ public static IWatcher<TObject, TKey> AsWatcher<TObject, TKey>(this IObservable<
where TObject : notnull
where TKey : notnull
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
source.ThrowArgumentNullExceptionIfNull(nameof(source));

return new Watcher<TObject, TKey>(source, scheduler ?? Scheduler.Default);
}
Expand Down
38 changes: 7 additions & 31 deletions src/DynamicData/Kernel/EnumerableEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ public static class EnumerableEx
/// <returns>The array of items.</returns>
public static T[] AsArray<T>(this IEnumerable<T> source)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
source.ThrowArgumentNullExceptionIfNull(nameof(source));

return source as T[] ?? source.ToArray();
}
Expand All @@ -33,10 +30,7 @@ public static T[] AsArray<T>(this IEnumerable<T> source)
/// <returns>The list.</returns>
public static List<T> AsList<T>(this IEnumerable<T> source)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
source.ThrowArgumentNullExceptionIfNull(nameof(source));

return source as List<T> ?? source.ToList();
}
Expand All @@ -51,15 +45,8 @@ public static List<T> AsList<T>(this IEnumerable<T> source)
/// <returns>The enumerable of items.</returns>
public static IEnumerable<T> Duplicates<T, TValue>(this IEnumerable<T> source, Func<T, TValue> valueSelector)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}

if (valueSelector is null)
{
throw new ArgumentNullException(nameof(valueSelector));
}
source.ThrowArgumentNullExceptionIfNull(nameof(source));
valueSelector.ThrowArgumentNullExceptionIfNull(nameof(valueSelector));

return source.GroupBy(valueSelector).Where(group => group.Count() > 1).SelectMany(t => t);
}
Expand All @@ -86,20 +73,9 @@ public static IEnumerable<T> Duplicates<T, TValue>(this IEnumerable<T> source, F
/// <returns>A result as specified by the result selector.</returns>
public static IEnumerable<TResult> IndexOfMany<TObject, TResult>(this IEnumerable<TObject> source, IEnumerable<TObject> itemsToFind, Func<TObject, int, TResult> resultSelector)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}

if (itemsToFind is null)
{
throw new ArgumentNullException(nameof(itemsToFind));
}

if (resultSelector is null)
{
throw new ArgumentNullException(nameof(resultSelector));
}
source.ThrowArgumentNullExceptionIfNull(nameof(source));
itemsToFind.ThrowArgumentNullExceptionIfNull(nameof(itemsToFind));
resultSelector.ThrowArgumentNullExceptionIfNull(nameof(resultSelector));

var indexed = source.Select((element, index) => new { Element = element, Index = index });
return itemsToFind.Join(indexed, left => left, right => right.Element, (_, right) => right).Select(x => resultSelector(x.Element, x.Index));
Expand Down
5 changes: 2 additions & 3 deletions src/DynamicData/Kernel/EnumeratorIList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
// Lifted from here /~https://github.com/benaadams/Ben.Enumerable. Many thanks to the genius of the man.
namespace DynamicData.Kernel;

[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Same class name, different generics.")]
internal struct EnumeratorIList<T>(IList<T> list) : IEnumerator<T>
{
private int _index = -1;

public T Current => list[_index];
public readonly T Current => list[_index];

object? IEnumerator.Current => Current;
readonly object? IEnumerator.Current => Current;

public bool MoveNext()
{
Expand Down
5 changes: 2 additions & 3 deletions src/DynamicData/Kernel/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Roland Pheasant licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

#pragma warning disable 1591

namespace DynamicData.Kernel;

/// <summary>
Expand All @@ -17,6 +15,7 @@ namespace DynamicData.Kernel;
/// <param name="exception">The exception that caused the error.</param>
/// <param name="value">The value for the error.</param>
/// <param name="key">The key for the error.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1716:Identifiers should not match keywords", Justification = "By Design.")]
public sealed class Error<TObject, TKey>(Exception? exception, TObject value, TKey key) : IKeyValue<TObject, TKey>, IEquatable<Error<TObject, TKey>>
where TKey : notnull
{
Expand Down Expand Up @@ -78,7 +77,7 @@ public override int GetHashCode()
{
var hashCode = EqualityComparer<TKey>.Default.GetHashCode(Key);
hashCode = (hashCode * 397) ^ (Value is null ? 0 : EqualityComparer<TObject>.Default.GetHashCode(Value));
hashCode = (hashCode * 397) ^ (Exception is not null ? Exception.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Exception?.GetHashCode() ?? 0);
return hashCode;
}
}
Expand Down
12 changes: 2 additions & 10 deletions src/DynamicData/Kernel/InternalEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ public static IDisposable ScheduleRecurringAction(this IScheduler scheduler, Tim
/// <returns>A disposable that will stop the schedule.</returns>
public static IDisposable ScheduleRecurringAction(this IScheduler scheduler, Func<TimeSpan> interval, Action action)
{
if (interval is null)
{
throw new ArgumentNullException(nameof(interval));
}
interval.ThrowArgumentNullExceptionIfNull(nameof(interval));

return scheduler.Schedule(
interval(),
Expand All @@ -95,12 +92,7 @@ public static IDisposable ScheduleRecurringAction(this IScheduler scheduler, Fun

internal static void OnNext(this ISubject<Unit> source) => source.OnNext(Unit.Default);

internal static void Swap<TSwap>(ref TSwap t1, ref TSwap t2)
{
var temp = t1;
t1 = t2;
t2 = temp;
}
internal static void Swap<TSwap>(ref TSwap t1, ref TSwap t2) => (t2, t1) = (t1, t2);

internal static IObservable<Unit> ToUnit<T>(this IObservable<T> source) => source.Select(_ => Unit.Default);

Expand Down
5 changes: 1 addition & 4 deletions src/DynamicData/Kernel/OptionElse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ public sealed class OptionElse
/// <exception cref="ArgumentNullException">action.</exception>
public void Else(Action action)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
action.ThrowArgumentNullExceptionIfNull(nameof(action));

if (_shouldRunAction)
{
Expand Down
Loading

0 comments on commit 6395e7d

Please sign in to comment.