Lucene.Net.Core.Support.Arrays.Equals(): Changed implementation to be more like the original JDK version, including the ability to test equality based on the values of the nested value if it is a collection
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/d0c2c108 Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/d0c2c108 Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/d0c2c108 Branch: refs/heads/api-work Commit: d0c2c108eea1fd2035a9d8a54eb9cee34772ac74 Parents: 842182c Author: Shad Storhaug <[email protected]> Authored: Thu Mar 30 09:55:01 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Thu Mar 30 09:55:01 2017 +0700 ---------------------------------------------------------------------- src/Lucene.Net.Core/Support/Arrays.cs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d0c2c108/src/Lucene.Net.Core/Support/Arrays.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Core/Support/Arrays.cs b/src/Lucene.Net.Core/Support/Arrays.cs index 80cd06e..bea165d 100644 --- a/src/Lucene.Net.Core/Support/Arrays.cs +++ b/src/Lucene.Net.Core/Support/Arrays.cs @@ -87,19 +87,39 @@ namespace Lucene.Net.Support /// considered equal if (e1==null ? e2==null : e1.equals(e2)). In other /// words, the two arrays are equal if they contain the same elements in /// the same order. Also, two array references are considered equal if - /// both are null.</returns> + /// both are null. + /// <para/> + /// Note that if the type of <paramref name="T"/> is a <see cref="IDictionary{TKey, TValue}"/>, + /// <see cref="IList{T}"/>, or <see cref="ISet{T}"/>, its values and any nested collection values + /// will be compared for equality as well. + /// </returns> public static bool Equals<T>(T[] a, T[] b) { - if (a == null) + if (object.ReferenceEquals(a, b)) + { + return true; + } + bool isValueType = typeof(T).IsValueType; + if (!isValueType && a == null) + { return b == null; + } + + int length = a.Length; - if (a.Length != b.Length) + if (b.Length != length) + { return false; + } - for (int i = 0; i < a.Length; i++) + for (int i = 0; i < length; i++) { - if (!object.Equals(a[i], b[i])) + T o1 = a[i]; + T o2 = b[i]; + if (!(isValueType ? o1.Equals(o2) : (o1 == null ? o2 == null : Collections.Equals(o1, o2)))) + { return false; + } } return true;
