/Src/NHibernate.Envers/Tools/DictionaryComparer.cs

https://bitbucket.org/RogerKratz/nhibernate.envers/ · C# · 33 lines · 31 code · 2 blank · 0 comment · 4 complexity · d0c38e6feab3ba2245a70b25d2d52646 MD5 · raw file

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace NHibernate.Envers.Tools
  4. {
  5. public class DictionaryComparer<K, V> : IEqualityComparer<IDictionary<K, V>>
  6. {
  7. public bool Equals(IDictionary<K, V> x, IDictionary<K, V> y)
  8. {
  9. if (x.Count != y.Count)
  10. return false;
  11. foreach (var xKeyValue in x)
  12. {
  13. V yValue;
  14. if (y.TryGetValue(xKeyValue.Key, out yValue))
  15. {
  16. if (!xKeyValue.Value.Equals(yValue))
  17. return false;
  18. }
  19. else
  20. {
  21. return false;
  22. }
  23. }
  24. return true;
  25. }
  26. public int GetHashCode(IDictionary<K, V> obj)
  27. {
  28. return obj.Aggregate(123, (current, keyValue) => current + (keyValue.GetHashCode()*31));
  29. }
  30. }
  31. }