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