c09d6607d029fca51956b3662393b657a60b8b27
[mono.git] / mcs / class / Mono.Data.TdsClient / Mono.Data.TdsTypes / TdsDouble.cs
1 //
2 // Mono.Data.TdsTypes.TdsDouble
3 //
4 // Author:
5 //   Tim Coleman <tim@timcoleman.com>
6 //
7 // (C) Copyright Tim Coleman, 2002
8 //
9
10 using Mono.Data.TdsClient;
11 using System;
12 using System.Data.SqlTypes;
13 using System.Globalization;
14
15 namespace Mono.Data.TdsTypes {
16         public struct TdsDouble : INullable, IComparable
17         {
18                 #region Fields
19                 double value;
20
21                 private bool notNull;
22
23                 public static readonly TdsDouble MaxValue = new TdsDouble (1.7976931348623157e308);
24                 public static readonly TdsDouble MinValue = new TdsDouble (-1.7976931348623157e308);
25                 public static readonly TdsDouble Null;
26                 public static readonly TdsDouble Zero = new TdsDouble (0);
27
28                 #endregion
29
30                 #region Constructors
31
32                 public TdsDouble (double value) 
33                 {
34                         this.value = value;
35                         notNull = true;
36                 }
37
38                 #endregion
39
40                 #region Properties
41
42                 public bool IsNull { 
43                         get { return !notNull; }
44                 }
45
46                 public double Value { 
47                         get { 
48                                 if (this.IsNull) 
49                                         throw new TdsNullValueException ();
50                                 else 
51                                         return value; 
52                         }
53                 }
54
55                 #endregion
56
57                 #region Methods
58
59                 public static TdsDouble Add (TdsDouble x, TdsDouble y)
60                 {
61                         return (x + y);
62                 }
63
64                 public int CompareTo (object value)
65                 {
66                         if (value == null)
67                                 return 1;
68                         else if (!(value is TdsDouble))
69                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.TdsTypes.TdsDouble"));
70                         else if (((TdsDouble)value).IsNull)
71                                 return 1;
72                         else
73                                 return this.value.CompareTo (((TdsDouble)value).Value);
74                 }
75
76                 public static TdsDouble Divide (TdsDouble x, TdsDouble y)
77                 {
78                         return (x / y);
79                 }
80
81                 public override bool Equals (object value)
82                 {
83                         if (!(value is TdsDouble))
84                                 return false;
85                         else
86                                 return (bool) (this == (TdsDouble)value);
87                 }
88
89                 public static TdsBoolean Equals (TdsDouble x, TdsDouble y)
90                 {
91                         return (x == y);
92                 }
93
94                 public override int GetHashCode ()
95                 {
96                         long LongValue = (long)value;
97                         return (int)(LongValue ^ (LongValue >> 32));
98                         
99                 }
100
101                 public static TdsBoolean GreaterThan (TdsDouble x, TdsDouble y)
102                 {
103                         return (x > y);
104                 }
105
106                 public static TdsBoolean GreaterThanOrEqual (TdsDouble x, TdsDouble y)
107                 {
108                         return (x >= y);
109                 }
110
111                 public static TdsBoolean LessThan (TdsDouble x, TdsDouble y)
112                 {
113                         return (x < y);
114                 }
115
116                 public static TdsBoolean LessThanOrEqual (TdsDouble x, TdsDouble y)
117                 {
118                         return (x <= y);
119                 }
120
121                 public static TdsDouble Multiply (TdsDouble x, TdsDouble y)
122                 {
123                         return (x * y);
124                 }
125
126                 public static TdsBoolean NotEquals (TdsDouble x, TdsDouble y)
127                 {
128                         return (x != y);
129                 }
130
131                 public static TdsDouble Parse (string s)
132                 {
133                         return new TdsDouble (Double.Parse (s));
134                 }
135
136                 public static TdsDouble Subtract (TdsDouble x, TdsDouble y)
137                 {
138                         return (x - y);
139                 }
140
141                 public TdsBoolean ToTdsBoolean ()
142                 {
143                         return ((TdsBoolean)this);      
144                 }
145                 
146                 public TdsByte ToTdsByte ()
147                 {
148                         return ((TdsByte)this); 
149                 }
150
151                 public TdsDecimal ToTdsDecimal ()
152                 {
153                         return ((TdsDecimal)this);      
154                 }
155
156                 public TdsInt16 ToTdsInt16 ()
157                 {
158                         return ((TdsInt16)this);
159                 }
160
161                 public TdsInt32 ToTdsInt32 ()
162                 {
163                         return ((TdsInt32)this);
164                 }
165
166                 public TdsInt64 ToTdsInt64 ()
167                 {
168                         return ((TdsInt64)this);
169                 }
170
171                 public TdsMoney ToTdsMoney ()
172                 {
173                         return ((TdsMoney)this);
174                 }
175
176                 public TdsSingle ToTdsSingle ()
177                 {
178                         return ((TdsSingle)this);
179                 }
180
181                 public TdsString ToTdsString ()
182                 {
183                         return ((TdsString)this);
184                 }
185
186                 public override string ToString ()
187                 {
188                         if (this.IsNull)
189                                 return String.Empty;
190                         else
191                                 return value.ToString ();
192                 }
193
194                 public static TdsDouble operator + (TdsDouble x, TdsDouble y)
195                 {
196                         return new TdsDouble (x.Value + y.Value);
197                 }
198
199                 public static TdsDouble operator / (TdsDouble x, TdsDouble y)
200                 {
201                         return new TdsDouble (x.Value / y.Value);
202                 }
203
204                 public static TdsBoolean operator == (TdsDouble x, TdsDouble y)
205                 {
206                         if (x.IsNull || y.IsNull)       
207                                 return TdsBoolean.Null;
208                         else
209                                 return new TdsBoolean (x.Value == y.Value);
210                 }
211
212                 public static TdsBoolean operator > (TdsDouble x, TdsDouble y)
213                 {
214                         if (x.IsNull || y.IsNull) 
215                                 return TdsBoolean.Null;
216                         else
217                                 return new TdsBoolean (x.Value > y.Value);
218                 }
219
220                 public static TdsBoolean operator >= (TdsDouble x, TdsDouble y)
221                 {
222                         if (x.IsNull || y.IsNull) 
223                                 return TdsBoolean.Null;
224                         else
225                                 return new TdsBoolean (x.Value >= y.Value);
226                 }
227
228                 public static TdsBoolean operator != (TdsDouble x, TdsDouble y)
229                 {
230                         if (x.IsNull || y.IsNull) 
231                                 return TdsBoolean.Null;
232                         else
233                                 return new TdsBoolean (!(x.Value == y.Value));
234                 }
235
236                 public static TdsBoolean operator < (TdsDouble x, TdsDouble y)
237                 {
238                         if (x.IsNull || y.IsNull) 
239                                 return TdsBoolean.Null;
240                         else
241                                 return new TdsBoolean (x.Value < y.Value);
242                 }
243
244                 public static TdsBoolean operator <= (TdsDouble x, TdsDouble y)
245                 {
246                         if (x.IsNull || y.IsNull) 
247                                 return TdsBoolean.Null;
248                         else
249                                 return new TdsBoolean (x.Value <= y.Value);
250                 }
251
252                 public static TdsDouble operator * (TdsDouble x, TdsDouble y)
253                 {
254                         return new TdsDouble (x.Value * y.Value);
255                 }
256
257                 public static TdsDouble operator - (TdsDouble x, TdsDouble y)
258                 {
259                         return new TdsDouble (x.Value - y.Value);
260                 }
261
262                 public static TdsDouble operator - (TdsDouble n)
263                 {
264                         return new TdsDouble (-(n.Value));
265                 }
266
267                 public static explicit operator TdsDouble (TdsBoolean x)
268                 {
269                         if (x.IsNull) 
270                                 return Null;
271                         else
272                                 return new TdsDouble ((double)x.ByteValue);
273                 }
274
275                 public static explicit operator double (TdsDouble x)
276                 {
277                         return x.Value;
278                 }
279
280                 public static explicit operator TdsDouble (TdsString x)
281                 {
282                         return TdsDouble.Parse (x.Value);
283                 }
284
285                 public static implicit operator TdsDouble (double x)
286                 {
287                         return new TdsDouble (x);
288                 }
289
290                 public static implicit operator TdsDouble (TdsByte x)
291                 {
292                         if (x.IsNull) 
293                                 return Null;
294                         else
295                                 return new TdsDouble ((double)x.Value);
296                 }
297
298                 public static implicit operator TdsDouble (TdsDecimal x)
299                 {
300                         if (x.IsNull) 
301                                 return Null;
302                         else
303                                 return new TdsDouble ((double)x.Value);
304                 }
305
306                 public static implicit operator TdsDouble (TdsInt16 x)
307                 {
308                         if (x.IsNull) 
309                                 return Null;
310                         else
311                                 return new TdsDouble ((double)x.Value);
312                 }
313
314                 public static implicit operator TdsDouble (TdsInt32 x)
315                 {
316                         if (x.IsNull) 
317                                 return Null;
318                         else
319                                 return new TdsDouble ((double)x.Value);
320                 }
321
322                 public static implicit operator TdsDouble (TdsInt64 x)
323                 {
324                         if (x.IsNull) 
325                                 return Null;
326                         else
327                                 return new TdsDouble ((double)x.Value);
328                 }
329
330                 public static implicit operator TdsDouble (TdsMoney x)
331                 {
332                         if (x.IsNull) 
333                                 return Null;
334                         else
335                                 return new TdsDouble ((double)x.Value);
336                 }
337
338                 public static implicit operator TdsDouble (TdsSingle x)
339                 {
340                         if (x.IsNull) 
341                                 return Null;
342                         else
343                                 return new TdsDouble ((double)x.Value);
344                 }
345
346                 #endregion
347         }
348 }
349