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