Use ObjectPool in ConcurrentQueue
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlGuid.cs
1 //
2 // System.Data.SqlTypes.SqlGuid
3 //
4 // Author:
5 //   Tim Coleman <tim@timcoleman.com>
6 //   Ville Palo <vi64pa@koti.soon.fi>
7 //
8 // (C) Copyright 2002 Tim Coleman
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Xml;
36 using System.Xml.Schema;
37 using System.Globalization;
38 using System.Xml.Serialization;
39
40 namespace System.Data.SqlTypes
41 {
42 #if NET_2_0
43         [SerializableAttribute]
44         [XmlSchemaProvider ("GetXsdType")]
45 #endif
46         public struct SqlGuid : INullable, IComparable
47 #if NET_2_0
48                                 , IXmlSerializable
49 #endif
50         {
51                 #region Fields
52
53                 Guid value;
54
55                 private bool notNull;
56
57                 public static readonly SqlGuid Null;
58
59                 #endregion
60
61                 #region Constructors
62
63                 public SqlGuid (byte[] value) 
64                 {
65                         this.value = new Guid (value);
66                         notNull = true;
67                 }
68
69                 public SqlGuid (Guid g) 
70                 {
71                         this.value = g;
72                         notNull = true;
73                 }
74
75                 public SqlGuid (string s) 
76                 {
77                         this.value = new Guid (s);
78                         notNull = true;
79                 }
80
81                 public SqlGuid (int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k) 
82                 {
83                         this.value = new Guid (a, b, c, d, e, f, g, h, i, j, k);
84                         notNull = true;
85                 }
86
87                 #endregion
88
89                 #region Properties
90
91                 public bool IsNull {
92                         get { return !notNull; }
93                 }
94
95                 public Guid Value { 
96                         get { 
97                                 if (this.IsNull) 
98                                         throw new SqlNullValueException ("The property contains Null.");
99                                 else 
100                                         return value; 
101                         }
102                 }
103
104                 #endregion
105
106                 #region Methods
107
108                 public int CompareTo (object value)
109                 {
110                         if (value == null)
111                                 return 1;
112                         if (!(value is SqlGuid))
113                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlGuid"));
114
115                         return CompareTo ((SqlGuid) value);
116                 }
117 #if NET_2_0
118                 public
119 #endif
120                 int CompareTo (SqlGuid value)
121                 {
122                         if (value.IsNull)
123                                 return 1;
124                         else
125                                 // LAMESPEC : ms.net implementation actually compares all the 16 bytes.
126                                 // This code is kept for future changes, if required.
127                                 /*
128                                 {
129                                         //MSDN documentation says that CompareTo is different from 
130                                         //Guid's CompareTo. It uses the SQL Server behavior where
131                                         //only the last 6 bytes of value are evaluated  
132                                         byte[] compareValue = ((SqlGuid)value).GetLastSixBytes();
133                                         byte[] currentValue = GetLastSixBytes();
134                                         for (int i = 0; i < 6; i++)
135                                         {
136                                                 if (currentValue[i] != compareValue[i]) {
137                                                       return Compare(currentValue[i], compareValue[i]);
138                                                 }
139                                         }
140                                         return 0;
141                                 }
142                                 */
143                                 return this.value.CompareTo (value.Value);
144                                 
145                 }
146
147                 public override bool Equals (object value)
148                 {
149                         if (!(value is SqlGuid))
150                                 return false;
151                         else if (this.IsNull)
152                                 return ((SqlGuid)value).IsNull;
153                         else if (((SqlGuid)value).IsNull)
154                                 return false;
155                         else
156                                 return (bool) (this == (SqlGuid)value);
157                 }
158
159                 public static SqlBoolean Equals (SqlGuid x, SqlGuid y)
160                 {
161                         return (x == y);
162                 }
163
164                 public override int GetHashCode ()
165                 {
166                         byte [] bytes  = this.ToByteArray ();
167                         
168                         int result = 10;
169                         foreach (byte b in  bytes) {
170                                 result = 91 * result + b.GetHashCode ();
171                         }
172
173                         return result;
174                 }
175
176                 public static SqlBoolean GreaterThan (SqlGuid x, SqlGuid y)
177                 {
178                         return (x > y);
179                 }
180
181                 public static SqlBoolean GreaterThanOrEqual (SqlGuid x, SqlGuid y)
182                 {
183                         return (x >= y);
184                 }
185
186                 public static SqlBoolean LessThan (SqlGuid x, SqlGuid y)
187                 {
188                         return (x < y);
189                 }
190
191                 public static SqlBoolean LessThanOrEqual (SqlGuid x, SqlGuid y)
192                 {
193                         return (x <= y);
194                 }
195
196                 public static SqlBoolean NotEquals (SqlGuid x, SqlGuid y)
197                 {
198                         return (x != y);
199                 }
200
201                 public static SqlGuid Parse (string s)
202                 {
203                         return new SqlGuid (s);
204                 }
205
206                 public byte[] ToByteArray()
207                 {
208                         return value.ToByteArray ();
209                 }
210
211                 public SqlBinary ToSqlBinary ()
212                 {
213                         return ((SqlBinary)this);
214                 }
215
216                 public SqlString ToSqlString ()
217                 {
218                         return ((SqlString)this);
219                 }
220
221                 public override string ToString ()
222                 {
223                         if (!notNull)
224                                 return "Null";
225                         else
226                                 return value.ToString ();
227                 }
228
229                 public static SqlBoolean operator == (SqlGuid x, SqlGuid y)
230                 {
231                         if (x.IsNull || y.IsNull) return SqlBoolean.Null;
232                         return new SqlBoolean (x.Value == y.Value);
233                 }
234
235                 public static SqlBoolean operator > (SqlGuid x, SqlGuid y)
236                 {
237                         if (x.IsNull || y.IsNull)
238                                 return SqlBoolean.Null;
239
240                         if (x.Value.CompareTo (y.Value) > 0)
241                                 return new SqlBoolean (true);
242                         else
243                                 return new SqlBoolean (false);
244                 }
245
246                 public static SqlBoolean operator >= (SqlGuid x, SqlGuid y)
247                 {
248                         if (x.IsNull || y.IsNull)
249                                 return SqlBoolean.Null;
250                         
251                         if (x.Value.CompareTo (y.Value) >= 0)
252                                 return new SqlBoolean (true);
253                         else
254                                 return new SqlBoolean (false);
255
256                 }
257
258                 public static SqlBoolean operator != (SqlGuid x, SqlGuid y)
259                 {
260                         if (x.IsNull || y.IsNull) return SqlBoolean.Null;
261                         return new SqlBoolean (!(x.Value == y.Value));
262                 }
263
264                 public static SqlBoolean operator < (SqlGuid x, SqlGuid y)
265                 {
266                         if (x.IsNull || y.IsNull)
267                                 return SqlBoolean.Null;
268
269                         if (x.Value.CompareTo (y.Value) < 0)
270                                 return new SqlBoolean (true);
271                         else
272                                 return new SqlBoolean (false);
273
274                 }
275
276                 public static SqlBoolean operator <= (SqlGuid x, SqlGuid y)
277                 {
278                         if (x.IsNull || y.IsNull)
279                                 return SqlBoolean.Null;
280
281                         if (x.Value.CompareTo (y.Value) <= 0)
282                                 return new SqlBoolean (true);
283                         else
284                                 return new SqlBoolean (false);
285                 }
286
287                 public static explicit operator SqlGuid (SqlBinary x)
288                 {
289                         return new SqlGuid (x.Value);
290                 }
291
292                 public static explicit operator Guid (SqlGuid x)
293                 {
294                         return x.Value;
295                 }
296
297                 public static explicit operator SqlGuid (SqlString x)
298                 {
299                         return new SqlGuid (x.Value);
300                 }
301
302                 public static implicit operator SqlGuid (Guid x)
303                 {
304                         return new SqlGuid (x);
305                 }
306
307 #if NET_2_0
308                 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
309                 {
310                         XmlQualifiedName qualifiedName = new XmlQualifiedName ("string", "http://www.w3.org/2001/XMLSchema");
311                         return qualifiedName;
312                 }
313
314                 [MonoTODO]
315                 XmlSchema IXmlSerializable.GetSchema ()
316                 {
317                         throw new NotImplementedException ();
318                 }
319                 
320                 [MonoTODO]
321                 void IXmlSerializable.ReadXml (XmlReader reader)
322                 {
323                         throw new NotImplementedException ();
324                 }
325                 
326                 [MonoTODO]
327                 void IXmlSerializable.WriteXml (XmlWriter writer) 
328                 {
329                         throw new NotImplementedException ();
330                 }
331 #endif
332                 #endregion
333         }
334 }
335