Merge branch 'master' of git://github.com/mono/mono
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes.jvm / SqlDouble.cs
1 // System.Data.SqlTypes.SqlDouble\r
2 //\r
3 // Authors:\r
4 //      Konstantin Triger <kostat@mainsoft.com>\r
5 //      Boris Kirzner <borisk@mainsoft.com>\r
6 //      \r
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)\r
8 //\r
9 \r
10 //\r
11 // Permission is hereby granted, free of charge, to any person obtaining\r
12 // a copy of this software and associated documentation files (the\r
13 // "Software"), to deal in the Software without restriction, including\r
14 // without limitation the rights to use, copy, modify, merge, publish,\r
15 // distribute, sublicense, and/or sell copies of the Software, and to\r
16 // permit persons to whom the Software is furnished to do so, subject to\r
17 // the following conditions:\r
18 // \r
19 // The above copyright notice and this permission notice shall be\r
20 // included in all copies or substantial portions of the Software.\r
21 // \r
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
29 //\r
30 \r
31 namespace System.Data.SqlTypes\r
32 {\r
33 \r
34     /**\r
35      * <p>Title: </p>\r
36      * <p>Description: </p>\r
37      * <p>Copyright: Copyright (c) 2002</p>\r
38      * <p>Company: MainSoft</p>\r
39      * @author Pavel Sandler\r
40      * @version 1.0\r
41      */\r
42 \r
43     using System;\r
44 \r
45     public struct SqlDouble : INullable, IComparable\r
46     {\r
47 \r
48         private Double _value;\r
49         private bool _isNull;\r
50 \r
51         //private static double _maxVal = 1.79E+308;\r
52         //private static double _minVal = -1.79E+308;\r
53 \r
54         public static readonly SqlDouble MaxValue = new SqlDouble(1.79E+308);\r
55         public static readonly SqlDouble MinValue = new SqlDouble(-1.79E+308);\r
56         public static readonly SqlDouble Null = new SqlDouble(true);\r
57         public static readonly SqlDouble Zero = new SqlDouble(0.0);\r
58 \r
59         \r
60         private SqlDouble(bool isNull)\r
61         {\r
62             _value = 0;\r
63             _isNull = isNull;\r
64         }\r
65 \r
66         /**\r
67          * Constructor\r
68          * @param value A double whose value will be used for the new SqlDouble.\r
69          */\r
70         public SqlDouble(double value) \r
71         { \r
72             _value = value;\r
73             _isNull = false;\r
74         }\r
75 \r
76         \r
77         /**\r
78          * Indicates whether or not Value is null.\r
79          * @return true if Value is null, otherwise false.\r
80          */\r
81         public bool IsNull\r
82         {\r
83             get\r
84             {\r
85                 return _isNull;\r
86             }\r
87         }\r
88 \r
89         /**\r
90          * Gets the value of the SqlDouble instance.\r
91          * @return the value of this instance\r
92          */\r
93         public double Value\r
94         {\r
95             get\r
96             {\r
97                 if(IsNull)\r
98                     throw new SqlNullValueException();\r
99                 return _value;\r
100             }\r
101         }\r
102 \r
103         /**\r
104          * The addition operator computes the sum of the two SqlDouble operands.\r
105          * @param x A SqlDouble structure.\r
106          * @param y A SqlDouble structure.\r
107          * @return The sum of the two SqlDouble operands.\r
108          * If one of the parameters is null or null value - return SqlDouble.Null.\r
109          */\r
110         public static SqlDouble Add(SqlDouble x, SqlDouble y)\r
111         {\r
112             if (x.IsNull || y.IsNull)\r
113                 return SqlDouble.Null;\r
114 \r
115             double xVal = x._value;\r
116             double yVal = y._value;\r
117 \r
118             return new SqlDouble(checked(xVal + yVal));\r
119         }\r
120 \r
121         /**\r
122          * Compares this instance to the supplied object and returns an indication of their relative values.\r
123          * @param obj The object to compare.\r
124          * @return A signed number indicating the relative values of the instance and the object.\r
125          * Less than zero This instance is less than object.\r
126          * Zero This instance is the same as object.\r
127          * Greater than zero This instance is greater than object -or-\r
128          * object is a null reference.\r
129          */\r
130         public int CompareTo(Object obj)\r
131         {\r
132             if (obj == null)\r
133                 return 1;\r
134 \r
135             if (obj is SqlDouble)\r
136             {\r
137                 SqlDouble d = (SqlDouble)obj;\r
138 \r
139                 if (d.IsNull)\r
140                     return 1;\r
141                 if (this.IsNull)\r
142                     return -1;\r
143 \r
144                 return this._value.CompareTo(d._value);\r
145             }\r
146 \r
147             throw new ArgumentException("parameter obj is not SqlDouble : " + obj.GetType().Name);\r
148 \r
149         }\r
150 \r
151         /**\r
152          * The division operator divides the first SqlDouble operand by the second.\r
153          * @param x A SqlDouble instance.\r
154          * @param y A SqlDouble instance.\r
155          * @return A SqlDouble structure containing the results of the division operation.\r
156          * If one of the parameters is null or null value - return SqlDouble.Null.\r
157          */\r
158         public static SqlDouble Divide(SqlDouble x, SqlDouble y)\r
159         {\r
160 \r
161             if (x.IsNull || y.IsNull)\r
162                 return SqlDouble.Null;\r
163 \r
164             double xVal = x._value;\r
165             double yVal = y._value;\r
166 \r
167             return new SqlDouble(checked(xVal / yVal));\r
168 \r
169         }\r
170 \r
171         public override bool Equals(Object obj)\r
172         {\r
173             \r
174             if (obj == null)\r
175                 return false;\r
176 \r
177             if (obj is SqlDouble)\r
178             {\r
179                 SqlDouble d = (SqlDouble)obj;\r
180 \r
181                 if (IsNull && d.IsNull)\r
182                     return true;\r
183 \r
184                 if (IsNull || d.IsNull)\r
185                     return false;\r
186 \r
187                 return _value == d._value;\r
188             }\r
189 \r
190             return false;\r
191         }\r
192 \r
193         /**\r
194          * Performs a logical comparison on two instances of SqlDouble to determine if they are equal.\r
195          * @param x A SqlDouble instance.\r
196          * @param y A SqlDouble instance.\r
197          * @return true if the two values are equal, otherwise false.\r
198          * If one of the parameters is null or null value return SqlBoolean.Null.\r
199          */\r
200         public static SqlBoolean Equals(SqlDouble x, SqlDouble y)\r
201         {\r
202             if (x.IsNull || y.IsNull)\r
203                 return SqlBoolean.Null;\r
204 \r
205             if (x.Equals(y))\r
206                 return SqlBoolean.True;\r
207 \r
208             return SqlBoolean.False;\r
209         }\r
210 \r
211         /**\r
212          * Compares two instances of SqlDouble to determine if the first is greater than the second.\r
213          * @param x A SqlDouble instance\r
214          * @param y A SqlDouble instance\r
215          * @return A SqlBoolean that is True if the first instance is greater than the second instance, otherwise False.\r
216          * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.\r
217          */\r
218         public static SqlBoolean GreaterThan(SqlDouble x, SqlDouble y)\r
219         {\r
220             if (x.IsNull || y.IsNull)\r
221                 return SqlBoolean.Null;\r
222 \r
223             if (x._value > y._value)\r
224                 return SqlBoolean.True;\r
225 \r
226             return SqlBoolean.False;\r
227         }\r
228 \r
229         /**\r
230          * Compares two instances of SqlDouble to determine if the first is greater than or equal to the second.\r
231          * @param x A SqlDouble instance\r
232          * @param y A SqlDouble instance\r
233          * @return A SqlBoolean that is True if the first instance is greaater than or equal to the second instance, otherwise False.\r
234          * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.\r
235          */\r
236         public static SqlBoolean GreaterThanOrEqual(SqlDouble x, SqlDouble y)\r
237         {\r
238             if (x.IsNull || y.IsNull)\r
239                 return SqlBoolean.Null;\r
240 \r
241             if (x._value >= y._value)\r
242                 return SqlBoolean.True;\r
243 \r
244             return SqlBoolean.False;\r
245         }\r
246 \r
247         /**\r
248          * Compares two instances of SqlDouble to determine if the first is less than the second.\r
249          * @param x A SqlDouble instance\r
250          * @param y A SqlDouble instance\r
251          * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.\r
252          * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.\r
253          */\r
254         public static SqlBoolean LessThan(SqlDouble x, SqlDouble y)\r
255         {\r
256             if (x.IsNull || y.IsNull)\r
257                 return SqlBoolean.Null;\r
258 \r
259             if (x._value < y._value)\r
260                 return SqlBoolean.True;\r
261 \r
262             return SqlBoolean.False;\r
263         }\r
264 \r
265         /**\r
266          * Compares two instances of SqlDouble to determine if the first is less than or equal to the second.\r
267          * @param x A SqlDouble instance\r
268          * @param y A SqlDouble instance\r
269          * @return A SqlBoolean that is True if the first instance is less than or equal to the second instance, otherwise False.\r
270          * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.\r
271          */\r
272         public static SqlBoolean LessThanOrEqual(SqlDouble x, SqlDouble y)\r
273         {\r
274             if (x.IsNull || y.IsNull)\r
275                 return SqlBoolean.Null;\r
276 \r
277             if (x._value <= y._value)\r
278                 return SqlBoolean.True;\r
279 \r
280             return SqlBoolean.False;\r
281         }\r
282 \r
283         /**\r
284          * The multiplication operator computes the product of the two SqlDouble operands.\r
285          * @param x A SqlDouble instance\r
286          * @param y A SqlDouble instance\r
287          * @return The product of the two SqlDouble operands.\r
288          */\r
289         public static SqlDouble Multiply(SqlDouble x, SqlDouble y)\r
290         {\r
291             if (x.IsNull || y.IsNull)\r
292                 return SqlDouble.Null;\r
293 \r
294             double xVal = x._value;\r
295             double yVal = y._value;\r
296 \r
297             return new SqlDouble(checked(xVal * yVal));\r
298         }\r
299 \r
300 \r
301         /**\r
302          * Compares two instances of SqlDouble to determine if they are equal.\r
303          * @param x A SqlDouble instance\r
304          * @param y A SqlDouble instance\r
305          * @return A SqlBoolean that is True if the two instances are not equal or False if the two instances are equal.\r
306          * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.\r
307          */\r
308         public static SqlBoolean NotEquals(SqlDouble x, SqlDouble y)\r
309         {\r
310             SqlBoolean res = Equals(x, y);\r
311 \r
312             if (res.IsNull)\r
313                 return res;\r
314             if (res.IsFalse)\r
315                 return SqlBoolean.True;\r
316 \r
317             return SqlBoolean.False;\r
318         }\r
319 \r
320         /**\r
321          * Converts the String representation of a number to its double-precision floating point number equivalent.\r
322          * @param s The String to be parsed.\r
323          * @return A SqlDouble containing the value represented by the String.\r
324          */\r
325         public static SqlDouble Parse(String s)\r
326         {\r
327             double d = double.Parse(s);\r
328             return new SqlDouble(d);\r
329         }\r
330 \r
331         /**\r
332          * The subtraction operator the second SqlDouble operand from the first.\r
333          * @param x A SqlDouble instance\r
334          * @param y A SqlDouble instance\r
335          * @return The results of the subtraction operation.\r
336          */\r
337         public static SqlDouble Subtract(SqlDouble x, SqlDouble y)\r
338         {\r
339             if (x.IsNull || y.IsNull)\r
340                 return SqlDouble.Null;\r
341 \r
342             double xVal = x._value;\r
343             double yVal = y._value;\r
344 \r
345             return new SqlDouble(checked(xVal - yVal));\r
346         }\r
347 \r
348         /**\r
349          * Converts this SqlDouble structure to SqlBoolean.\r
350          * @return A SqlBoolean structure whose Value will be True if the SqlDouble structure's Value is non-zero, False if the SqlDouble is zero\r
351          * and Null if the SqlDouble structure is Null.\r
352          */\r
353         public SqlBoolean ToSqlBoolean()\r
354         {\r
355             if (IsNull)\r
356                 return SqlBoolean.Null;\r
357 \r
358             if (_value == 0)\r
359                 return new SqlBoolean(0);\r
360 \r
361             return new SqlBoolean(1);\r
362         }\r
363 \r
364         /**\r
365          * Converts this SqlDouble structure to SqlByte.\r
366          * @return A SqlByte structure whose Value equals the Value of this SqlDouble structure.\r
367          */\r
368         public SqlByte ToSqlByte()\r
369         {\r
370             if (IsNull)\r
371                 return SqlByte.Null;\r
372 \r
373             return new SqlByte(checked((byte)_value));\r
374         }\r
375 \r
376         /**\r
377          * Converts this SqlDouble structure to SqlDecimal.\r
378          * @return A SqlDecimal structure whose Value equals the Value of this SqlDouble structure.\r
379          */\r
380         public SqlDecimal ToSqlDecimal()\r
381         {\r
382             if (IsNull)\r
383                 return SqlDecimal.Null;\r
384 \r
385             return new SqlDecimal(_value);\r
386         }\r
387 \r
388         /**\r
389          * Converts this SqlDouble structure to SqlInt16.\r
390          * @return A SqlInt16 structure whose Value equals the Value of this SqlDouble structure.\r
391          */\r
392         public SqlInt16 ToSqlInt16()\r
393         {\r
394             if (IsNull)\r
395                 return SqlInt16.Null;\r
396 \r
397             return new SqlInt16(checked((short)_value));\r
398         }\r
399 \r
400         /**\r
401          * Converts this SqlDouble structure to SqlInt32.\r
402          * @return A SqlInt32 structure whose Value equals the Value of this SqlDouble structure.\r
403          */\r
404         public SqlInt32 ToSqlInt32()\r
405         {\r
406             if (IsNull)\r
407                 return SqlInt32.Null;\r
408 \r
409             return new SqlInt32(checked((int)_value));\r
410         }\r
411 \r
412         /**\r
413          * Converts this SqlDouble structure to SqlInt64.\r
414          * @return A SqlInt64 structure whose Value equals the Value of this SqlDouble structure.\r
415          */\r
416         public SqlInt64 ToSqlInt64()\r
417         {\r
418             if (IsNull)\r
419                 return SqlInt64.Null;\r
420 \r
421             return new SqlInt64(checked((long)_value));\r
422         }\r
423 \r
424         /**\r
425          * Converts this SqlDouble instance to SqlDouble.\r
426          * @return A SqlMoney instance whose Value equals the Value of this SqlDouble instance.\r
427          */\r
428         public SqlMoney ToSqlMoney()\r
429         {\r
430             if (IsNull)\r
431                 return SqlMoney.Null;\r
432 \r
433             return new SqlMoney(_value);\r
434         }\r
435 \r
436         /**\r
437          * Converts this SqlMoney instance to SqlSingle.\r
438          * @return A SqlSingle instance whose Value equals the Value of this SqlMoney instance.\r
439          */\r
440         public SqlSingle ToSqlSingle()\r
441         {\r
442             if (IsNull)\r
443                 return SqlSingle.Null;\r
444 \r
445             return new SqlSingle(checked((float)_value));\r
446         }\r
447 \r
448 \r
449         /**\r
450          * Converts this SqlDouble structure to SqlString.\r
451          * @return A SqlString structure whose value is a string representing the date and time contained in this SqlDouble structure.\r
452          */\r
453         public SqlString ToSqlString()\r
454         {\r
455             return new SqlString(ToString());\r
456         }\r
457 \r
458         public override String ToString()\r
459         {\r
460             if (IsNull)\r
461                 return "null";\r
462 \r
463 \r
464             return _value.ToString();\r
465         }\r
466 \r
467         public override int GetHashCode()\r
468         {\r
469             return _value.GetHashCode();\r
470         }\r
471 \r
472         public static SqlDouble operator + (SqlDouble x, SqlDouble y)\r
473         {\r
474             double d = 0;\r
475             d = x.Value + y.Value;\r
476                         \r
477             if (Double.IsInfinity (d)) \r
478                 throw new OverflowException ();\r
479 \r
480             return new SqlDouble (d);\r
481         }\r
482 \r
483         public static SqlDouble operator / (SqlDouble x, SqlDouble y)\r
484         {\r
485             double d = x.Value / y.Value;\r
486 \r
487             if (Double.IsInfinity (d)) \r
488             {\r
489                 if (y.Value == 0) \r
490                     throw new DivideByZeroException ();\r
491             }\r
492                                 \r
493             return new SqlDouble (d);\r
494         }\r
495 \r
496         public static SqlBoolean operator == (SqlDouble x, SqlDouble y)\r
497         {\r
498             if (x.IsNull || y.IsNull)   \r
499                 return SqlBoolean.Null;\r
500             else\r
501                 return new SqlBoolean (x.Value == y.Value);\r
502         }\r
503 \r
504         public static SqlBoolean operator > (SqlDouble x, SqlDouble y)\r
505         {\r
506             if (x.IsNull || y.IsNull) \r
507                 return SqlBoolean.Null;\r
508             else\r
509                 return new SqlBoolean (x.Value > y.Value);\r
510         }\r
511 \r
512         public static SqlBoolean operator >= (SqlDouble x, SqlDouble y)\r
513         {\r
514             if (x.IsNull || y.IsNull) \r
515                 return SqlBoolean.Null;\r
516             else\r
517                 return new SqlBoolean (x.Value >= y.Value);\r
518         }\r
519 \r
520         public static SqlBoolean operator != (SqlDouble x, SqlDouble y)\r
521         {\r
522             if (x.IsNull || y.IsNull) \r
523                 return SqlBoolean.Null;\r
524             else\r
525                 return new SqlBoolean (!(x.Value == y.Value));\r
526         }\r
527 \r
528         public static SqlBoolean operator < (SqlDouble x, SqlDouble y)\r
529         {\r
530             if (x.IsNull || y.IsNull) \r
531                 return SqlBoolean.Null;\r
532             else\r
533                 return new SqlBoolean (x.Value < y.Value);\r
534         }\r
535 \r
536         public static SqlBoolean operator <= (SqlDouble x, SqlDouble y)\r
537         {\r
538             if (x.IsNull || y.IsNull) \r
539                 return SqlBoolean.Null;\r
540             else\r
541                 return new SqlBoolean (x.Value <= y.Value);\r
542         }\r
543 \r
544         public static SqlDouble operator * (SqlDouble x, SqlDouble y)\r
545         {\r
546             double d = x.Value * y.Value;\r
547                         \r
548             if (Double.IsInfinity (d))\r
549                 throw new OverflowException ();\r
550 \r
551             return new SqlDouble (d);\r
552 \r
553         }\r
554 \r
555         public static SqlDouble operator - (SqlDouble x, SqlDouble y)\r
556         {\r
557             double d = x.Value - y.Value;\r
558                         \r
559             if (Double.IsInfinity (d))\r
560                 throw new OverflowException ();\r
561 \r
562             return new SqlDouble (d);\r
563         }\r
564 \r
565         public static SqlDouble operator - (SqlDouble n)\r
566         {                       \r
567             return new SqlDouble (-(n.Value));\r
568         }\r
569 \r
570         public static explicit operator SqlDouble (SqlBoolean x)\r
571         {\r
572             if (x.IsNull) \r
573                 return Null;\r
574             else\r
575                 return new SqlDouble ((double)x.ByteValue);\r
576         }\r
577 \r
578         public static explicit operator double (SqlDouble x)\r
579         {\r
580             return x.Value;\r
581         }\r
582 \r
583         public static explicit operator SqlDouble (SqlString x)\r
584         {\r
585             checked \r
586             {\r
587                 return SqlDouble.Parse (x.Value);\r
588             }\r
589         }\r
590 \r
591         public static implicit operator SqlDouble (double x)\r
592         {\r
593             return new SqlDouble (x);\r
594         }\r
595 \r
596         public static implicit operator SqlDouble (SqlByte x)\r
597         {\r
598             if (x.IsNull) \r
599                 return Null;\r
600             else\r
601                 return new SqlDouble ((double)x.Value);\r
602         }\r
603 \r
604         public static implicit operator SqlDouble (SqlDecimal x)\r
605         {\r
606             if (x.IsNull) \r
607                 return Null;\r
608             else\r
609                 return new SqlDouble (x.ToDouble ());\r
610         }\r
611 \r
612         public static implicit operator SqlDouble (SqlInt16 x)\r
613         {\r
614             if (x.IsNull) \r
615                 return Null;\r
616             else\r
617                 return new SqlDouble ((double)x.Value);\r
618         }\r
619 \r
620         public static implicit operator SqlDouble (SqlInt32 x)\r
621         {\r
622             if (x.IsNull) \r
623                 return Null;\r
624             else\r
625                 return new SqlDouble ((double)x.Value);\r
626         }\r
627 \r
628         public static implicit operator SqlDouble (SqlInt64 x)\r
629         {\r
630             if (x.IsNull) \r
631                 return Null;\r
632             else\r
633                 return new SqlDouble ((double)x.Value);\r
634         }\r
635 \r
636         public static implicit operator SqlDouble (SqlMoney x)\r
637         {\r
638             if (x.IsNull) \r
639                 return Null;\r
640             else\r
641                 return new SqlDouble ((double)x.Value);\r
642         }\r
643 \r
644         public static implicit operator SqlDouble (SqlSingle x)\r
645         {\r
646             if (x.IsNull) \r
647                 return Null;\r
648             else\r
649                 return new SqlDouble ((double)x.Value);\r
650         }\r
651    }\r
652 }