2002-11-13 Tim Coleman <tim@timcoleman.com>
[mono.git] / mcs / class / Mono.Data.TdsClient / Mono.Data.TdsTypes / TdsBoolean.cs
1 //
2 // Mono.Data.TdsTypes.TdsBoolean
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 TdsBoolean : INullable, IComparable 
17         {
18                 #region Fields
19
20                 byte value;
21                 
22                 // default is false
23                 private bool notNull;
24
25                 public static readonly TdsBoolean False = new TdsBoolean (false);
26                 public static readonly TdsBoolean Null;
27                 public static readonly TdsBoolean One = new TdsBoolean (1);
28                 public static readonly TdsBoolean True = new TdsBoolean (true);
29                 public static readonly TdsBoolean Zero = new TdsBoolean (0);
30
31                 #endregion // Fields
32
33                 #region Constructors
34
35                 public TdsBoolean (bool value) 
36                 {
37                         this.value = (byte) (value ? 1 : 0);
38                         notNull = true;
39                 }
40
41                 public TdsBoolean (int value) 
42                 {
43                         this.value = (byte) (value != 0 ? 1 : 0);
44                         notNull = true;
45                 }
46
47                 #endregion // Constructors
48
49                 #region Properties
50
51                 public byte ByteValue {
52                         get {
53                                 if (this.IsNull)
54                                         throw new TdsNullValueException(Locale.GetText("The property is set to null."));
55                                 else
56                                         return value;
57                         }
58                 }
59
60                 public bool IsFalse {
61                         get { 
62                                 if (this.IsNull) 
63                                         return false;
64                                 else 
65                                         return (value == 0);
66                         }
67                 }
68
69                 public bool IsNull {
70                         get { 
71                                 return !notNull;
72                         }
73                 }
74
75                 public bool IsTrue {
76                         get { 
77                                 if (this.IsNull) 
78                                         return false;
79                                 else    
80                                         return (value != 0);
81                         }
82                 }
83
84                 public bool Value {
85                         get { 
86                                 if (this.IsNull)
87                                         throw new TdsNullValueException(Locale.GetText("The property is set to null."));
88                                 else
89                                         return this.IsTrue;
90                         }
91                 }
92
93                 #endregion // Properties
94
95                 public static TdsBoolean And (TdsBoolean x, TdsBoolean y) 
96                 {
97                         return (x & y);
98                 }
99
100                 public int CompareTo (object value) 
101                 {
102                         if (value == null)
103                                 return 1;
104                         else if (!(value is TdsBoolean))
105                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.TdsTypes.TdsBoolean"));
106                         else if (((TdsBoolean)value).IsNull)
107                                 return 1;
108                         else
109                                 return this.value.CompareTo (((TdsBoolean)value).ByteValue);
110                 }
111
112                 public override bool Equals(object value) 
113                 {
114                         if (!(value is TdsByte))
115                                 return false;
116                         else
117                                 return (bool) (this == (TdsBoolean)value);
118                 }
119
120                 public static TdsBoolean Equals(TdsBoolean x, TdsBoolean y) 
121                 {
122                         return (x == y);
123                 }
124
125                 public override int GetHashCode() 
126                 {
127                         return (int)value;
128                 }
129
130                 public static TdsBoolean NotEquals(TdsBoolean x, TdsBoolean y) 
131                 {
132                         return (x != y);
133                 }
134
135                 public static TdsBoolean OnesComplement(TdsBoolean x) 
136                 {
137                         return ~x;
138                 }
139
140                 public static TdsBoolean Or(TdsBoolean x, TdsBoolean y) 
141                 {
142                         return (x | y);
143                 }
144
145                 public static TdsBoolean Parse(string s) 
146                 {
147                         return new TdsBoolean (Boolean.Parse (s));
148                 }
149
150                 public TdsByte ToTdsByte() 
151                 {
152                         return new TdsByte (value);
153                 }
154
155                 // **************************************************
156                 // Conversion from TdsBoolean to other TdsTypes
157                 // **************************************************
158
159                 public TdsDecimal ToTdsDecimal() 
160                 {
161                         return ((TdsDecimal)this);
162                 }
163
164                 public TdsDouble ToTdsDouble() 
165                 {
166                         return ((TdsDouble)this);
167                 }
168
169                 public TdsInt16 ToTdsInt16() 
170                 {
171                         return ((TdsInt16)this);
172                 }
173
174                 public TdsInt32 ToTdsInt32() 
175                 {
176                         return ((TdsInt32)this);
177                 }
178
179                 public TdsInt64 ToTdsInt64() 
180                 {
181                         return ((TdsInt64)this);
182                 }
183
184                 public TdsMoney ToTdsMoney() 
185                 {
186                         return ((TdsMoney)this);
187                 }
188
189                 public TdsSingle ToTdsSingle() 
190                 {
191                         return ((TdsSingle)this);
192                 }
193
194                 public TdsString ToTdsString() 
195                 {
196                         if (this.IsNull)
197                                 return new TdsString ("Null");
198                         if (this.IsTrue)
199                                 return new TdsString ("True");
200                         else
201                                 return new TdsString ("False");
202                 }
203
204                 public override string ToString() 
205                 {
206                         if (this.IsNull)
207                                 return "Null";
208                         if (this.IsTrue)
209                                 return "True";
210                         else
211                                 return "False";
212                 }
213
214                 // Bitwise exclusive-OR (XOR)
215                 public static TdsBoolean Xor(TdsBoolean x, TdsBoolean y) 
216                 {
217                         return (x ^ y);
218                 }
219
220                 // **************************************************
221                 // Public Operators
222                 // **************************************************
223
224                 // Bitwise AND
225                 public static TdsBoolean operator & (TdsBoolean x, TdsBoolean y)
226                 {
227                         return new TdsBoolean (x.Value & y.Value);
228                 }
229
230                 // Bitwise OR
231                 public static TdsBoolean operator | (TdsBoolean x, TdsBoolean y)
232                 {
233                         return new TdsBoolean (x.Value | y.Value);
234
235                 }
236
237                 // Compares two instances for equality
238                 public static TdsBoolean operator == (TdsBoolean x, TdsBoolean y)
239                 {
240                         if (x.IsNull || y.IsNull) 
241                                 return TdsBoolean.Null;
242                         else
243                                 return new TdsBoolean (x.Value == y.Value);
244                 }
245                 
246                 // Bitwize exclusive-OR (XOR)
247                 public static TdsBoolean operator ^ (TdsBoolean x, TdsBoolean y) 
248                 {
249                         return new TdsBoolean (x.Value ^ y.Value);
250                 }
251
252                 // test Value of TdsBoolean to determine it is false.
253                 public static bool operator false (TdsBoolean x) 
254                 {
255                         return x.IsFalse;
256                 }
257
258                 // in-equality
259                 public static TdsBoolean operator != (TdsBoolean x, TdsBoolean y)
260                 {
261                         if (x.IsNull || y.IsNull) 
262                                 return TdsBoolean.Null;
263                         else
264                                 return new TdsBoolean (x.Value != y.Value);
265                 }
266
267                 // Logical NOT
268                 public static TdsBoolean operator ! (TdsBoolean x) 
269                 {
270                         if (x.IsNull)
271                                 return TdsBoolean.Null;
272                         else
273                                 return new TdsBoolean (!x.Value);
274                 }
275
276                 // One's Complement
277                 public static TdsBoolean operator ~ (TdsBoolean x) 
278                 {
279                         return new TdsBoolean (~x.ByteValue);
280                 }
281
282                 // test to see if value is true
283                 public static bool operator true (TdsBoolean x) 
284                 {
285                         return x.IsTrue;
286                 }
287
288                 // ****************************************
289                 // Type Conversion 
290                 // ****************************************
291
292                 
293                 // TdsBoolean to Boolean
294                 public static explicit operator bool (TdsBoolean x) 
295                 {
296                         return x.Value;
297                 }
298
299                 
300                 // TdsByte to TdsBoolean
301                 public static explicit operator TdsBoolean (TdsByte x) 
302                 {
303                         if (x.IsNull)
304                                 return Null;
305                         else
306                                 return new TdsBoolean ((int)x.Value);
307                 }
308
309                 // TdsDecimal to TdsBoolean
310                 public static explicit operator TdsBoolean (TdsDecimal x) 
311                 {
312                         if (x.IsNull)
313                                 return Null;
314                         else
315                                 return new TdsBoolean ((int)x.Value);
316                 }
317                 
318                 // TdsDouble to TdsBoolean
319                 public static explicit operator TdsBoolean (TdsDouble x) 
320                 {
321                         if (x.IsNull)
322                                 return Null;
323                         else
324                                 return new TdsBoolean ((int)x.Value);
325                 }
326
327                 // TdsInt16 to TdsBoolean
328                 public static explicit operator TdsBoolean (TdsInt16 x) 
329                 {
330                         if (x.IsNull)
331                                 return Null;
332                         else
333                                 return new TdsBoolean ((int)x.Value);
334                 }
335
336                 // TdsInt32 to TdsBoolean
337                 public static explicit operator TdsBoolean (TdsInt32 x) 
338                 {
339                         if (x.IsNull)
340                                 return Null;
341                         else
342                                 return new TdsBoolean (x.Value);
343                 }
344
345                 // TdsInt64 to TdsBoolean
346                 public static explicit operator TdsBoolean (TdsInt64 x) 
347                 {
348                         if (x.IsNull)
349                                 return Null;
350                         else
351                                 return new TdsBoolean ((int)x.Value);
352                 }
353
354                 // TdsMoney to TdsBoolean
355                 public static explicit operator TdsBoolean (TdsMoney x) 
356                 {
357                         if (x.IsNull)
358                                 return Null;
359                         else
360                                 return new TdsBoolean ((int)x.Value);
361                 }
362
363                 // TdsSingle to TdsBoolean
364                 public static explicit operator TdsBoolean (TdsSingle x) 
365                 {
366                         if (x.IsNull)
367                                 return Null;
368                         else
369                                 return new TdsBoolean ((int)x.Value);
370                 }
371
372                 // TdsString to TdsBoolean
373                 public static explicit operator TdsBoolean (TdsString x) 
374                 {
375                         return TdsBoolean.Parse (x.Value);
376                 }
377
378                 // Boolean to TdsBoolean
379                 public static implicit operator TdsBoolean (bool x) 
380                 {
381                         return new TdsBoolean (x);
382                 }
383         }
384 }