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