lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [14/58] [abbrv] lucenenet git commit: Added object overrides to Core.Support.HashMap so it can be used as a dictionary key based on values within the dictionary rather than by reference equality.
Date Thu, 10 Nov 2016 11:47:28 GMT
Added object overrides to Core.Support.HashMap so it can be used as a dictionary key based
on values within the dictionary rather than by reference equality.


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

Branch: refs/heads/master
Commit: 63fa4caff3c10f2e9ad9c63c6b24043534f8a2a6
Parents: dc8a4b8
Author: Shad Storhaug <shad@shadstorhaug.com>
Authored: Sat Nov 5 16:51:13 2016 +0700
Committer: Shad Storhaug <shad@shadstorhaug.com>
Committed: Mon Nov 7 18:44:13 2016 +0700

----------------------------------------------------------------------
 src/Lucene.Net.Core/Support/HashMap.cs | 78 +++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/63fa4caf/src/Lucene.Net.Core/Support/HashMap.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Support/HashMap.cs b/src/Lucene.Net.Core/Support/HashMap.cs
index addd506..0fcf29a 100644
--- a/src/Lucene.Net.Core/Support/HashMap.cs
+++ b/src/Lucene.Net.Core/Support/HashMap.cs
@@ -125,6 +125,84 @@ namespace Lucene.Net.Support
             return this[key];
         }
 
+        #region Object overrides
+
+        public override bool Equals(object obj)
+        {
+            if (obj == this)
+                return true;
+
+            if (!(obj is IDictionary<TKey, TValue>))
+                return false;
+            IDictionary<TKey, TValue> m = (IDictionary<TKey, TValue>)obj;
+            if (m.Count != Count)
+                return false;
+
+            try
+            {
+                var i = GetEnumerator();
+                while (i.MoveNext())
+                {
+                    KeyValuePair<TKey, TValue> e = i.Current;
+                    TKey key = e.Key;
+                    TValue value = e.Value;
+                    if (value == null)
+                    {
+                        if (!(m[key] == null && m.ContainsKey(key)))
+                            return false;
+                    }
+                    else
+                    {
+                        if (!value.Equals(m[key]))
+                            return false;
+                    }
+                }
+            }
+            catch (InvalidCastException)
+            {
+                return false;
+            }
+            catch (NullReferenceException)
+            {
+                return false;
+            }
+
+            return true;
+        }
+
+        public override int GetHashCode()
+        {
+            int h = 0;
+            var i = GetEnumerator();
+            while (i.MoveNext())
+                h += i.Current.GetHashCode();
+            return h;
+        }
+
+        public override string ToString()
+        {
+            var i = GetEnumerator();
+            if (!i.MoveNext())
+                return "{}";
+
+            StringBuilder sb = new StringBuilder();
+            sb.Append('{');
+            for (;;)
+            {
+                var e = i.Current;
+                TKey key = e.Key;
+                TValue value = e.Value;
+                sb.Append(key);
+                sb.Append('=');
+                sb.Append(value);
+                if (!i.MoveNext())
+                    return sb.Append('}').ToString();
+                sb.Append(',').Append(' ');
+            }
+        }
+
+        #endregion
+
         #region Implementation of IEnumerable
 
         public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()


Mime
View raw message