PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Data.Linq/src/DbLinq/Data/Linq/Identity/IdentityKey.cs

https://bitbucket.org/danipen/mono
C# | 122 lines | 54 code | 9 blank | 59 comment | 6 complexity | 856e4c49e36847504b4c219026dcd602 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. #region MIT license
  2. //
  3. // MIT license
  4. //
  5. // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #endregion
  26. using System;
  27. using System.Collections.Generic;
  28. namespace DbLinq.Data.Linq.Identity
  29. {
  30. /// <summary>
  31. /// Identifies an object in a unique way (think Primay Keys in a database table)
  32. /// Identity is:
  33. /// - A type
  34. /// - A collection
  35. ///
  36. /// Example: to store Product with ProductID=1, we use the following IdentityKey:
  37. /// IdentityKey{Type=Product, Keys={1}}
  38. /// </summary>
  39. #if !MONO_STRICT
  40. public
  41. #endif
  42. sealed class IdentityKey
  43. {
  44. /// <summary>
  45. /// Entity type
  46. /// </summary>
  47. public Type Type { get; private set; }
  48. /// <summary>
  49. /// Entity keys
  50. /// </summary>
  51. public IList<object> Keys { get; private set; }
  52. private readonly int hashCode;
  53. /// <summary>
  54. /// Determines equality between two refs
  55. /// </summary>
  56. /// <param name="obj"></param>
  57. /// <returns></returns>
  58. public override bool Equals(object obj)
  59. {
  60. var other = (IdentityKey)obj;
  61. if (Type != other.Type)
  62. return false;
  63. if (Keys.Count != other.Keys.Count)
  64. return false;
  65. for (int keyIndex = 0; keyIndex < Keys.Count; keyIndex++)
  66. {
  67. if (!Equals(Keys[keyIndex], other.Keys[keyIndex]))
  68. return false;
  69. }
  70. return true;
  71. }
  72. /// <summary>
  73. /// Computes hash code
  74. /// </summary>
  75. /// <returns></returns>
  76. public override int GetHashCode()
  77. {
  78. return hashCode;
  79. }
  80. /// <summary>
  81. /// Initializes a new instance of the <see cref="IdentityKey"/> class.
  82. /// </summary>
  83. /// <param name="type">The type.</param>
  84. /// <param name="keys">The keys.</param>
  85. public IdentityKey(Type type, IEnumerable<object> keys)
  86. {
  87. Type = type;
  88. Keys = new List<object>(keys);
  89. // Done here becouse IdentityKeys exists to be keys in dictionaries...
  90. hashCode = type.GetHashCode();
  91. foreach (object key in keys)
  92. {
  93. hashCode ^= key.GetHashCode();
  94. }
  95. }
  96. /// <summary>
  97. /// Initializes a new instance of the <see cref="IdentityKey"/> class.
  98. /// </summary>
  99. /// <param name="type">The type.</param>
  100. /// <param name="keys">The keys.</param>
  101. public IdentityKey(Type type, params object[] keys)
  102. {
  103. Type = type;
  104. Keys = new List<object>(keys);
  105. // Done here becouse IdentityKeys exists to be keys in dictionaries...
  106. hashCode = Type.GetHashCode();
  107. foreach (object key in Keys)
  108. {
  109. hashCode ^= key.GetHashCode();
  110. }
  111. }
  112. }
  113. }