2002-11-13 Tim Coleman <tim@timcoleman.com>
[mono.git] / mcs / class / Mono.Data.TdsClient / Mono.Data.TdsTypes / TdsInt32.cs
1 //
2 // Mono.Data.TdsTypes.TdsInt32
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 TdsInt32 : INullable, IComparable 
17         {
18                 #region Fields
19
20                 int value;
21                 private bool notNull;
22
23                 public static readonly TdsInt32 MaxValue = new TdsInt32 (2147483647);
24                 public static readonly TdsInt32 MinValue = new TdsInt32 (-2147483648);
25                 public static readonly TdsInt32 Null;
26                 public static readonly TdsInt32 Zero = new TdsInt32 (0);
27
28                 #endregion
29
30                 #region Constructors
31
32                 public TdsInt32(int 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 int 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 TdsInt32 Add (TdsInt32 x, TdsInt32 y) 
60                 {
61                         return (x + y);
62                 }
63
64                 public static TdsInt32 BitwiseAnd(TdsInt32 x, TdsInt32 y) 
65                 {
66                         return (x & y);
67                 }
68                 
69                 public static TdsInt32 BitwiseOr(TdsInt32 x, TdsInt32 y) 
70                 {
71                         return (x | y);
72                 }
73
74                 public int CompareTo(object value) 
75                 {
76                         if (value == null)
77                                 return 1;
78                         else if (!(value is TdsInt32))
79                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.TdsTypes.TdsInt32"));
80                         else if (((TdsInt32)value).IsNull)
81                                 return 1;
82                         else
83                                 return this.value.CompareTo (((TdsInt32)value).Value);
84                 }
85
86                 public static TdsInt32 Divide(TdsInt32 x, TdsInt32 y) 
87                 {
88                         return (x / y);
89                 }
90
91                 public override bool Equals(object value) 
92                 {
93                         if (!(value is TdsInt32))
94                                 return false;
95                         else
96                                 return (bool) (this == (TdsInt32)value);
97                 }
98
99                 public static TdsBoolean Equals(TdsInt32 x, TdsInt32 y) 
100                 {
101                         return (x == y);
102                 }
103
104                 public override int GetHashCode() 
105                 {
106                         return value;
107                 }
108
109                 public static TdsBoolean GreaterThan (TdsInt32 x, TdsInt32 y) 
110                 {
111                         return (x > y);
112                 }
113
114                 public static TdsBoolean GreaterThanOrEqual (TdsInt32 x, TdsInt32 y) 
115                 {
116                         return (x >= y);
117                 }
118                 
119                 public static TdsBoolean LessThan(TdsInt32 x, TdsInt32 y) 
120                 {
121                         return (x < y);
122                 }
123
124                 public static TdsBoolean LessThanOrEqual(TdsInt32 x, TdsInt32 y) 
125                 {
126                         return (x <= y);
127                 }
128
129                 public static TdsInt32 Mod(TdsInt32 x, TdsInt32 y) 
130                 {
131                         return (x % y);
132                 }
133
134                 public static TdsInt32 Multiply(TdsInt32 x, TdsInt32 y) 
135                 {
136                         return (x * y);
137                 }
138
139                 public static TdsBoolean NotEquals(TdsInt32 x, TdsInt32 y) 
140                 {
141                         return (x != y);
142                 }
143
144                 public static TdsInt32 OnesComplement(TdsInt32 x) 
145                 {
146                         return ~x;
147                 }
148
149                 public static TdsInt32 Parse(string s) 
150                 {
151                         return new TdsInt32 (Int32.Parse (s));
152                 }
153
154                 public static TdsInt32 Subtract(TdsInt32 x, TdsInt32 y) 
155                 {
156                         return (x - y);
157                 }
158
159                 public TdsBoolean ToTdsBoolean() 
160                 {
161                         return ((TdsBoolean)this);
162                 }
163
164                 public TdsByte ToTdsByte() 
165                 {
166                         return ((TdsByte)this);
167                 }
168
169                 public TdsDecimal ToTdsDecimal() 
170                 {
171                         return ((TdsDecimal)this);
172                 }
173
174                 public TdsDouble ToTdsDouble()  
175                 {
176                         return ((TdsDouble)this);
177                 }
178
179                 public TdsInt16 ToTdsInt16() 
180                 {
181                         return ((TdsInt16)this);
182                 }
183
184                 public TdsInt64 ToTdsInt64() 
185                 {
186                         return ((TdsInt64)this);
187                 }
188
189                 public TdsMoney ToTdsMoney() 
190                 {
191                         return ((TdsMoney)this);
192                 }
193
194                 public TdsSingle ToTdsSingle() 
195                 {
196                         return ((TdsSingle)this);
197                 }
198
199                 public TdsString ToTdsString ()
200                 {
201                         return ((TdsString)this);
202                 }
203
204                 public override string ToString() 
205                 {
206                         if (this.IsNull)
207                                 return "Null";
208                         else
209                                 return value.ToString ();
210                 }
211
212                 public static TdsInt32 Xor(TdsInt32 x, TdsInt32 y) 
213                 {
214                         return (x ^ y);
215                 }
216
217                 #endregion
218
219                 #region Operators
220
221                 // Compute Addition
222                 public static TdsInt32 operator + (TdsInt32 x, TdsInt32 y) 
223                 {
224                         return new TdsInt32 (x.Value + y.Value);
225                 }
226
227                 // Bitwise AND
228                 public static TdsInt32 operator & (TdsInt32 x, TdsInt32 y) 
229                 {
230                         return new TdsInt32 (x.Value & y.Value);
231                 }
232
233                 // Bitwise OR
234                 public static TdsInt32 operator | (TdsInt32 x, TdsInt32 y) 
235                 {
236                         return new TdsInt32 (x.Value | y.Value);
237                 }
238
239                 // Compute Division
240                 public static TdsInt32 operator / (TdsInt32 x, TdsInt32 y) 
241                 {
242                         return new TdsInt32 (x.Value / y.Value);
243                 }
244
245                 // Compare Equality
246                 public static TdsBoolean operator == (TdsInt32 x, TdsInt32 y) 
247                 {
248                         if (x.IsNull || y.IsNull) 
249                                 return TdsBoolean.Null;
250                         else
251                                 return new TdsBoolean (x.Value == y.Value);
252                 }
253
254                 // Bitwise Exclusive-OR (XOR)
255                 public static TdsInt32 operator ^ (TdsInt32 x, TdsInt32 y) 
256                 {
257                         return new TdsInt32 (x.Value ^ y.Value);
258                 }
259
260                 // > Compare
261                 public static TdsBoolean operator >(TdsInt32 x, TdsInt32 y) 
262                 {
263                         if (x.IsNull || y.IsNull) 
264                                 return TdsBoolean.Null;
265                         else
266                                 return new TdsBoolean (x.Value > y.Value);
267                 }
268
269                 // >= Compare
270                 public static TdsBoolean operator >= (TdsInt32 x, TdsInt32 y) 
271                 {
272                         if (x.IsNull || y.IsNull) 
273                                 return TdsBoolean.Null;
274                         else
275                                 return new TdsBoolean (x.Value >= y.Value);
276                 }
277
278                 // != Inequality Compare
279                 public static TdsBoolean operator != (TdsInt32 x, TdsInt32 y) 
280                 {
281                         if (x.IsNull || y.IsNull) 
282                                 return TdsBoolean.Null;
283                         else
284                                 return new TdsBoolean (x.Value != y.Value);
285                 }
286                 
287                 // < Compare
288                 public static TdsBoolean operator < (TdsInt32 x, TdsInt32 y) 
289                 {
290                         if (x.IsNull || y.IsNull) 
291                                 return TdsBoolean.Null;
292                         else
293                                 return new TdsBoolean (x.Value < y.Value);
294                 }
295
296                 // <= Compare
297                 public static TdsBoolean operator <= (TdsInt32 x, TdsInt32 y) 
298                 {
299                         if (x.IsNull || y.IsNull) 
300                                 return TdsBoolean.Null;
301                         else
302                                 return new TdsBoolean (x.Value <= y.Value);
303                 }
304
305                 // Compute Modulus
306                 public static TdsInt32 operator % (TdsInt32 x, TdsInt32 y) 
307                 {
308                         return new TdsInt32 (x.Value % y.Value);
309                 }
310
311                 // Compute Multiplication
312                 public static TdsInt32 operator * (TdsInt32 x, TdsInt32 y) 
313                 {
314                         return new TdsInt32 (x.Value * y.Value);
315                 }
316
317                 // Ones Complement
318                 public static TdsInt32 operator ~ (TdsInt32 x) 
319                 {
320                         return new TdsInt32 (~x.Value);
321                 }
322
323                 // Subtraction
324                 public static TdsInt32 operator - (TdsInt32 x, TdsInt32 y) 
325                 {
326                         return new TdsInt32 (x.Value - y.Value);
327                 }
328
329                 // Negates the Value
330                 public static TdsInt32 operator - (TdsInt32 x) 
331                 {
332                         return new TdsInt32 (-x.Value);
333                 }
334
335                 // Type Conversions
336                 public static explicit operator TdsInt32 (TdsBoolean x) 
337                 {
338                         if (x.IsNull) 
339                                 return Null;
340                         else 
341                                 return new TdsInt32 ((int)x.ByteValue);
342                 }
343
344                 public static explicit operator TdsInt32 (TdsDecimal x) 
345                 {
346                         if (x.IsNull) 
347                                 return Null;
348                         else 
349                                 return new TdsInt32 ((int)x.Value);
350                 }
351
352                 public static explicit operator TdsInt32 (TdsDouble x) 
353                 {
354                         if (x.IsNull) 
355                                 return Null;
356                         else 
357                                 return new TdsInt32 ((int)x.Value);
358                 }
359
360                 public static explicit operator int (TdsInt32 x)
361                 {
362                         return x.Value;
363                 }
364
365                 public static explicit operator TdsInt32 (TdsInt64 x) 
366                 {
367                         if (x.IsNull) 
368                                 return Null;
369                         else 
370                                 return new TdsInt32 ((int)x.Value);
371                 }
372
373                 public static explicit operator TdsInt32(TdsMoney x) 
374                 {
375                         if (x.IsNull) 
376                                 return Null;
377                         else 
378                                 return new TdsInt32 ((int)x.Value);
379                 }
380
381                 public static explicit operator TdsInt32(TdsSingle x) 
382                 {
383                         if (x.IsNull) 
384                                 return Null;
385                         else 
386                                 return new TdsInt32 ((int)x.Value);
387                 }
388
389                 public static explicit operator TdsInt32(TdsString x) 
390                 {
391                         return TdsInt32.Parse (x.Value);
392                 }
393
394                 public static implicit operator TdsInt32(int x) 
395                 {
396                         return new TdsInt32 (x);
397                 }
398
399                 public static implicit operator TdsInt32(TdsByte x) 
400                 {
401                         if (x.IsNull) 
402                                 return Null;
403                         else 
404                                 return new TdsInt32 ((int)x.Value);
405                 }
406
407                 public static implicit operator TdsInt32(TdsInt16 x) 
408                 {
409                         if (x.IsNull) 
410                                 return Null;
411                         else 
412                                 return new TdsInt32 ((int)x.Value);
413                 }
414
415                 #endregion
416         }
417 }