**** Merged from MCS ****
[mono-project.git] / mcs / class / Mono.Data.TdsClient / Mono.Data.TdsTypes / TdsByte.cs
blob43854b0ac9289ec6d3ea9c7efcff54b5ca9df73e
1 //
2 // Mono.Data.TdsTypes.TdsByte
3 //
4 // Author:
5 // Tim Coleman <tim@timcoleman.com>
6 //
7 // (C) Copyright Tim Coleman, 2002
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using Mono.Data.TdsClient;
32 using System;
33 using System.Data.SqlTypes;
34 using System.Globalization;
36 namespace Mono.Data.TdsTypes {
37 public struct TdsByte : INullable, IComparable
39 #region Fields
41 byte value;
42 private bool notNull;
44 public static readonly TdsByte MaxValue = new TdsByte (0xff);
45 public static readonly TdsByte MinValue = new TdsByte (0);
46 public static readonly TdsByte Null;
47 public static readonly TdsByte Zero = new TdsByte (0);
49 #endregion
51 #region Constructors
53 public TdsByte (byte value)
55 this.value = value;
56 notNull = true;
59 #endregion
61 #region Properties
63 public bool IsNull {
64 get { return !notNull; }
67 public byte Value {
68 get {
69 if (this.IsNull)
70 throw new TdsNullValueException ();
71 else
72 return value;
76 #endregion
78 #region Methods
80 public static TdsByte Add (TdsByte x, TdsByte y)
82 return (x + y);
85 public static TdsByte BitwiseAnd (TdsByte x, TdsByte y)
87 return (x & y);
90 public static TdsByte BitwiseOr (TdsByte x, TdsByte y)
92 return (x | y);
95 public int CompareTo (object value)
97 if (value == null)
98 return 1;
99 else if (!(value is TdsByte))
100 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.TdsTypes.TdsByte"));
101 else if (((TdsByte)value).IsNull)
102 return 1;
103 else
104 return this.value.CompareTo (((TdsByte)value).Value);
107 public static TdsByte Divide (TdsByte x, TdsByte y)
109 return (x / y);
112 public override bool Equals (object value)
114 if (!(value is TdsByte))
115 return false;
116 else
117 return (bool) (this == (TdsByte)value);
120 public static TdsBoolean Equals (TdsByte x, TdsByte y)
122 return (x == y);
125 public override int GetHashCode ()
127 return (int)value;
130 public static TdsBoolean GreaterThan (TdsByte x, TdsByte y)
132 return (x > y);
135 public static TdsBoolean GreaterThanOrEqual (TdsByte x, TdsByte y)
137 return (x >= y);
140 public static TdsBoolean LessThan (TdsByte x, TdsByte y)
142 return (x < y);
145 public static TdsBoolean LessThanOrEqual (TdsByte x, TdsByte y)
147 return (x <= y);
150 public static TdsByte Mod (TdsByte x, TdsByte y)
152 return (x % y);
155 public static TdsByte Multiply (TdsByte x, TdsByte y)
157 return (x * y);
160 public static TdsBoolean NotEquals (TdsByte x, TdsByte y)
162 return (x != y);
165 public static TdsByte OnesComplement (TdsByte x)
167 return ~x;
170 public static TdsByte Parse (string s)
172 return new TdsByte (Byte.Parse (s));
175 public static TdsByte Subtract (TdsByte x, TdsByte y)
177 return (x - y);
180 public TdsBoolean ToTdsBoolean ()
182 return ((TdsBoolean)this);
185 public TdsDecimal ToTdsDecimal ()
187 return ((TdsDecimal)this);
190 public TdsDouble ToTdsDouble ()
192 return ((TdsDouble)this);
195 public TdsInt16 ToTdsInt16 ()
197 return ((TdsInt16)this);
200 public TdsInt32 ToTdsInt32 ()
202 return ((TdsInt32)this);
205 public TdsInt64 ToTdsInt64 ()
207 return ((TdsInt64)this);
210 public TdsMoney ToTdsMoney ()
212 return ((TdsMoney)this);
215 public TdsSingle ToTdsSingle ()
217 return ((TdsSingle)this);
220 public TdsString ToTdsString ()
222 return ((TdsString)this);
225 public override string ToString ()
227 if (this.IsNull)
228 return "Null";
229 else
230 return value.ToString ();
233 public static TdsByte Xor (TdsByte x, TdsByte y)
235 return (x ^ y);
238 public static TdsByte operator + (TdsByte x, TdsByte y)
240 return new TdsByte ((byte) (x.Value + y.Value));
243 public static TdsByte operator & (TdsByte x, TdsByte y)
245 return new TdsByte ((byte) (x.Value & y.Value));
248 public static TdsByte operator | (TdsByte x, TdsByte y)
250 return new TdsByte ((byte) (x.Value | y.Value));
253 public static TdsByte operator / (TdsByte x, TdsByte y)
255 return new TdsByte ((byte) (x.Value / y.Value));
258 public static TdsBoolean operator == (TdsByte x, TdsByte y)
260 if (x.IsNull || y.IsNull)
261 return TdsBoolean.Null;
262 else
263 return new TdsBoolean (x.Value == y.Value);
266 public static TdsByte operator ^ (TdsByte x, TdsByte y)
268 return new TdsByte ((byte) (x.Value ^ y.Value));
271 public static TdsBoolean operator > (TdsByte x, TdsByte y)
273 if (x.IsNull || y.IsNull)
274 return TdsBoolean.Null;
275 else
276 return new TdsBoolean (x.Value > y.Value);
279 public static TdsBoolean operator >= (TdsByte x, TdsByte y)
281 if (x.IsNull || y.IsNull)
282 return TdsBoolean.Null;
283 else
284 return new TdsBoolean (x.Value >= y.Value);
287 public static TdsBoolean operator != (TdsByte x, TdsByte y)
289 if (x.IsNull || y.IsNull)
290 return TdsBoolean.Null;
291 else
292 return new TdsBoolean (!(x.Value == y.Value));
295 public static TdsBoolean operator < (TdsByte x, TdsByte y)
297 if (x.IsNull || y.IsNull)
298 return TdsBoolean.Null;
299 else
300 return new TdsBoolean (x.Value < y.Value);
303 public static TdsBoolean operator <= (TdsByte x, TdsByte y)
305 if (x.IsNull || y.IsNull)
306 return TdsBoolean.Null;
307 else
308 return new TdsBoolean (x.Value <= y.Value);
311 public static TdsByte operator % (TdsByte x, TdsByte y)
313 return new TdsByte ((byte) (x.Value % y.Value));
316 public static TdsByte operator * (TdsByte x, TdsByte y)
318 return new TdsByte ((byte) (x.Value * y.Value));
321 public static TdsByte operator ~ (TdsByte x)
323 return new TdsByte ((byte) ~x.Value);
326 public static TdsByte operator - (TdsByte x, TdsByte y)
328 return new TdsByte ((byte) (x.Value - y.Value));
331 public static explicit operator TdsByte (TdsBoolean x)
333 if (x.IsNull)
334 return Null;
335 else
336 return new TdsByte (x.ByteValue);
339 public static explicit operator byte (TdsByte x)
341 return x.Value;
344 public static explicit operator TdsByte (TdsDecimal x)
346 if (x.IsNull)
347 return Null;
348 else
349 return new TdsByte ((byte)x.Value);
352 public static explicit operator TdsByte (TdsDouble x)
354 if (x.IsNull)
355 return Null;
356 else
357 return new TdsByte ((byte)x.Value);
360 public static explicit operator TdsByte (TdsInt16 x)
362 if (x.IsNull)
363 return Null;
364 else
365 return new TdsByte ((byte)x.Value);
368 public static explicit operator TdsByte (TdsInt32 x)
370 if (x.IsNull)
371 return Null;
372 else
373 return new TdsByte ((byte)x.Value);
376 public static explicit operator TdsByte (TdsInt64 x)
378 if (x.IsNull)
379 return Null;
380 else
381 return new TdsByte ((byte)x.Value);
384 public static explicit operator TdsByte (TdsMoney x)
386 if (x.IsNull)
387 return Null;
388 else
389 return new TdsByte ((byte)x.Value);
392 public static explicit operator TdsByte (TdsSingle x)
394 if (x.IsNull)
395 return Null;
396 else
397 return new TdsByte ((byte)x.Value);
401 public static explicit operator TdsByte (TdsString x)
403 return TdsByte.Parse (x.Value);
406 public static implicit operator TdsByte (byte x)
408 return new TdsByte (x);
411 #endregion