2010-03-26 Veerapuram Varadhan <vvaradhan@novell.com>
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlInt16.cs
1 //
2 // System.Data.SqlTypes.SqlInt16
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 SqlInt16 : INullable, IComparable
47 #if NET_2_0
48                                 , IXmlSerializable
49 #endif
50         {
51                 #region Fields
52
53                 private short value;
54                 private bool notNull;
55
56                 public static readonly SqlInt16 MaxValue = new SqlInt16 (32767);
57                 public static readonly SqlInt16 MinValue = new SqlInt16 (-32768);
58                 public static readonly SqlInt16 Null;
59                 public static readonly SqlInt16 Zero = new SqlInt16 (0);
60
61                 #endregion
62
63                 #region Constructors
64
65                 public SqlInt16 (short value)
66                 {
67                         this.value = value;
68                         notNull = true;;
69                 }
70
71                 #endregion
72
73                 #region Properties
74
75                 public bool IsNull {
76                         get { return !notNull; }
77                 }
78
79                 public short Value {
80                         get {
81                                 if (this.IsNull) 
82                                         throw new SqlNullValueException ();
83                                 else 
84                                         return value; 
85                         }
86                 }
87
88                 #endregion
89
90                 #region Methods
91
92                 public static SqlInt16 Add (SqlInt16 x, SqlInt16 y)
93                 {
94                         return (x + y);
95                 }
96
97                 public static SqlInt16 BitwiseAnd (SqlInt16 x, SqlInt16 y)
98                 {
99                         return (x & y);
100                 }
101
102                 public static SqlInt16 BitwiseOr (SqlInt16 x, SqlInt16 y)
103                 {
104                         return (x | y);
105                 }
106
107                 public int CompareTo (object value)
108                 {
109                         if (value == null)
110                                 return 1;
111                         else if (!(value is SqlInt16))
112                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlInt16"));
113                         return CompareSqlInt16 ((SqlInt16) value);
114                 }
115
116 #if NET_2_0
117                 public int CompareTo (SqlInt16 value)
118                 {
119                         return CompareSqlInt16 (value);
120                 }
121 #endif
122
123                 private int CompareSqlInt16 (SqlInt16 value)
124                 {
125                          if (((SqlInt16)value).IsNull)
126                                 return 1;
127                         else
128                                 return this.value.CompareTo (value.Value);
129                 }
130
131                 public static SqlInt16 Divide (SqlInt16 x, SqlInt16 y)
132                 {
133                         return (x / y);
134                 }
135
136                 public override bool Equals (object value)
137                 {
138                         if (!(value is SqlInt16))
139                                 return false;
140                         else if (this.IsNull)
141                                 return ((SqlInt16)value).IsNull;
142                         else if (((SqlInt16)value).IsNull)
143                                 return false;
144                         else
145                                 return (bool)(this == (SqlInt16)value);
146                 }
147
148                 public static SqlBoolean Equals (SqlInt16 x, SqlInt16 y)
149                 {
150                         return (x == y);
151                 }
152
153                 public override int GetHashCode ()
154                 {
155                         return (int) value;
156                 }
157
158                 public static SqlBoolean GreaterThan (SqlInt16 x, SqlInt16 y)
159                 {
160                         return (x > y);
161                 }
162
163                 public static SqlBoolean GreaterThanOrEqual (SqlInt16 x, SqlInt16 y)
164                 {
165                         return (x >= y);
166                 }
167
168                 public static SqlBoolean LessThan (SqlInt16 x, SqlInt16 y)
169                 {
170                         return (x < y);
171                 }
172
173                 public static SqlBoolean LessThanOrEqual (SqlInt16 x, SqlInt16 y)
174                 {
175                         return (x <= y);
176                 }
177
178                 public static SqlInt16 Mod (SqlInt16 x, SqlInt16 y)
179                 {
180                         return (x % y);
181                 }
182                 
183 #if NET_2_0
184                 public static SqlInt16 Modulus (SqlInt16 x, SqlInt16 y)
185                 {
186                         return (x % y);
187                 }
188 #endif
189
190                 public static SqlInt16 Multiply (SqlInt16 x, SqlInt16 y)
191                 {
192                         return (x * y);
193                 }
194
195                 public static SqlBoolean NotEquals (SqlInt16 x, SqlInt16 y)
196                 {
197                         return (x != y);
198                 }
199
200                 public static SqlInt16 OnesComplement (SqlInt16 x)
201                 {
202                         if (x.IsNull)
203                                 return Null;
204
205                         return ~x;
206                 }
207
208                 public static SqlInt16 Parse (string s)
209                 {
210                         checked {
211                                 return new SqlInt16 (Int16.Parse (s));
212                         }
213                 }
214
215                 public static SqlInt16 Subtract (SqlInt16 x, SqlInt16 y)
216                 {
217                         return (x - y);
218                 }
219
220                 public SqlBoolean ToSqlBoolean ()
221                 {
222                         return ((SqlBoolean) this);
223                 }
224                 
225                 public SqlByte ToSqlByte ()
226                 {
227                         return ((SqlByte) this);
228                 }
229
230                 public SqlDecimal ToSqlDecimal ()
231                 {
232                         return ((SqlDecimal) this);
233                 }
234
235                 public SqlDouble ToSqlDouble ()
236                 {
237                         return ((SqlDouble) this);
238                 }
239
240                 public SqlInt32 ToSqlInt32 ()
241                 {
242                         return ((SqlInt32) this);
243                 }
244
245                 public SqlInt64 ToSqlInt64 ()
246                 {
247                         return ((SqlInt64) this);
248                 }
249
250                 public SqlMoney ToSqlMoney ()
251                 {
252                         return ((SqlMoney) this);
253                 }
254
255                 public SqlSingle ToSqlSingle ()
256                 {
257                         return ((SqlSingle) this);
258                 }
259
260                 public SqlString ToSqlString ()
261                 {
262                         return ((SqlString) this);
263                 }
264
265                 public override string ToString ()
266                 {
267                         if (this.IsNull)
268                                 return "Null";
269                         else
270                                 return value.ToString ();
271                 }
272
273                 public static SqlInt16 Xor (SqlInt16 x, SqlInt16 y)
274                 {
275                         return (x ^ y);
276                 }
277
278                 public static SqlInt16 operator + (SqlInt16 x, SqlInt16 y)
279                 {
280                         checked {
281                                 return new SqlInt16 ((short) (x.Value + y.Value));
282                         }
283                 }
284
285                 public static SqlInt16 operator & (SqlInt16 x, SqlInt16 y)
286                 {
287                         return new SqlInt16 ((short) (x.value & y.Value));
288                 }
289
290                 public static SqlInt16 operator | (SqlInt16 x, SqlInt16 y)
291                 {
292                         return new SqlInt16 ((short) ( x.Value | y.Value));
293                 }
294
295                 public static SqlInt16 operator / (SqlInt16 x, SqlInt16 y)
296                 {
297                         checked {
298                                 return new SqlInt16 ((short) (x.Value / y.Value));
299                         }
300                 }
301
302                 public static SqlBoolean operator == (SqlInt16 x, SqlInt16 y)
303                 {
304                         if (x.IsNull || y.IsNull)
305                                 return SqlBoolean.Null;
306                         else
307                                 return new SqlBoolean (x.Value == y.Value);
308                 }
309
310                 public static SqlInt16 operator ^ (SqlInt16 x, SqlInt16 y)
311                 {
312                         return new SqlInt16 ((short) (x.Value ^ y.Value));
313                 }
314
315                 public static SqlBoolean operator > (SqlInt16 x, SqlInt16 y)
316                 {
317                         if (x.IsNull || y.IsNull)
318                                 return SqlBoolean.Null;
319                         else
320                                 return new SqlBoolean (x.Value > y.Value);
321                 }
322
323                 public static SqlBoolean operator >= (SqlInt16 x, SqlInt16 y)
324                 {
325                         if (x.IsNull || y.IsNull)
326                                 return SqlBoolean.Null;
327                         else
328                                 return new SqlBoolean (x.Value >= y.Value);
329                 }
330
331                 public static SqlBoolean operator != (SqlInt16 x, SqlInt16 y)
332                 {
333                         if (x.IsNull || y.IsNull)
334                                 return SqlBoolean.Null;
335                         else 
336                                 return new SqlBoolean (!(x.Value == y.Value));
337                 }
338
339                 public static SqlBoolean operator < (SqlInt16 x, SqlInt16 y)
340                 {
341                         if (x.IsNull || y.IsNull)
342                                 return SqlBoolean.Null;
343                         else
344                                 return new SqlBoolean (x.Value < y.Value);
345                 }
346
347                 public static SqlBoolean operator <= (SqlInt16 x, SqlInt16 y)
348                 {
349                         if (x.IsNull || y.IsNull)
350                                 return SqlBoolean.Null;
351                         else
352                                 return new SqlBoolean (x.Value <= y.Value);
353                 }
354
355                 public static SqlInt16 operator % (SqlInt16 x, SqlInt16 y)
356                 {
357                         return new SqlInt16 ((short) (x.Value % y.Value));
358                 }
359
360                 public static SqlInt16 operator * (SqlInt16 x, SqlInt16 y)
361                 {
362                         checked {
363                                 return new SqlInt16 ((short) (x.Value * y.Value));
364                         }
365                 }
366
367                 public static SqlInt16 operator ~ (SqlInt16 x)
368                 {
369                         if (x.IsNull)
370                                 return Null;
371                         
372                         return new SqlInt16 ((short) (~x.Value));
373                 }
374
375                 public static SqlInt16 operator - (SqlInt16 x, SqlInt16 y)
376                 {
377                         checked {
378                                 return new SqlInt16 ((short) (x.Value - y.Value));
379                         }
380                 }
381
382                 public static SqlInt16 operator - (SqlInt16 x)
383                 {
384                         checked {
385                                 return new SqlInt16 ((short) (-x.Value));
386                         }
387                 }
388
389                 public static explicit operator SqlInt16 (SqlBoolean x)
390                 {
391                         if (x.IsNull)
392                                 return Null;
393                         else
394                                 return new SqlInt16 ((short) x.ByteValue);
395                 }
396
397                 public static explicit operator SqlInt16 (SqlDecimal x)
398                 {               
399                         checked {
400                                 if (x.IsNull)
401                                         return Null;
402                                 else
403                                         return new SqlInt16 ((short) x.Value);
404                         }
405                 }
406
407                 public static explicit operator SqlInt16 (SqlDouble x)
408                 {
409                         if (x.IsNull)
410                                 return Null;
411                         else 
412                                 return new SqlInt16 (checked ((short) x.Value));
413                 }
414
415                 public static explicit operator short (SqlInt16 x)
416                 {
417                         return x.Value;
418                 }
419
420                 public static explicit operator SqlInt16 (SqlInt32 x)
421                 {
422                         checked {
423                                 if (x.IsNull)
424                                         return Null;
425                                 else 
426                                         return new SqlInt16 ((short) x.Value);
427                         }
428                 }
429
430                 public static explicit operator SqlInt16 (SqlInt64 x)
431                 {
432                         if (x.IsNull)
433                                 return Null;
434                         else {
435                                 checked {
436                                         return new SqlInt16 ((short) x.Value);
437                                 }
438                         }
439                 }
440
441                 public static explicit operator SqlInt16 (SqlMoney x)
442                 {
443                         checked {
444                                 if (x.IsNull)
445                                         return Null;
446                                 else 
447                                         return new SqlInt16 ((short) Math.Round (x.Value));
448                         }
449                 }
450
451
452                 public static explicit operator SqlInt16 (SqlSingle x)
453                 {
454                         if (x.IsNull)
455                                 return Null;
456                         else {
457                                 checked {
458                                         return new SqlInt16 ((short) x.Value);
459                                 }
460                         }
461                 }
462
463                 public static explicit operator SqlInt16 (SqlString x)
464                 {       
465                         if (x.IsNull)
466                                 return Null;
467
468                         return SqlInt16.Parse (x.Value);
469                 }
470
471                 public static implicit operator SqlInt16 (short x)
472                 {
473                         return new SqlInt16 (x);
474                 }
475
476                 public static implicit operator SqlInt16 (SqlByte x)
477                 {
478                         return new SqlInt16 ((short) x.Value);
479                 }
480
481 #if NET_2_0
482                 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
483                 {
484                         if (schemaSet != null && schemaSet.Count == 0) {
485                                 XmlSchema xs = new XmlSchema ();
486                                 XmlSchemaComplexType ct = new XmlSchemaComplexType ();
487                                 ct.Name = "short";
488                                 xs.Items.Add (ct);
489                                 schemaSet.Add (xs);
490                         }
491                         return new XmlQualifiedName ("short", "http://www.w3.org/2001/XMLSchema");
492                 }
493
494                 XmlSchema IXmlSerializable.GetSchema ()
495                 {
496                         return null;
497                 }
498         
499                 void IXmlSerializable.ReadXml (XmlReader reader)
500                 {
501                         if (reader == null)
502                                 return;
503
504                         switch (reader.ReadState) {
505                         case ReadState.EndOfFile:
506                         case ReadState.Error:
507                         case ReadState.Closed:
508                                 return;
509                         }
510
511                         // Skip XML declaration and prolog
512                         // or do I need to validate for the <SqlInt16> tag?
513                         reader.MoveToContent ();
514                         if (reader.EOF)
515                                 return;
516
517                         reader.Read ();
518                         if (reader.NodeType == XmlNodeType.EndElement)
519                                 return;
520
521                         if (reader.Value.Length > 0) {
522                                 if (String.Compare ("Null", reader.Value) == 0) {
523                                         // means a null reference/invalid value
524                                         notNull = false;
525                                         return; 
526                                 }
527                                 // FIXME: Validate the value for expected format
528                                 this.value = Int16.Parse (reader.Value);
529                                 this.notNull = true;
530                         }
531                 }
532                 
533                 void IXmlSerializable.WriteXml (XmlWriter writer) 
534                 {
535                         writer.WriteString (this.ToString ());
536                 }
537 #endif
538
539                 #endregion
540         }
541 }