get Miguel's patch back in, without cloning the serialization info
[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 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using Mono.Data.TdsClient;
32 using System;
33 using System.Data.SqlTypes;
34 using System.Globalization;
35
36 namespace Mono.Data.TdsTypes {
37         public struct TdsDouble : INullable, IComparable
38         {
39                 #region Fields
40                 double value;
41
42                 private bool notNull;
43
44                 public static readonly TdsDouble MaxValue = new TdsDouble (1.7976931348623157e308);
45                 public static readonly TdsDouble MinValue = new TdsDouble (-1.7976931348623157e308);
46                 public static readonly TdsDouble Null;
47                 public static readonly TdsDouble Zero = new TdsDouble (0);
48
49                 #endregion
50
51                 #region Constructors
52
53                 public TdsDouble (double value) 
54                 {
55                         this.value = value;
56                         notNull = true;
57                 }
58
59                 #endregion
60
61                 #region Properties
62
63                 public bool IsNull { 
64                         get { return !notNull; }
65                 }
66
67                 public double Value { 
68                         get { 
69                                 if (this.IsNull) 
70                                         throw new TdsNullValueException ();
71                                 else 
72                                         return value; 
73                         }
74                 }
75
76                 #endregion
77
78                 #region Methods
79
80                 public static TdsDouble Add (TdsDouble x, TdsDouble y)
81                 {
82                         return (x + y);
83                 }
84
85                 public int CompareTo (object value)
86                 {
87                         if (value == null)
88                                 return 1;
89                         else if (!(value is TdsDouble))
90                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.TdsTypes.TdsDouble"));
91                         else if (((TdsDouble)value).IsNull)
92                                 return 1;
93                         else
94                                 return this.value.CompareTo (((TdsDouble)value).Value);
95                 }
96
97                 public static TdsDouble Divide (TdsDouble x, TdsDouble y)
98                 {
99                         return (x / y);
100                 }
101
102                 public override bool Equals (object value)
103                 {
104                         if (!(value is TdsDouble))
105                                 return false;
106                         else
107                                 return (bool) (this == (TdsDouble)value);
108                 }
109
110                 public static TdsBoolean Equals (TdsDouble x, TdsDouble y)
111                 {
112                         return (x == y);
113                 }
114
115                 public override int GetHashCode ()
116                 {
117                         long LongValue = (long)value;
118                         return (int)(LongValue ^ (LongValue >> 32));
119                         
120                 }
121
122                 public static TdsBoolean GreaterThan (TdsDouble x, TdsDouble y)
123                 {
124                         return (x > y);
125                 }
126
127                 public static TdsBoolean GreaterThanOrEqual (TdsDouble x, TdsDouble y)
128                 {
129                         return (x >= y);
130                 }
131
132                 public static TdsBoolean LessThan (TdsDouble x, TdsDouble y)
133                 {
134                         return (x < y);
135                 }
136
137                 public static TdsBoolean LessThanOrEqual (TdsDouble x, TdsDouble y)
138                 {
139                         return (x <= y);
140                 }
141
142                 public static TdsDouble Multiply (TdsDouble x, TdsDouble y)
143                 {
144                         return (x * y);
145                 }
146
147                 public static TdsBoolean NotEquals (TdsDouble x, TdsDouble y)
148                 {
149                         return (x != y);
150                 }
151
152                 public static TdsDouble Parse (string s)
153                 {
154                         return new TdsDouble (Double.Parse (s));
155                 }
156
157                 public static TdsDouble Subtract (TdsDouble x, TdsDouble y)
158                 {
159                         return (x - y);
160                 }
161
162                 public TdsBoolean ToTdsBoolean ()
163                 {
164                         return ((TdsBoolean)this);      
165                 }
166                 
167                 public TdsByte ToTdsByte ()
168                 {
169                         return ((TdsByte)this); 
170                 }
171
172                 public TdsDecimal ToTdsDecimal ()
173                 {
174                         return ((TdsDecimal)this);      
175                 }
176
177                 public TdsInt16 ToTdsInt16 ()
178                 {
179                         return ((TdsInt16)this);
180                 }
181
182                 public TdsInt32 ToTdsInt32 ()
183                 {
184                         return ((TdsInt32)this);
185                 }
186
187                 public TdsInt64 ToTdsInt64 ()
188                 {
189                         return ((TdsInt64)this);
190                 }
191
192                 public TdsMoney ToTdsMoney ()
193                 {
194                         return ((TdsMoney)this);
195                 }
196
197                 public TdsSingle ToTdsSingle ()
198                 {
199                         return ((TdsSingle)this);
200                 }
201
202                 public TdsString ToTdsString ()
203                 {
204                         return ((TdsString)this);
205                 }
206
207                 public override string ToString ()
208                 {
209                         if (this.IsNull)
210                                 return String.Empty;
211                         else
212                                 return value.ToString ();
213                 }
214
215                 public static TdsDouble operator + (TdsDouble x, TdsDouble y)
216                 {
217                         return new TdsDouble (x.Value + y.Value);
218                 }
219
220                 public static TdsDouble operator / (TdsDouble x, TdsDouble y)
221                 {
222                         return new TdsDouble (x.Value / y.Value);
223                 }
224
225                 public static TdsBoolean operator == (TdsDouble x, TdsDouble y)
226                 {
227                         if (x.IsNull || y.IsNull)       
228                                 return TdsBoolean.Null;
229                         else
230                                 return new TdsBoolean (x.Value == y.Value);
231                 }
232
233                 public static TdsBoolean operator > (TdsDouble x, TdsDouble y)
234                 {
235                         if (x.IsNull || y.IsNull) 
236                                 return TdsBoolean.Null;
237                         else
238                                 return new TdsBoolean (x.Value > y.Value);
239                 }
240
241                 public static TdsBoolean operator >= (TdsDouble x, TdsDouble y)
242                 {
243                         if (x.IsNull || y.IsNull) 
244                                 return TdsBoolean.Null;
245                         else
246                                 return new TdsBoolean (x.Value >= y.Value);
247                 }
248
249                 public static TdsBoolean operator != (TdsDouble x, TdsDouble y)
250                 {
251                         if (x.IsNull || y.IsNull) 
252                                 return TdsBoolean.Null;
253                         else
254                                 return new TdsBoolean (!(x.Value == y.Value));
255                 }
256
257                 public static TdsBoolean operator < (TdsDouble x, TdsDouble y)
258                 {
259                         if (x.IsNull || y.IsNull) 
260                                 return TdsBoolean.Null;
261                         else
262                                 return new TdsBoolean (x.Value < y.Value);
263                 }
264
265                 public static TdsBoolean operator <= (TdsDouble x, TdsDouble y)
266                 {
267                         if (x.IsNull || y.IsNull) 
268                                 return TdsBoolean.Null;
269                         else
270                                 return new TdsBoolean (x.Value <= y.Value);
271                 }
272
273                 public static TdsDouble operator * (TdsDouble x, TdsDouble y)
274                 {
275                         return new TdsDouble (x.Value * y.Value);
276                 }
277
278                 public static TdsDouble operator - (TdsDouble x, TdsDouble y)
279                 {
280                         return new TdsDouble (x.Value - y.Value);
281                 }
282
283                 public static TdsDouble operator - (TdsDouble n)
284                 {
285                         return new TdsDouble (-(n.Value));
286                 }
287
288                 public static explicit operator TdsDouble (TdsBoolean x)
289                 {
290                         if (x.IsNull) 
291                                 return Null;
292                         else
293                                 return new TdsDouble ((double)x.ByteValue);
294                 }
295
296                 public static explicit operator double (TdsDouble x)
297                 {
298                         return x.Value;
299                 }
300
301                 public static explicit operator TdsDouble (TdsString x)
302                 {
303                         return TdsDouble.Parse (x.Value);
304                 }
305
306                 public static implicit operator TdsDouble (double x)
307                 {
308                         return new TdsDouble (x);
309                 }
310
311                 public static implicit operator TdsDouble (TdsByte x)
312                 {
313                         if (x.IsNull) 
314                                 return Null;
315                         else
316                                 return new TdsDouble ((double)x.Value);
317                 }
318
319                 public static implicit operator TdsDouble (TdsDecimal x)
320                 {
321                         if (x.IsNull) 
322                                 return Null;
323                         else
324                                 return new TdsDouble ((double)x.Value);
325                 }
326
327                 public static implicit operator TdsDouble (TdsInt16 x)
328                 {
329                         if (x.IsNull) 
330                                 return Null;
331                         else
332                                 return new TdsDouble ((double)x.Value);
333                 }
334
335                 public static implicit operator TdsDouble (TdsInt32 x)
336                 {
337                         if (x.IsNull) 
338                                 return Null;
339                         else
340                                 return new TdsDouble ((double)x.Value);
341                 }
342
343                 public static implicit operator TdsDouble (TdsInt64 x)
344                 {
345                         if (x.IsNull) 
346                                 return Null;
347                         else
348                                 return new TdsDouble ((double)x.Value);
349                 }
350
351                 public static implicit operator TdsDouble (TdsMoney x)
352                 {
353                         if (x.IsNull) 
354                                 return Null;
355                         else
356                                 return new TdsDouble ((double)x.Value);
357                 }
358
359                 public static implicit operator TdsDouble (TdsSingle x)
360                 {
361                         if (x.IsNull) 
362                                 return Null;
363                         else
364                                 return new TdsDouble ((double)x.Value);
365                 }
366
367                 #endregion
368         }
369 }
370