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