Skip to content

Commit 1e211f5

Browse files
Inspection fixes and nullable improvements.
1 parent 49e7fe5 commit 1e211f5

23 files changed

+145
-182
lines changed

source/ConcurrentHashSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Open.Collections
55
{
66
public class ConcurrentHashSet<T> : DictionaryToHashSetWrapper<T>
77
{
8-
public ConcurrentHashSet(IEnumerable<T> intialValues = null)
8+
public ConcurrentHashSet(IEnumerable<T>? intialValues = null)
99
: base(new ConcurrentDictionary<T, bool>())
1010
{
1111
if (intialValues != null)

source/DictionaryToHashSetWrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void CopyTo(T[] array, int arrayIndex)
5050
=> InternalSource.Keys.CopyTo(array, arrayIndex);
5151

5252
public HashSet<T> ToHashSet()
53-
=> new HashSet<T>(InternalSource.Keys);
53+
=> new(InternalSource.Keys);
5454

5555
public IEnumerator<T> GetEnumerator()
5656
=> InternalSource.Keys.GetEnumerator();

source/Extensions.ByteArray.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static partial class Extensions
1010
/// </summary>
1111
/// <param name="value">The string value.</param>
1212
/// <param name="encoding">Default is UTF8.</param>
13-
public static byte[] ToByteArray(this string value, Encoding encoding = null)
13+
public static byte[] ToByteArray(this string value, Encoding? encoding = null)
1414
{
1515
if (value is null)
1616
throw new NullReferenceException();
@@ -23,7 +23,7 @@ public static byte[] ToByteArray(this string value, Encoding encoding = null)
2323
/// </summary>
2424
/// <param name="value">The string value.</param>
2525
/// <param name="encoding">Default is UTF8.</param>
26-
public static sbyte[] ToSbyteArray(this string value, Encoding encoding = null)
26+
public static sbyte[] ToSbyteArray(this string value, Encoding? encoding = null)
2727
{
2828
if (value is null)
2929
throw new NullReferenceException();

source/Extensions.ConcurrentBag.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static partial class Extensions
1212

1313
public static IEnumerable<T> TryTakeWhile<T>(this ConcurrentBag<T> target, Func<ConcurrentBag<T>, bool> predicate)
1414
{
15-
if (target is null) throw new NullReferenceException();
15+
if (target is null) throw new ArgumentNullException(nameof(target));
1616

1717
while (!target.IsEmpty && predicate(target) && target.TryTake(out var value))
1818
{
@@ -22,14 +22,14 @@ public static IEnumerable<T> TryTakeWhile<T>(this ConcurrentBag<T> target, Func<
2222

2323
public static IEnumerable<T> TryTakeWhile<T>(this ConcurrentBag<T> target, Func<bool> predicate)
2424
{
25-
if (target is null) throw new NullReferenceException();
25+
if (target is null) throw new ArgumentNullException(nameof(target));
2626

2727
return TryTakeWhile(target, t => predicate());
2828
}
2929

3030
public static void Trim<T>(this ConcurrentBag<T> target, int maxSize)
3131
{
32-
if (target is null) throw new NullReferenceException();
32+
if (target is null) throw new ArgumentNullException(nameof(target));
3333

3434
foreach (var _ in TryTakeWhile(target, t => t.Count > maxSize))
3535
{
@@ -38,7 +38,7 @@ public static void Trim<T>(this ConcurrentBag<T> target, int maxSize)
3838

3939
public static Task TrimAsync<T>(this ConcurrentBag<T> target, int maxSize, Action<T> handler)
4040
{
41-
if (target is null) throw new NullReferenceException();
41+
if (target is null) throw new ArgumentNullException(nameof(target));
4242

4343
return Task.WhenAll(
4444
TryTakeWhile(target, t => t.Count > maxSize)

source/Extensions.ConcurrentDictionary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static partial class Extensions
1010
/// </summary>
1111
public static bool TryRemove<TKey, T>(this ConcurrentDictionary<TKey, T> target, TKey key)
1212
{
13-
if (target is null) throw new NullReferenceException();
13+
if (target is null) throw new ArgumentNullException(nameof(target));
1414

1515
return target.TryRemove(key, out _);
1616
}

source/Extensions.Generic.Synchronized.cs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public static bool TryGetValueSynchronized<TKey, TValue>(
2626
this IDictionary<TKey, TValue> target,
2727
TKey key, out TValue value)
2828
{
29-
if (target is null) throw new NullReferenceException();
29+
if (target is null) throw new ArgumentNullException(nameof(target));
3030
if (key is null) throw new ArgumentNullException(nameof(key));
3131
Contract.EndContractBlock();
3232

33-
TValue result = default;
33+
TValue result = default!;
3434
var success = ThreadSafety.SynchronizeRead(target, key, () =>
3535
ThreadSafety.SynchronizeRead(target, () =>
3636
target.TryGetValue(key, out result)
@@ -47,7 +47,7 @@ public static bool TryGetValueSynchronized<TKey, TValue>(
4747
/// </summary>
4848
public static TValue GetValueSynchronized<TKey, TValue>(this IDictionary<TKey, TValue> target, TKey key, bool throwIfNotExists = true)
4949
{
50-
if (target is null) throw new NullReferenceException();
50+
if (target is null) throw new ArgumentNullException(nameof(target));
5151
if (key is null) throw new ArgumentNullException(nameof(key));
5252
Contract.EndContractBlock();
5353

@@ -56,7 +56,7 @@ public static TValue GetValueSynchronized<TKey, TValue>(this IDictionary<TKey, T
5656
if (!exists && throwIfNotExists)
5757
throw new KeyNotFoundException(key.ToString());
5858

59-
return exists ? value : default;
59+
return exists ? value : default!;
6060
}
6161

6262

@@ -65,7 +65,8 @@ public static TValue GetValueSynchronized<TKey, TValue>(this IDictionary<TKey, T
6565
/// </summary>
6666
public static void RegisterSynchronized<T>(this ICollection<T> target, T value)
6767
{
68-
if (target is null) throw new NullReferenceException();
68+
if (target is null) throw new ArgumentNullException(nameof(target));
69+
if (value is null) throw new ArgumentNullException(nameof(value));
6970
Contract.EndContractBlock();
7071

7172
ThreadSafety.SynchronizeReadWriteKeyAndObject(target, value,
@@ -82,12 +83,12 @@ public static void RegisterSynchronized<T>(this ICollection<T> target, T value)
8283
public static T AddOrUpdateSynchronized<TKey, T>(this IDictionary<TKey, T> target, TKey key, T value,
8384
Func<TKey, T, T> updateValueFactory)
8485
{
85-
if (target is null) throw new NullReferenceException();
86+
if (target is null) throw new ArgumentNullException(nameof(target));
8687
if (key is null) throw new ArgumentNullException(nameof(key));
8788
if (updateValueFactory is null) throw new ArgumentNullException(nameof(updateValueFactory));
8889
Contract.EndContractBlock();
8990

90-
T valueUsed = default;
91+
T valueUsed = default!;
9192

9293
// First we get a lock on the key action which should prevent the individual action from changing..
9394
ThreadSafety.SynchronizeWrite(target, key, () =>
@@ -120,13 +121,13 @@ public static T AddOrUpdateSynchronized<TKey, T>(this IDictionary<TKey, T> targe
120121
Func<TKey, T> newValueFactory,
121122
Func<TKey, T, T> updateValueFactory)
122123
{
123-
if (target is null) throw new NullReferenceException();
124+
if (target is null) throw new ArgumentNullException(nameof(target));
124125
if (key is null) throw new ArgumentNullException(nameof(key));
125126
if (newValueFactory is null) throw new ArgumentNullException(nameof(newValueFactory));
126127
if (updateValueFactory is null) throw new ArgumentNullException(nameof(updateValueFactory));
127128
Contract.EndContractBlock();
128129

129-
T valueUsed = default;
130+
T valueUsed = default!;
130131

131132
// First we get a lock on the key action which should prevent the individual action from changing..
132133
ThreadSafety.SynchronizeWrite(target, key, () =>
@@ -141,7 +142,7 @@ public static T AddOrUpdateSynchronized<TKey, T>(this IDictionary<TKey, T> targe
141142
() =>
142143
valueUsed = target.AddOrUpdate(key,
143144
newValueFactory,
144-
(k, o) => o.Equals(old) && k.Equals(key) ? updateValue : updateValueFactory(k, o)
145+
(k, o) => k!.Equals(key) && (o?.Equals(old) ?? old==null) ? updateValue : updateValueFactory(k, o)
145146
));
146147
}
147148
else
@@ -152,7 +153,7 @@ public static T AddOrUpdateSynchronized<TKey, T>(this IDictionary<TKey, T> targe
152153
ThreadSafety.SynchronizeWrite(target,
153154
() =>
154155
valueUsed = target.AddOrUpdate(key,
155-
k => k.Equals(key) ? value : newValueFactory(k),
156+
k => k!.Equals(key) ? value : newValueFactory(k),
156157
updateValueFactory
157158
));
158159
}
@@ -163,7 +164,7 @@ public static T AddOrUpdateSynchronized<TKey, T>(this IDictionary<TKey, T> targe
163164

164165
public static void AddSynchronized<T>(this ICollection<T> target, T value)
165166
{
166-
if (target is null) throw new NullReferenceException();
167+
if (target is null) throw new ArgumentNullException(nameof(target));
167168
ThreadSafety.SynchronizeWrite(target, () => target.Add(value));
168169
}
169170

@@ -185,7 +186,7 @@ public static void AddToSynchronized<TKey, TValue>(this IDictionary<TKey, IList<
185186
/// </summary>
186187
public static void EnsureDefaultSynchronized<TKey, T>(this IDictionary<TKey, T> target, TKey key, T defaultValue)
187188
{
188-
if (target is null) throw new NullReferenceException();
189+
if (target is null) throw new ArgumentNullException(nameof(target));
189190
if (key is null) throw new ArgumentNullException(nameof(key));
190191
Contract.EndContractBlock();
191192

@@ -200,7 +201,7 @@ public static void EnsureDefaultSynchronized<TKey, T>(this IDictionary<TKey, T>
200201
public static void EnsureDefaultSynchronized<TKey, T>(this IDictionary<TKey, T> target, TKey key,
201202
Func<TKey, T> defaultValueFactory)
202203
{
203-
if (target is null) throw new NullReferenceException();
204+
if (target is null) throw new ArgumentNullException(nameof(target));
204205
if (key is null) throw new ArgumentNullException(nameof(key));
205206
if (defaultValueFactory is null) throw new ArgumentNullException(nameof(defaultValueFactory));
206207
Contract.EndContractBlock();
@@ -223,12 +224,12 @@ public static T GetOrAddSynchronized<TKey, T>(
223224
int millisecondsTimeout = SYNC_TIMEOUT_DEFAULT_MILLISECONDS,
224225
bool throwsOnTimeout = true)
225226
{
226-
if (target is null) throw new NullReferenceException();
227+
if (target is null) throw new ArgumentNullException(nameof(target));
227228
if (key is null) throw new ArgumentNullException(nameof(key));
228229
ValidateMillisecondsTimeout(millisecondsTimeout);
229230
Contract.EndContractBlock();
230231

231-
T result = default;
232+
T result = default!;
232233
bool condition(LockType lockType) => !target.TryGetValue(key, out result);
233234
void render()
234235
{
@@ -252,13 +253,13 @@ public static T GetOrAddSynchronized<TKey, T>(
252253
Func<TKey, T> valueFactory,
253254
int millisecondsTimeout = SYNC_TIMEOUT_DEFAULT_MILLISECONDS)
254255
{
255-
if (target is null) throw new NullReferenceException();
256+
if (target is null) throw new ArgumentNullException(nameof(target));
256257
if (key is null) throw new ArgumentNullException(nameof(key));
257258
if (valueFactory is null) throw new ArgumentNullException(nameof(valueFactory));
258259
ValidateMillisecondsTimeout(millisecondsTimeout);
259260
Contract.EndContractBlock();
260261

261-
T result = default;
262+
T result = default!;
262263
// Note, the following sync read is on the TARGET and not the key. See below.
263264
bool condition(LockType lockType) => !ThreadSafety.SynchronizeRead(target, () => target.TryGetValue(key, out result));
264265

@@ -294,7 +295,7 @@ public static bool TryAddSynchronized<TKey, T>(
294295
T value,
295296
int millisecondsTimeout = SYNC_TIMEOUT_DEFAULT_MILLISECONDS)
296297
{
297-
if (target is null) throw new NullReferenceException();
298+
if (target is null) throw new ArgumentNullException(nameof(target));
298299
if (key is null) throw new ArgumentNullException(nameof(key));
299300
Contract.EndContractBlock();
300301

@@ -323,7 +324,7 @@ public static bool TryAddSynchronized<TKey, T>(
323324
Func<T> valueFactory,
324325
int millisecondsTimeout = SYNC_TIMEOUT_DEFAULT_MILLISECONDS)
325326
{
326-
if (target is null) throw new NullReferenceException();
327+
if (target is null) throw new ArgumentNullException(nameof(target));
327328
if (key is null) throw new ArgumentNullException(nameof(key));
328329
Contract.EndContractBlock();
329330

@@ -351,7 +352,7 @@ public static bool TryRemoveSynchronized<TKey, T>(
351352
TKey key,
352353
int millisecondsTimeout = SYNC_TIMEOUT_DEFAULT_MILLISECONDS)
353354
{
354-
if (target is null) throw new NullReferenceException();
355+
if (target is null) throw new ArgumentNullException(nameof(target));
355356
if (key is null) throw new ArgumentNullException(nameof(key));
356357
Contract.EndContractBlock();
357358

@@ -376,14 +377,14 @@ public static bool TryRemoveSynchronized<TKey, T>(
376377
out T value,
377378
int millisecondsTimeout = SYNC_TIMEOUT_DEFAULT_MILLISECONDS)
378379
{
379-
if (target is null) throw new NullReferenceException();
380+
if (target is null) throw new ArgumentNullException(nameof(target));
380381
if (key is null) throw new ArgumentNullException(nameof(key));
381382
Contract.EndContractBlock();
382383

383-
value = default;
384+
value = default!;
384385
var removed = false;
385386
ThreadSafety.SynchronizeReadWriteKeyAndObject(
386-
target, key, ref value,
387+
target, key, ref value!,
387388
lockType => target.ContainsKey(key),
388389
() =>
389390
{

0 commit comments

Comments
 (0)