Lucene.Net.Grouping.SearchGroup: Fixed Equals() and GetHashCode() to use 
Collections.Equals() for the generic type in case it is a collection that needs 
its values compared


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/5c512dd5
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/5c512dd5
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/5c512dd5

Branch: refs/heads/api-work
Commit: 5c512dd5d23692b07d5b310d63f2661964b30e2a
Parents: d747e7a
Author: Shad Storhaug <[email protected]>
Authored: Thu Mar 30 08:19:47 2017 +0700
Committer: Shad Storhaug <[email protected]>
Committed: Thu Mar 30 09:12:32 2017 +0700

----------------------------------------------------------------------
 src/Lucene.Net.Grouping/SearchGroup.cs | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/5c512dd5/src/Lucene.Net.Grouping/SearchGroup.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Grouping/SearchGroup.cs 
b/src/Lucene.Net.Grouping/SearchGroup.cs
index a5e6cd5..f9772d9 100644
--- a/src/Lucene.Net.Grouping/SearchGroup.cs
+++ b/src/Lucene.Net.Grouping/SearchGroup.cs
@@ -180,9 +180,17 @@ namespace Lucene.Net.Search.Grouping
             }
             private bool inQueue;
 
+            // LUCENENET specific - store whether T is value type
+            // for optimization of GetHashCode() and Equals()
+            private readonly bool groupValueIsValueType;
+
             public MergedGroup(T groupValue)
             {
                 this.groupValue = groupValue;
+
+                // LUCENENET specific - store whether T is value type
+                // for optimization of GetHashCode() and Equals()
+                this.groupValueIsValueType = typeof(T).IsValueType;
             }
 
             // Only for assert
@@ -197,7 +205,13 @@ namespace Lucene.Net.Search.Grouping
                     }
                     else
                     {
-                        
Debug.Assert(!groupValue.Equals(otherMergedGroup.groupValue));
+                        
+                        Debug.Assert(!groupValueIsValueType 
+                            ? groupValue.Equals(otherMergedGroup.groupValue)
+
+                            // LUCENENET specific - use Collections.Equals() 
if we have a reference type
+                            // to ensure if it is a collection its contents 
are compared
+                            : Collections.Equals(groupValue, 
otherMergedGroup.groupValue));
                     }
                 }
                 return true;
@@ -218,7 +232,9 @@ namespace Lucene.Net.Search.Grouping
                     }
                     else
                     {
-                        return groupValue.Equals(otherMergedGroup);
+                        // LUCENENET specific - use Collections.Equals() if we 
have a reference type
+                        // to ensure if it is a collection its contents are 
compared
+                        return groupValueIsValueType ? 
groupValue.Equals(otherMergedGroup) : Collections.Equals(groupValue, 
otherMergedGroup);
                     }
                 }
                 else
@@ -235,7 +251,9 @@ namespace Lucene.Net.Search.Grouping
                 }
                 else
                 {
-                    return groupValue.GetHashCode();
+                    // LUCENENET specific - use Collections.GetHashCode() if 
we have a reference type
+                    // to ensure if it is a collection its contents are 
compared
+                    return groupValueIsValueType ? groupValue.GetHashCode() : 
Collections.GetHashCode(groupValue);
                 }
             }
         }

Reply via email to