2010-03-26 Veerapuram Varadhan <vvaradhan@novell.com>
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlSingle.cs
1 //
2 // System.Data.SqlTypes.SqlSingle
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 SqlSingle : INullable, IComparable
47 #if NET_2_0
48                                 , IXmlSerializable
49 #endif
50         {
51                 #region Fields
52
53                 private float value;
54                 private bool notNull;
55
56                 public static readonly SqlSingle MaxValue = new SqlSingle (3.40282346638528859E+38f);
57                 public static readonly SqlSingle MinValue = new SqlSingle (-3.40282346638528859E+38f);
58                 public static readonly SqlSingle Null;
59                 public static readonly SqlSingle Zero = new SqlSingle (0);
60
61                 #endregion
62
63                 #region Constructors
64
65                 public SqlSingle (double value)
66                 {
67                         this.value = (float)value;
68                         notNull = true;
69                 }
70
71                 public SqlSingle (float value)
72                 {
73                         this.value = value;
74                         notNull = true;
75                 }
76
77                 #endregion
78
79                 #region Properties
80
81                 public bool IsNull {
82                         get { return !notNull; }
83                 }
84
85                 public float Value {
86                         get {
87                                 if (this.IsNull)
88                                         throw new SqlNullValueException ();
89                                 else 
90                                         return value; 
91                         }
92                 }
93
94                 #endregion
95
96                 #region Methods
97
98                 public static SqlSingle Add (SqlSingle x, SqlSingle y)
99                 {
100                         return (x + y);
101                 }
102
103                 public int CompareTo (object value)
104                 {
105                         if (value == null)
106                                 return 1;
107                         else if (!(value is SqlSingle))
108                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlSingle"));
109
110                         return CompareSqlSingle ((SqlSingle) value);
111                 }
112
113 #if NET_2_0
114                 public int CompareTo (SqlSingle value)
115                 {
116                         return CompareSqlSingle (value);
117                 }
118 #endif
119                 
120                 private int CompareSqlSingle (SqlSingle value)
121                 {
122                         if (value.IsNull)
123                                 return 1;
124                         else
125                                 return this.value.CompareTo (value.Value);
126                 }
127
128                 public static SqlSingle Divide (SqlSingle x, SqlSingle y)
129                 {
130                         return (x / y);
131                 }
132
133                 public override bool Equals (object value)
134                 {
135                         if (!(value is SqlSingle))
136                                 return false;
137                         else if (this.IsNull)
138                                 return ((SqlSingle)value).IsNull;
139                         else if (((SqlSingle)value).IsNull)
140                                 return false;
141                         else
142                                 return (bool) (this == (SqlSingle)value);
143                 }
144
145                 public static SqlBoolean Equals (SqlSingle x, SqlSingle y)
146                 {
147                         return (x == y);
148                 }
149
150                 public override int GetHashCode ()
151                 {
152                         long LongValue = (long) value;
153                         return (int) (LongValue ^ (LongValue >> 32));
154                 }
155
156                 public static SqlBoolean GreaterThan (SqlSingle x, SqlSingle y)
157                 {
158                         return (x > y);
159                 }
160
161                 public static SqlBoolean GreaterThanOrEqual (SqlSingle x, SqlSingle y)
162                 {
163                         return (x >= y);
164                 }
165
166                 public static SqlBoolean LessThan (SqlSingle x, SqlSingle y)
167                 {
168                         return (x < y);
169                 }
170
171                 public static SqlBoolean LessThanOrEqual (SqlSingle x, SqlSingle y)
172                 {
173                         return (x <= y);
174                 }
175
176                 public static SqlSingle Multiply (SqlSingle x, SqlSingle y)
177                 {
178                         return (x * y);
179                 }
180
181                 public static SqlBoolean NotEquals (SqlSingle x, SqlSingle y)
182                 {
183                         return (x != y);
184                 }
185
186                 public static SqlSingle Parse (string s)
187                 {
188                         return new SqlSingle (Single.Parse (s));
189                 }
190
191                 public static SqlSingle Subtract (SqlSingle x, SqlSingle y)
192                 {
193                         return (x - y);
194                 }
195
196                 public SqlBoolean ToSqlBoolean ()
197                 {
198                         return ((SqlBoolean) this);
199                 }
200                 
201                 public SqlByte ToSqlByte ()
202                 {
203                         return ((SqlByte) this);
204                 }
205
206                 public SqlDecimal ToSqlDecimal ()
207                 {
208                         return ((SqlDecimal) this);
209                 }
210
211                 public SqlDouble ToSqlDouble ()
212                 {
213                         return ((SqlDouble) this);
214                 }
215
216                 public SqlInt16 ToSqlInt16 ()
217                 {
218                         return ((SqlInt16) this);
219                 }
220
221                 public SqlInt32 ToSqlInt32 ()
222                 {
223                         return ((SqlInt32) this);
224                 }
225
226                 public SqlInt64 ToSqlInt64 ()
227                 {
228                         return ((SqlInt64) this);
229                 }
230
231                 public SqlMoney ToSqlMoney ()
232                 {
233                         return ((SqlMoney) this);
234                 }
235
236                 public SqlString ToSqlString ()
237                 {
238                         return ((SqlString) this);
239                 }
240
241                 public override string ToString ()
242                 {
243                         if (!notNull)
244                                 return "Null";
245                         return value.ToString ();
246                 }
247
248                 public static SqlSingle operator + (SqlSingle x, SqlSingle y)
249                 {
250                         float f = (float) (x.Value + y.Value);
251
252                         if (Single.IsInfinity (f))
253                                 throw new OverflowException ();
254
255                         return new SqlSingle (f);
256                 }
257
258                 public static SqlSingle operator / (SqlSingle x, SqlSingle y)
259                 {
260                         float f = (float) (x.Value / y.Value);
261
262                         if (Single.IsInfinity (f)) {
263                                 if (y.Value == 0d) 
264                                         throw new DivideByZeroException ();
265                         }
266
267                         return new SqlSingle (x.Value / y.Value);
268                 }
269
270                 public static SqlBoolean operator == (SqlSingle x, SqlSingle y)
271                 {
272                         if (x.IsNull || y .IsNull)
273                                 return SqlBoolean.Null;
274                         return new SqlBoolean (x.Value == y.Value);
275                 }
276
277                 public static SqlBoolean operator > (SqlSingle x, SqlSingle y)
278                 {
279                         if (x.IsNull || y .IsNull)
280                                 return SqlBoolean.Null;
281                         return new SqlBoolean (x.Value > y.Value);
282                 }
283
284                 public static SqlBoolean operator >= (SqlSingle x, SqlSingle y)
285                 {
286                         if (x.IsNull || y .IsNull)
287                                 return SqlBoolean.Null;
288                         return new SqlBoolean (x.Value >= y.Value);
289                 }
290
291                 public static SqlBoolean operator != (SqlSingle x, SqlSingle y)
292                 {
293                         if (x.IsNull || y .IsNull)
294                                 return SqlBoolean.Null;
295                         return new SqlBoolean (!(x.Value == y.Value));
296                 }
297
298                 public static SqlBoolean operator < (SqlSingle x, SqlSingle y)
299                 {
300                         if (x.IsNull || y .IsNull)
301                                 return SqlBoolean.Null;
302                         return new SqlBoolean (x.Value < y.Value);
303                 }
304
305                 public static SqlBoolean operator <= (SqlSingle x, SqlSingle y)
306                 {
307                         if (x.IsNull || y .IsNull)
308                                 return SqlBoolean.Null;
309                         return new SqlBoolean (x.Value <= y.Value);
310                 }
311
312                 public static SqlSingle operator * (SqlSingle x, SqlSingle y)
313                 {
314                         float f = (float) (x.Value * y.Value);
315                         
316                         if (Single.IsInfinity (f))
317                                 throw new OverflowException ();
318
319                         return new SqlSingle (f);
320                 }
321
322                 public static SqlSingle operator - (SqlSingle x, SqlSingle y)
323                 {
324                         float f = (float) (x.Value - y.Value);
325
326                         if (Single.IsInfinity (f))
327                                 throw new OverflowException ();
328
329                         return new SqlSingle (f);
330                 }
331
332                 public static SqlSingle operator - (SqlSingle x)
333                 {
334                         return new SqlSingle (-(x.Value));
335                 }
336
337                 public static explicit operator SqlSingle (SqlBoolean x)
338                 {
339                         checked {
340                                 if (x.IsNull)
341                                         return Null;
342                                 
343                                 return new SqlSingle((float)x.ByteValue);
344                         }
345                 }
346
347                 public static explicit operator SqlSingle (SqlDouble x)
348                 {
349                         if (x.IsNull)
350                                 return Null;
351
352                         float f = (float)x.Value;
353
354                         if (Single.IsInfinity (f))
355                                 throw new OverflowException ();
356
357                         return new SqlSingle(f);
358                 }
359
360                 public static explicit operator float (SqlSingle x)
361                 {
362                         return x.Value;
363                 }
364
365                 public static explicit operator SqlSingle (SqlString x)
366                 {
367                         checked {
368                                 if (x.IsNull)
369                                         return Null;
370                                 
371                                 return SqlSingle.Parse (x.Value);
372                         }
373                 }
374
375                 public static implicit operator SqlSingle (float x)
376                 {
377                         return new SqlSingle (x);
378                 }
379
380                 public static implicit operator SqlSingle (SqlByte x)
381                 {
382                         if (x.IsNull)
383                                 return Null;
384                         else 
385                                 return new SqlSingle((float) x.Value);
386                 }
387
388                 public static implicit operator SqlSingle (SqlDecimal x)
389                 {
390                         if (x.IsNull)
391                                 return Null;
392                         else
393                                 return new SqlSingle((float) x.Value);
394                 }
395
396                 public static implicit operator SqlSingle (SqlInt16 x)
397                 {
398                         if (x.IsNull)
399                                 return Null;
400                         else
401                                 return new SqlSingle((float) x.Value);
402                 }
403
404                 public static implicit operator SqlSingle (SqlInt32 x)
405                 {
406                         if (x.IsNull)
407                                 return Null;
408                         else
409                                 return new SqlSingle((float) x.Value);
410                 }
411
412                 public static implicit operator SqlSingle (SqlInt64 x)
413                 {
414                         if (x.IsNull)
415                                 return Null;
416                         else
417                                 return new SqlSingle((float) x.Value);
418                 }
419
420                 public static implicit operator SqlSingle (SqlMoney x)
421                 {
422                         if (x.IsNull)
423                                 return Null;
424                         else
425                                 return new SqlSingle((float) x.Value);
426                 }
427
428 #if NET_2_0
429                 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
430                 {
431                         XmlQualifiedName qualifiedName = new XmlQualifiedName ("float", "http://www.w3.org/2001/XMLSchema");
432                         return qualifiedName;
433                 }
434
435                 [MonoTODO]
436                 XmlSchema IXmlSerializable.GetSchema ()
437                 {
438                         throw new NotImplementedException ();
439                 }
440                 
441                 [MonoTODO]
442                 void IXmlSerializable.ReadXml (XmlReader reader)
443                 {
444                         throw new NotImplementedException ();
445                 }
446                 
447                 [MonoTODO]
448                 void IXmlSerializable.WriteXml (XmlWriter writer)
449                 {
450                         throw new NotImplementedException ();
451                 }
452 #endif
453
454                 #endregion
455         }
456 }