1 // System.Data.SqlTypes.SqlInt16
\r
4 // Konstantin Triger <kostat@mainsoft.com>
5 // Boris Kirzner <borisk@mainsoft.com>
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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 namespace System.Data.SqlTypes
\r
36 public struct SqlInt16 : INullable, IComparable
\r
39 private short _value;
\r
40 private bool _isNull;
\r
42 public static readonly SqlInt16 Null = new SqlInt16(true);
\r
43 public static readonly SqlInt16 MaxValue = new SqlInt16(short.MaxValue);
\r
44 public static readonly SqlInt16 MinValue = new SqlInt16(short.MinValue);
\r
45 public static readonly SqlInt16 Zero = new SqlInt16(0);
\r
48 private SqlInt16(bool isNull)
\r
55 * @param value A short whose value will be used for the new SqlInt16.
\r
57 public SqlInt16(short value)
\r
65 * @param value A int whose value will be used for the new SqlInt16.
\r
67 public SqlInt16(int value)
\r
69 if (value > short.MaxValue || value < short.MinValue)
\r
70 throw new OverflowException("the value is not legal - overflowing : " + value);
\r
72 _value = (short)value;
\r
78 * Indicates whether or not Value is null.
\r
79 * @return true if Value is null, otherwise false.
\r
92 * Gets the value of the SqlInt16 instance.
\r
93 * @return the value of this instance
\r
100 throw new SqlNullValueException();
\r
106 * Compares this instance to the supplied object and returns an indication of their relative values.
\r
107 * @param obj The object to compare.
\r
108 * @return A signed number indicating the relative values of the instance and the object.
\r
109 * Less than zero This instance is less than object.
\r
110 * Zero This instance is the same as object.
\r
111 * Greater than zero This instance is greater than object -or-
\r
112 * object is a null reference.
\r
114 public int CompareTo(Object obj)
\r
119 if (obj is SqlInt16)
\r
121 SqlInt16 i = (SqlInt16)obj;
\r
128 return this._value.CompareTo(i._value);
\r
131 throw new ArgumentException("parameter obj is not SqlInt16 : " + obj.GetType().Name);
\r
136 * The addition operator computes the sum of the two SqlInt16 operands.
\r
137 * @param x A SqlInt16 structure.
\r
138 * @param y A SqlInt16 structure.
\r
139 * @return The sum of the two SqlInt16 operands.
\r
140 * If one of the parameters is null or null value - return SqlInt16.Null.
\r
142 public static SqlInt16 Add(SqlInt16 x, SqlInt16 y)
\r
144 if (x.IsNull || y.IsNull)
\r
145 return SqlInt16.Null;
\r
147 int sum = checked(x._value + y._value);
\r
149 return new SqlInt16(sum);
\r
153 * Computes the bitwise AND of its SqlInt16 operands.
\r
154 * @param x A SqlInt16 instance.
\r
155 * @param y A SqlInt16 instance.
\r
156 * @return The results of the bitwise AND operation.
\r
158 public static SqlInt16 BitwiseAnd(SqlInt16 x, SqlInt16 y)
\r
160 if (x.IsNull || y.IsNull)
\r
161 return SqlInt16.Null;
\r
163 int res = x._value & y._value;
\r
165 return new SqlInt16(res);
\r
169 * Computes the bitwise OR of its SqlInt16 operands.
\r
170 * @param x A SqlInt16 instance.
\r
171 * @param y A SqlInt16 instance.
\r
172 * @return The results of the bitwise OR operation.
\r
174 public static SqlInt16 BitwiseOr(SqlInt16 x, SqlInt16 y)
\r
176 if (x.IsNull || y.IsNull)
\r
177 return SqlInt16.Null;
\r
179 int res = (ushort)x._value | (ushort)y._value;
\r
181 return new SqlInt16(res);
\r
185 * The division operator divides the first SqlInt16 operand by the second.
\r
186 * @param x A SqlInt16 instance.
\r
187 * @param y A SqlInt16 instance.
\r
188 * @return A SqlInt16 instance containing the results of the division operation.
\r
189 * If one of the parameters is null or null value - return SqlInt16.Null.
\r
191 public static SqlInt16 Divide(SqlInt16 x, SqlInt16 y)
\r
193 int val = x._value / y._value;
\r
194 return new SqlInt16(val);
\r
197 public override bool Equals(Object obj)
\r
203 if (obj is SqlInt16)
\r
205 SqlInt16 i = (SqlInt16)obj;
\r
207 if (IsNull && i.IsNull)
\r
210 if (IsNull || i.IsNull)
\r
213 return _value == i._value;
\r
222 * Performs a logical comparison on two instances of SqlInt16 to determine if they are equal.
\r
223 * @param x A SqlInt16 instance.
\r
224 * @param y A SqlInt16 instance.
\r
225 * @return true if the two values are equal, otherwise false.
\r
226 * If one of the parameters is null or null value return SqlBoolean.Null.
\r
228 public static SqlBoolean Equals(SqlInt16 x, SqlInt16 y)
\r
230 if (x.IsNull || y.IsNull)
\r
231 return SqlBoolean.Null;
\r
233 if (x._value == y._value)
\r
234 return SqlBoolean.True;
\r
236 return SqlBoolean.False;
\r
241 * Compares two instances of SqlByte to determine if the first is greater than the second.
\r
242 * @param x A SqlByte instance
\r
243 * @param y A SqlByte instance
\r
244 * @return A SqlBoolean that is True if the first instance is greater than the second instance, otherwise False.
\r
245 * If either instance of SqlInt16 is null, the Value of the SqlBoolean will be Null.
\r
247 public static SqlBoolean GreaterThan(SqlInt16 x, SqlInt16 y)
\r
249 if (x.IsNull || y.IsNull)
\r
250 return SqlBoolean.Null;
\r
252 if (x._value > y._value)
\r
253 return SqlBoolean.True;
\r
255 return SqlBoolean.False;
\r
259 * Compares two instances of SqlInt16 to determine if the first is greater than or equal to the second.
\r
260 * @param x A SqlInt16 instance
\r
261 * @param y A SqlInt16 instance
\r
262 * @return A SqlBoolean that is True if the first instance is greaater than or equal to the second instance, otherwise False.
\r
263 * If either instance of SqlInt16 is null, the Value of the SqlBoolean will be Null.
\r
265 public static SqlBoolean GreaterThanOrEqual(SqlInt16 x, SqlInt16 y)
\r
267 if (x.IsNull || y.IsNull)
\r
268 return SqlBoolean.Null;
\r
270 if (x._value >= y._value)
\r
271 return SqlBoolean.True;
\r
273 return SqlBoolean.False;
\r
277 * Compares two instances of SqlInt16 to determine if the first is less than the second.
\r
278 * @param x A SqlInt16 instance
\r
279 * @param y A SqlInt16 instance
\r
280 * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.
\r
281 * If either instance of SqlInt16 is null, the Value of the SqlBoolean will be Null.
\r
283 public static SqlBoolean LessThan(SqlInt16 x, SqlInt16 y)
\r
285 if (x.IsNull || y.IsNull)
\r
286 return SqlBoolean.Null;
\r
288 if (x._value < y._value)
\r
289 return SqlBoolean.True;
\r
291 return SqlBoolean.False;
\r
295 * Compares two instances of SqlInt16 to determine if the first is less than or equal to the second.
\r
296 * @param x A SqlInt16 instance
\r
297 * @param y A SqlInt16 instance
\r
298 * @return A SqlBoolean that is True if the first instance is less than or equal to the second instance, otherwise False.
\r
299 * If either instance of SqlInt16 is null, the Value of the SqlBoolean will be Null.
\r
301 public static SqlBoolean LessThanOrEqual(SqlInt16 x, SqlInt16 y)
\r
303 if (x.IsNull || y.IsNull)
\r
304 return SqlBoolean.Null;
\r
306 if (x._value <= y._value)
\r
307 return SqlBoolean.True;
\r
309 return SqlBoolean.False;
\r
313 * Computes the remainder after dividing its first SqlInt16 operand by its second.
\r
314 * @param x A SqlInt16 instance
\r
315 * @param y A SqlInt16 instance
\r
316 * @return A SqlInt16 instance whose Value contains the remainder.
\r
318 public static SqlInt16 Mod(SqlInt16 x, SqlInt16 y)
\r
320 if (x.IsNull || y.IsNull)
\r
321 return SqlInt16.Null;
\r
323 int mod = x._value % y._value;
\r
325 return new SqlInt16(mod);
\r
329 * The multiplication operator computes the product of the two SqlInt16 operands.
\r
330 * @param x A SqlInt16 instance
\r
331 * @param y A SqlInt16 instance
\r
332 * @return The product of the two SqlInt16 operands.
\r
334 public static SqlInt16 Multiply(SqlInt16 x, SqlInt16 y)
\r
336 if (x.IsNull || y.IsNull)
\r
337 return SqlInt16.Null;
\r
339 int res = checked(x._value * y._value);
\r
341 return new SqlInt16(res);
\r
345 * Compares two instances of SqlInt16 to determine if they are equal.
\r
346 * @param x A SqlInt16 instance
\r
347 * @param y A SqlInt16 instance
\r
348 * @return A SqlBoolean that is True if the two instances are not equal or False if the two instances are equal.
\r
349 * If either instance of SqlInt16 is null, the Value of the SqlBoolean will be Null.
\r
351 public static SqlBoolean NotEquals(SqlInt16 x, SqlInt16 y)
\r
353 if (x.IsNull || y.IsNull)
\r
354 return SqlBoolean.Null;
\r
356 if (x._value != y._value)
\r
357 return SqlBoolean.True;
\r
359 return SqlBoolean.False;
\r
363 * The ones complement operator performs a bitwise one's complement operation on its SqlInt16 operand.
\r
364 * @param x A SqlInt16 instance
\r
365 * @return A SqlInt16 instance whose Value property contains the ones complement of the SqlInt16 parameter.
\r
367 public static SqlInt16 OnesComplement(SqlInt16 x)
\r
369 int res = x._value ^ 0xFFFF;
\r
371 return new SqlInt16(res);
\r
375 * Converts the String representation of a number to its byte equivalent.
\r
376 * @param s The String to be parsed.
\r
377 * @return A SqlInt16 containing the value represented by the String.
\r
379 public static SqlInt16 Parse(String s)
\r
381 int res = short.Parse(s);
\r
382 return new SqlInt16(res);
\r
386 * The subtraction operator the second SqlInt16 operand from the first.
\r
387 * @param x A SqlInt16 instance
\r
388 * @param y A SqlInt16 instance
\r
389 * @return The results of the subtraction operation.
\r
391 public static SqlInt16 Subtract(SqlInt16 x, SqlInt16 y)
\r
393 int res = x._value - y._value;
\r
395 return new SqlInt16(res);
\r
399 * Performs a bitwise exclusive-OR operation on the supplied parameters.
\r
400 * @param x A SqlInt16 instance
\r
401 * @param y A SqlInt16 instance
\r
402 * @return The results of the XOR operation.
\r
404 public static SqlInt16 Xor(SqlInt16 x, SqlInt16 y)
\r
406 int res = x._value ^ y._value;
\r
408 return new SqlInt16(res);
\r
412 * Converts this SqlInt16 structure to SqlBoolean.
\r
413 * @return A SqlBoolean structure whose Value will be True if the SqlInt16 structure's Value is non-zero, False if the SqlInt16 is zero
\r
414 * and Null if the SqlInt16 structure is Null.
\r
416 public SqlBoolean ToSqlBoolean()
\r
419 return SqlBoolean.Null;
\r
422 return new SqlBoolean(0);
\r
424 return new SqlBoolean(1);
\r
428 * Converts this SqlInt16 structure to SqlByte.
\r
429 * @return A SqlByte structure whose Value equals the Value of this SqlInt16 structure.
\r
431 public SqlByte ToSqlByte()
\r
434 return SqlByte.Null;
\r
438 if (val < 0 || val > 255)
\r
439 throw new OverflowException("Can not conver this instance to SqlByte - overflowing : " + val);
\r
441 return new SqlByte((byte)val);
\r
445 * Converts this SqlInt16 structure to SqlDecimal.
\r
446 * @return A SqlDecimal structure whose Value equals the Value of this SqlInt16 structure.
\r
448 public SqlDecimal ToSqlDecimal()
\r
451 return SqlDecimal.Null;
\r
453 return new SqlDecimal(_value);
\r
457 * Converts this SqlInt16 structure to SqlDecimal.
\r
458 * @return A SqlDouble structure whose Value equals the Value of this SqlInt16 structure.
\r
460 public SqlDouble ToSqlDouble()
\r
463 return SqlDouble.Null;
\r
465 return new SqlDouble(_value);
\r
469 * Converts this SqlInt16 structure to SqlInt32.
\r
470 * @return A SqlInt32 structure whose Value equals the Value of this SqlDouble structure.
\r
472 public SqlInt32 ToSqlInt32()
\r
475 return SqlInt32.Null;
\r
477 return new SqlInt32(_value);
\r
482 * Converts this SqlInt16 structure to SqlInt64.
\r
483 * @return A SqlInt64 structure whose Value equals the Value of this SqlInt16 structure.
\r
485 public SqlInt64 ToSqlInt64()
\r
488 return SqlInt64.Null;
\r
490 return new SqlInt64(_value);
\r
494 * Converts this SqlInt16 instance to SqlDouble.
\r
495 * @return A SqlMoney instance whose Value equals the Value of this SqlInt16 instance.
\r
497 public SqlMoney ToSqlMoney()
\r
500 return SqlMoney.Null;
\r
502 return new SqlMoney(_value);
\r
506 * Converts this SqlInt16 instance to SqlSingle.
\r
507 * @return A SqlSingle instance whose Value equals the Value of this SqlInt16 instance.
\r
509 public SqlSingle ToSqlSingle()
\r
512 return SqlSingle.Null;
\r
514 return new SqlSingle(_value);
\r
518 * Converts this SqlInt16 structure to SqlString.
\r
519 * @return A SqlString structure whose value is a string representing the date and time contained in this SqlInt16 structure.
\r
521 public SqlString ToSqlString()
\r
523 return new SqlString(ToString());
\r
528 public override String ToString()
\r
532 return _value.ToString();
\r
535 public static void main(String[] args)
\r
537 SqlInt16 i1 = new SqlInt16(-2);
\r
538 SqlInt16 i2 = new SqlInt16(-2);
\r
540 Console.WriteLine(BitwiseAnd(i1, i2));
\r
544 public override int GetHashCode()
\r
549 public static SqlInt16 operator + (SqlInt16 x, SqlInt16 y)
\r
553 return new SqlInt16 ((short) (x.Value + y.Value));
\r
557 public static SqlInt16 operator & (SqlInt16 x, SqlInt16 y)
\r
559 return new SqlInt16 ((short) (x.Value & y.Value));
\r
562 public static SqlInt16 operator | (SqlInt16 x, SqlInt16 y)
\r
564 return new SqlInt16 ((short) ( x.Value | y.Value));
\r
567 public static SqlInt16 operator / (SqlInt16 x, SqlInt16 y)
\r
571 return new SqlInt16 ((short) (x.Value / y.Value));
\r
575 public static SqlBoolean operator == (SqlInt16 x, SqlInt16 y)
\r
577 if (x.IsNull || y.IsNull)
\r
578 return SqlBoolean.Null;
\r
580 return new SqlBoolean (x.Value == y.Value);
\r
583 public static SqlInt16 operator ^ (SqlInt16 x, SqlInt16 y)
\r
585 return new SqlInt16 ((short) (x.Value ^ y.Value));
\r
588 public static SqlBoolean operator > (SqlInt16 x, SqlInt16 y)
\r
590 if (x.IsNull || y.IsNull)
\r
591 return SqlBoolean.Null;
\r
593 return new SqlBoolean (x.Value > y.Value);
\r
596 public static SqlBoolean operator >= (SqlInt16 x, SqlInt16 y)
\r
598 if (x.IsNull || y.IsNull)
\r
599 return SqlBoolean.Null;
\r
601 return new SqlBoolean (x.Value >= y.Value);
\r
604 public static SqlBoolean operator != (SqlInt16 x, SqlInt16 y)
\r
606 if (x.IsNull || y.IsNull)
\r
607 return SqlBoolean.Null;
\r
609 return new SqlBoolean (!(x.Value == y.Value));
\r
612 public static SqlBoolean operator < (SqlInt16 x, SqlInt16 y)
\r
614 if (x.IsNull || y.IsNull)
\r
615 return SqlBoolean.Null;
\r
617 return new SqlBoolean (x.Value < y.Value);
\r
620 public static SqlBoolean operator <= (SqlInt16 x, SqlInt16 y)
\r
622 if (x.IsNull || y.IsNull)
\r
623 return SqlBoolean.Null;
\r
625 return new SqlBoolean (x.Value <= y.Value);
\r
628 public static SqlInt16 operator % (SqlInt16 x, SqlInt16 y)
\r
630 return new SqlInt16 ((short) (x.Value % y.Value));
\r
633 public static SqlInt16 operator * (SqlInt16 x, SqlInt16 y)
\r
637 return new SqlInt16 ((short) (x.Value * y.Value));
\r
641 public static SqlInt16 operator ~ (SqlInt16 x)
\r
646 return new SqlInt16 ((short) (~x.Value));
\r
649 public static SqlInt16 operator - (SqlInt16 x, SqlInt16 y)
\r
653 return new SqlInt16 ((short) (x.Value - y.Value));
\r
657 public static SqlInt16 operator - (SqlInt16 n)
\r
661 return new SqlInt16 ((short) (-n.Value));
\r
665 public static explicit operator SqlInt16 (SqlBoolean x)
\r
670 return new SqlInt16 ((short)x.ByteValue);
\r
673 public static explicit operator SqlInt16 (SqlDecimal x)
\r
680 return new SqlInt16 ((short)x.Value);
\r
684 public static explicit operator SqlInt16 (SqlDouble x)
\r
689 return new SqlInt16 (checked ((short)x.Value));
\r
692 public static explicit operator short (SqlInt16 x)
\r
697 public static explicit operator SqlInt16 (SqlInt32 x)
\r
704 return new SqlInt16 ((short)x.Value);
\r
708 public static explicit operator SqlInt16 (SqlInt64 x)
\r
716 return new SqlInt16 ((short)x.Value);
\r
721 public static explicit operator SqlInt16 (SqlMoney x)
\r
728 return new SqlInt16 ((short)x.Value);
\r
733 public static explicit operator SqlInt16 (SqlSingle x)
\r
741 return new SqlInt16 ((short)x.Value);
\r
746 public static explicit operator SqlInt16 (SqlString x)
\r
751 return SqlInt16.Parse (x.Value);
\r
754 public static implicit operator SqlInt16 (short x)
\r
756 return new SqlInt16 (x);
\r
759 public static implicit operator SqlInt16 (SqlByte x)
\r
761 return new SqlInt16 ((short)x.Value);
\r