Skip to content

Commit

Permalink
update null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
tibel committed Mar 7, 2021
1 parent 7fdfd8e commit c29f7d8
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public CachingDynamicDelegateBuilderDecorator(IDynamicDelegateBuilder builder)
public Func<object, object[], object> BuildDynamic(MethodInfo method)
{
var action = _cache.GetValueOrDefault(method);
if (action != null) return action;
if (action is object) return action;
action = _builder.BuildDynamic(method);
_cache.AddOrUpdate(method, action);
return action;
Expand Down
12 changes: 6 additions & 6 deletions src/Weakly/Builders/CachingOpenActionBuilderDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public CachingOpenActionBuilderDecorator(IOpenActionBuilder builder)
public Action<object> BuildAction(MethodInfo method)
{
var action = (Action<object>) _cache.GetValueOrDefault(method);
if (action != null) return action;
if (action is object) return action;
action = _builder.BuildAction(method);
_cache.AddOrUpdate(method, action);
return action;
Expand All @@ -47,7 +47,7 @@ public Action<object> BuildAction(MethodInfo method)
public Action<object, T> BuildAction<T>(MethodInfo method)
{
var action = (Action<object, T>) _cache.GetValueOrDefault(method);
if (action != null) return action;
if (action is object) return action;
action = _builder.BuildAction<T>(method);
_cache.AddOrUpdate(method, action);
return action;
Expand All @@ -65,7 +65,7 @@ public Action<object, T> BuildAction<T>(MethodInfo method)
public Action<object, T1, T2> BuildAction<T1, T2>(MethodInfo method)
{
var action = (Action<object, T1, T2>) _cache.GetValueOrDefault(method);
if (action != null) return action;
if (action is object) return action;
action = _builder.BuildAction<T1, T2>(method);
_cache.AddOrUpdate(method, action);
return action;
Expand All @@ -84,7 +84,7 @@ public Action<object, T1, T2> BuildAction<T1, T2>(MethodInfo method)
public Action<object, T1, T2, T3> BuildAction<T1, T2, T3>(MethodInfo method)
{
var action = (Action<object, T1, T2, T3>) _cache.GetValueOrDefault(method);
if (action != null) return action;
if (action is object) return action;
action = _builder.BuildAction<T1, T2, T3>(method);
_cache.AddOrUpdate(method, action);
return action;
Expand All @@ -104,7 +104,7 @@ public Action<object, T1, T2, T3> BuildAction<T1, T2, T3>(MethodInfo method)
public Action<object, T1, T2, T3, T4> BuildAction<T1, T2, T3, T4>(MethodInfo method)
{
var action = (Action<object, T1, T2, T3, T4>) _cache.GetValueOrDefault(method);
if (action != null) return action;
if (action is object) return action;
action = _builder.BuildAction<T1, T2, T3, T4>(method);
_cache.AddOrUpdate(method, action);
return action;
Expand All @@ -125,7 +125,7 @@ public Action<object, T1, T2, T3, T4> BuildAction<T1, T2, T3, T4>(MethodInfo met
public Action<object, T1, T2, T3, T4, T5> BuildAction<T1, T2, T3, T4, T5>(MethodInfo method)
{
var action = (Action<object, T1, T2, T3, T4, T5>) _cache.GetValueOrDefault(method);
if (action != null) return action;
if (action is object) return action;
action = _builder.BuildAction<T1, T2, T3, T4, T5>(method);
_cache.AddOrUpdate(method, action);
return action;
Expand Down
12 changes: 6 additions & 6 deletions src/Weakly/Builders/CachingOpenFuncBuilderDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CachingOpenFuncBuilderDecorator(IOpenFuncBuilder builder)
public Func<object, TResult> BuildFunc<TResult>(MethodInfo method)
{
var func = (Func<object, TResult>) _cache.GetValueOrDefault(method);
if (func != null) return func;
if (func is object) return func;
func = _builder.BuildFunc<TResult>(method);
_cache.AddOrUpdate(method, func);
return func;
Expand All @@ -49,7 +49,7 @@ public Func<object, TResult> BuildFunc<TResult>(MethodInfo method)
public Func<object, T, TResult> BuildFunc<T, TResult>(MethodInfo method)
{
var func = (Func<object, T, TResult>) _cache.GetValueOrDefault(method);
if (func != null) return func;
if (func is object) return func;
func = _builder.BuildFunc<T, TResult>(method);
_cache.AddOrUpdate(method, func);
return func;
Expand All @@ -68,7 +68,7 @@ public Func<object, T, TResult> BuildFunc<T, TResult>(MethodInfo method)
public Func<object, T1, T2, TResult> BuildFunc<T1, T2, TResult>(MethodInfo method)
{
var func = (Func<object, T1, T2, TResult>) _cache.GetValueOrDefault(method);
if (func != null) return func;
if (func is object) return func;
func = _builder.BuildFunc<T1, T2, TResult>(method);
_cache.AddOrUpdate(method, func);
return func;
Expand All @@ -88,7 +88,7 @@ public Func<object, T1, T2, TResult> BuildFunc<T1, T2, TResult>(MethodInfo metho
public Func<object, T1, T2, T3, TResult> BuildFunc<T1, T2, T3, TResult>(MethodInfo method)
{
var func = (Func<object, T1, T2, T3, TResult>) _cache.GetValueOrDefault(method);
if (func != null) return func;
if (func is object) return func;
func = _builder.BuildFunc<T1, T2, T3, TResult>(method);
_cache.AddOrUpdate(method, func);
return func;
Expand All @@ -109,7 +109,7 @@ public Func<object, T1, T2, T3, TResult> BuildFunc<T1, T2, T3, TResult>(MethodIn
public Func<object, T1, T2, T3, T4, TResult> BuildFunc<T1, T2, T3, T4, TResult>(MethodInfo method)
{
var func = (Func<object, T1, T2, T3, T4, TResult>) _cache.GetValueOrDefault(method);
if (func != null) return func;
if (func is object) return func;
func = _builder.BuildFunc<T1, T2, T3, T4, TResult>(method);
_cache.AddOrUpdate(method, func);
return func;
Expand All @@ -131,7 +131,7 @@ public Func<object, T1, T2, T3, T4, TResult> BuildFunc<T1, T2, T3, T4, TResult>(
public Func<object, T1, T2, T3, T4, T5, TResult> BuildFunc<T1, T2, T3, T4, T5, TResult>(MethodInfo method)
{
var func = (Func<object, T1, T2, T3, T4, T5, TResult>) _cache.GetValueOrDefault(method);
if (func != null) return func;
if (func is object) return func;
func = _builder.BuildFunc<T1, T2, T3, T4, T5, TResult>(method);
_cache.AddOrUpdate(method, func);
return func;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CachingPropertyAccessorBuilderDecorator(IPropertyAccessorBuilder builder)
public Func<object, object> BuildGetter(PropertyInfo property)
{
var action = _getterCache.GetValueOrDefault(property);
if (action != null) return action;
if (action is object) return action;
action = _builder.BuildGetter(property);
_getterCache.AddOrUpdate(property, action);
return action;
Expand All @@ -47,7 +47,7 @@ public Func<object, object> BuildGetter(PropertyInfo property)
public Action<object, object> BuildSetter(PropertyInfo property)
{
var action = _setterCache.GetValueOrDefault(property);
if (action != null) return action;
if (action is object) return action;
action = _builder.BuildSetter(property);
_setterCache.AddOrUpdate(property, action);
return action;
Expand Down
7 changes: 1 addition & 6 deletions src/Weakly/Builders/SimpleCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ namespace Weakly.Builders
{
internal sealed class SimpleCache<TKey, TValue>
{
private readonly IDictionary<TKey, TValue> _storage;

public SimpleCache()
{
_storage = new Dictionary<TKey, TValue>();
}
private readonly Dictionary<TKey, TValue> _storage = new Dictionary<TKey, TValue>();

public TValue GetValueOrDefault(TKey key)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Weakly/Collections/WeakCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public IEnumerator<T> GetEnumerator()
CleanIfNeeded();

var enumerable = _inner.Select(item => (T) item.Target)
.Where(value => value != null);
.Where(value => value is object);
return enumerable.GetEnumerator();
}

Expand Down Expand Up @@ -119,15 +119,15 @@ public void CopyTo(T[] array, int arrayIndex)
{
CleanIfNeeded();

if (array == null)
if (array is null)
throw new ArgumentNullException(nameof(array));
if (arrayIndex < 0 || arrayIndex >= array.Length)
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
if ((arrayIndex + _inner.Count) > array.Length)
throw new ArgumentException("The number of elements in the source collection is greater than the available space from arrayIndex to the end of the destination array.");

var items = _inner.Select(item => (T) item.Target)
.Where(value => value != null)
.Where(value => value is object)
.ToArray();

items.CopyTo(array, arrayIndex);
Expand Down
10 changes: 5 additions & 5 deletions src/Weakly/Collections/WeakValueDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
CleanIfNeeded();

var enumerable = _inner.Select(pair => new KeyValuePair<TKey, TValue>(pair.Key, (TValue) pair.Value.Target))
.Where(pair => pair.Value != null);
.Where(pair => pair.Value is object);
return enumerable.GetEnumerator();
}

Expand Down Expand Up @@ -156,15 +156,15 @@ void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[]
{
CleanIfNeeded();

if (array == null)
if (array is null)
throw new ArgumentNullException(nameof(array));
if (arrayIndex < 0 || arrayIndex >= array.Length)
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
if ((arrayIndex + _inner.Count) > array.Length)
throw new ArgumentException("The number of elements in the source collection is greater than the available space from arrayIndex to the end of the destination array.");

var items = _inner.Select(pair => new KeyValuePair<TKey, TValue>(pair.Key, (TValue) pair.Value.Target))
.Where(pair => pair.Value != null)
.Where(pair => pair.Value is object)
.ToArray();

items.CopyTo(array, arrayIndex);
Expand Down Expand Up @@ -256,7 +256,7 @@ public bool TryGetValue(TKey key, out TValue value)
}

value = (TValue) wr.Target;
if (value == null)
if (value is null)
{
_inner.Remove(key);
return false;
Expand Down Expand Up @@ -343,7 +343,7 @@ bool ICollection<TValue>.Contains(TValue item)

public void CopyTo(TValue[] array, int arrayIndex)
{
if (array == null)
if (array is null)
throw new ArgumentNullException(nameof(array));
if (arrayIndex < 0 || arrayIndex >= array.Length)
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
Expand Down
4 changes: 2 additions & 2 deletions src/Weakly/Delegates/DisposableAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class DisposableAction : IDisposable
/// <param name="action">The action to execute on dispose.</param>
public DisposableAction(Action action)
{
if (action == null)
if (action is null)
throw new ArgumentNullException(nameof(action));

_action = action;
Expand All @@ -26,7 +26,7 @@ public DisposableAction(Action action)
/// </summary>
public void Dispose()
{
if (_action == null) return;
if (_action is null) return;
_action();
_action = null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Weakly/Delegates/WeakAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public sealed class WeakAction<TTarget>
/// <param name="weakAction">The method represented by the delegate.</param>
public WeakAction(TTarget target, [EmptyCapture] Action<TTarget> weakAction)
{
if (target == null)
if (target is null)
throw new ArgumentNullException(nameof(target));
if (weakAction == null)
if (weakAction is null)
throw new ArgumentNullException(nameof(weakAction));

_target = new WeakReference<TTarget>(target);
Expand Down
4 changes: 2 additions & 2 deletions src/Weakly/Delegates/WeakFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public sealed class WeakFunc<TTarget, TResult>
/// <param name="weakFunc">The method represented by the delegate.</param>
public WeakFunc(TTarget target, [EmptyCapture] Func<TTarget, TResult> weakFunc)
{
if (target == null)
if (target is null)
throw new ArgumentNullException(nameof(target));
if (weakFunc == null)
if (weakFunc is null)
throw new ArgumentNullException(nameof(weakFunc));

_target = new WeakReference<TTarget>(target);
Expand Down
2 changes: 1 addition & 1 deletion src/Weakly/Events/WeakEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class WeakEventHandler
/// <param name="weakHandler">The weak handler.</param>
/// <returns>A registration object that can be used to deregister from the event.</returns>
public static IDisposable RegisterPropertyChangingWeak<TSubscriber>(this INotifyPropertyChanging source,
TSubscriber subscriber, Action<TSubscriber, object, PropertyChangingEventArgs> weakHandler)
TSubscriber subscriber, [EmptyCapture] Action<TSubscriber, object, PropertyChangingEventArgs> weakHandler)
where TSubscriber : class
{
return new WeakNotifyPropertyChangingHandler<TSubscriber>(source, subscriber, weakHandler);
Expand Down
2 changes: 1 addition & 1 deletion src/Weakly/IO/MemoryTributary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public override int Read(byte[] buffer, int offset, int count)
if (lcount > remaining)
lcount = remaining;

if (buffer == null)
if (buffer is null)
{
throw new ArgumentNullException(nameof(buffer), "Buffer cannot be null.");
}
Expand Down
10 changes: 5 additions & 5 deletions src/Weakly/Reflection/ReflectionPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed class ReflectionPath
/// <param name="path">The reflection path.</param>
public ReflectionPath(string path)
{
if (path == null)
if (path is null)
throw new ArgumentNullException(nameof(path));

_items = path.Split('.');
Expand All @@ -34,13 +34,13 @@ public ReflectionPath(string path)
/// <returns>The value.</returns>
public object GetValue(object source)
{
if (source == null)
if (source is null)
throw new ArgumentNullException(nameof(source));

var current = source;
for (var i = 0; i < _items.Length; i++)
{
if (current == null)
if (current is null)
throw new NullReferenceException(
string.Format(
"Could not get the value of the property path '{0}' because '{1}' is null on object '{2}'.",
Expand All @@ -52,10 +52,10 @@ public object GetValue(object source)

var getValue = _getters[i];
var currentType = current.GetType();
if (getValue == null || _reflectedTypes[i] != currentType)
if (getValue is null || _reflectedTypes[i] != currentType)
{
var pi = currentType.GetRuntimeProperty(_items[i]);
if (pi == null)
if (pi is null)
throw new InvalidOperationException(
string.Format(
"Could not find property '{0}' on type '{1}'.",
Expand Down
12 changes: 6 additions & 6 deletions src/Weakly/Tasks/ApmHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public static class ApmHelper
/// <returns>The asynchronous operation, to be returned by the Begin method of the APM pattern.</returns>
public static IAsyncResult ToBegin<TResult>(this Task<TResult> task, AsyncCallback callback, object state)
{
if (task == null)
if (task is null)
throw new ArgumentNullException(nameof(task));

if (task.AsyncState == state)
{
if (callback != null)
if (callback is object)
{
task.ContinueWith(
(t, cb) => ((AsyncCallback)cb)(t),
Expand Down Expand Up @@ -53,7 +53,7 @@ public static IAsyncResult ToBegin<TResult>(this Task<TResult> task, AsyncCallba
tcs.TrySetResult(t.Result);
}

if (callback != null)
if (callback is object)
{
callback(tcs.Task);
}
Expand All @@ -74,12 +74,12 @@ public static IAsyncResult ToBegin<TResult>(this Task<TResult> task, AsyncCallba
/// <returns>The asynchronous operation, to be returned by the Begin method of the APM pattern.</returns>
public static IAsyncResult ToBegin(this Task task, AsyncCallback callback, object state)
{
if (task == null)
if (task is null)
throw new ArgumentNullException(nameof(task));

if (task.AsyncState == state)
{
if (callback != null)
if (callback is object)
{
task.ContinueWith(
(t, cb) => ((AsyncCallback)cb)(t),
Expand Down Expand Up @@ -109,7 +109,7 @@ public static IAsyncResult ToBegin(this Task task, AsyncCallback callback, objec
tcs.TrySetResult(null);
}

if (callback != null)
if (callback is object)
{
callback(tcs.Task);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Weakly/Tasks/TaskEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class TaskEventArgs : EventArgs
/// <param name="task">The supplied Task.</param>
public TaskEventArgs(Task task)
{
if (task == null)
if (task is null)
throw new ArgumentNullException(nameof(task));

_task = task;
Expand Down
6 changes: 3 additions & 3 deletions src/Weakly/Tasks/TaskHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static Task<T> FailFastOnException<T>(this Task<T> task)
private static void OnTaskFaulted(Task task)
{
var handler = TaskFaulted;
if (handler != null)
if (handler is object)
handler(null, new TaskEventArgs(task));
}

Expand Down Expand Up @@ -208,11 +208,11 @@ public static void PropagateExceptionsTo(this Task task, SynchronizationContext
/// <param name="task">The task to watch.</param>
public static void Watch(this Task task)
{
if (task == null)
if (task is null)
throw new ArgumentNullException(nameof(task));

var handler = TaskWatched;
if (handler != null)
if (handler is object)
handler(null, new TaskEventArgs(task));
}

Expand Down

0 comments on commit c29f7d8

Please sign in to comment.