Skip to content

Commit

Permalink
Improved equality checks
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed Mar 18, 2019
1 parent ab9987e commit f8d73f2
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions src/Exceptionless/Extensions/CollectionEqualityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
namespace Exceptionless {
internal static class CollectionEqualityExtensions {
public static bool CollectionEquals<T>(this IEnumerable<T> source, IEnumerable<T> other) {
if (source == null && other == null)
return true;

if (source == null || other == null)
return false;

var sourceEnumerator = source.GetEnumerator();
var otherEnumerator = other.GetEnumerator();

Expand All @@ -14,23 +20,29 @@ public static bool CollectionEquals<T>(this IEnumerable<T> source, IEnumerable<T
return false;
}

if (sourceEnumerator.Current.Equals(otherEnumerator.Current)) {
// values aren't equal
var sourceValue = sourceEnumerator.Current;
var otherValue = otherEnumerator.Current;
if (sourceValue == null && otherValue == null)
continue;

if (source == null || other == null || !sourceValue.Equals(otherValue))
return false;
}
}

if (otherEnumerator.MoveNext()) {
// counts differ
return false;
}

return true;
}

public static bool CollectionEquals<TValue>(this IDictionary<string, TValue> source, IDictionary<string, TValue> other) {
if (source.Count != other.Count) {
if (source == null && other == null)
return true;

if (source == null || other == null || source.Count != other.Count)
return false;
}

foreach (var key in source.Keys) {
var sourceValue = source[key];
Expand All @@ -40,12 +52,26 @@ public static bool CollectionEquals<TValue>(this IDictionary<string, TValue> sou
return false;
}

if (sourceValue.Equals(otherValue)) {
if (sourceValue == null && otherValue == null)
continue;

if (source == null || other == null || !sourceValue.Equals(otherValue))
return false;
}
}

return true;
}


public static bool CollectionEquals<TValue>(this ISet<TValue> source, ISet<TValue> other) {
if (source == null && other == null)
return true;

if (source == null || other == null || source.Count != other.Count)
return false;

return source.SetEquals(other);
}

public static int GetCollectionHashCode<T>(this IEnumerable<T> source) {
var assemblyQualifiedName = typeof(T).AssemblyQualifiedName;
Expand Down

1 comment on commit f8d73f2

@srijken
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

Please sign in to comment.