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