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