2007-11-05 Stephane Delcroix <sdelcroix@novell.com>
[mono.git] / mcs / class / corlib / System / UInt16.cs
1 //
2 // System.UInt16.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 // Copyright (C) 2004 Novell (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Globalization;
31
32 namespace System
33 {
34         [Serializable]
35         [CLSCompliant (false)]
36 #if NET_2_0
37         [System.Runtime.InteropServices.ComVisible (true)]
38 #endif
39         public struct UInt16 : IFormattable, IConvertible, IComparable
40 #if NET_2_0
41                 , IComparable<UInt16>, IEquatable <UInt16>
42 #endif
43         {
44                 public const ushort MaxValue = 0xffff;
45                 public const ushort MinValue = 0;
46
47                 internal ushort m_value;
48
49                 public int CompareTo (object value)
50                 {
51                         if (value == null)
52                                 return 1;
53
54                         if(!(value is System.UInt16))
55                                 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt16."));
56
57                         return this.m_value - ((ushort) value);
58                 }
59
60                 public override bool Equals (object obj)
61                 {
62                         if (!(obj is System.UInt16))
63                                 return false;
64
65                         return ((ushort) obj) == m_value;
66                 }
67
68                 public override int GetHashCode ()
69                 {
70                         return m_value;
71                 }
72
73 #if NET_2_0
74                 public int CompareTo (ushort value)
75                 {
76                         if (m_value == value)
77                                 return 0;
78                         if (m_value > value)
79                                 return 1;
80                         else
81                                 return -1;
82                 }
83
84                 public bool Equals (ushort value)
85                 {
86                         return value == m_value;
87                 }
88 #endif
89
90
91                 [CLSCompliant (false)]
92                 public static ushort Parse (string s, IFormatProvider provider)
93                 {
94                         return Parse (s, NumberStyles.Integer, provider);
95                 }
96
97                 [CLSCompliant (false)]
98                 public static ushort Parse (string s, NumberStyles style)
99                 {
100                         return Parse (s, style, null);
101                 }
102
103                 [CLSCompliant (false)]
104                 public static ushort Parse (string s, NumberStyles style, IFormatProvider provider)
105                 {
106                         uint tmpResult = UInt32.Parse (s, style, provider);
107                         if (tmpResult > UInt16.MaxValue)
108                                 throw new OverflowException (Locale.GetText ("Value too large."));
109
110                         return (ushort) tmpResult;
111                 }
112
113                 [CLSCompliant(false)]
114                 public static ushort Parse (string s) 
115                 {
116                         return Parse (s, NumberStyles.Number, null);
117                 }
118
119 #if NET_2_0
120                 [CLSCompliant(false)]
121                 public static bool TryParse (string s, out ushort result) 
122                 {
123                         return TryParse (s, NumberStyles.Integer, null, out result);
124                 }
125
126                 [CLSCompliant(false)]
127                 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out ushort result) 
128                 {
129                         uint tmpResult;
130                         result = 0;
131                                 
132                         if (!UInt32.TryParse (s, style, provider, out tmpResult))
133                                 return false;
134                                 
135                         if (tmpResult > UInt16.MaxValue)
136                                 return false;
137                                 
138                         result = (ushort)tmpResult;
139                         return true;
140                 }
141 #endif
142
143                 public override string ToString ()
144                 {
145                         return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value));
146                 }
147
148                 public string ToString (IFormatProvider provider)
149                 {
150                         return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value), provider);
151                 }
152
153                 public string ToString (string format)
154                 {
155                         return ToString (format, null);
156                 }
157
158                 public string ToString (string format, IFormatProvider provider)
159                 {
160                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
161                         return NumberFormatter.NumberToString (format, m_value, nfi);
162                 }
163
164                 // =========== IConvertible Methods =========== //
165                 public TypeCode GetTypeCode ()
166                 {
167                         return TypeCode.UInt16;
168                 }
169
170                 bool IConvertible.ToBoolean (IFormatProvider provider)
171                 {
172                         return System.Convert.ToBoolean (m_value);
173                 }
174
175                 byte IConvertible.ToByte (IFormatProvider provider)
176                 {
177                         return System.Convert.ToByte (m_value);
178                 }
179
180                 char IConvertible.ToChar (IFormatProvider provider)
181                 {
182                         return System.Convert.ToChar (m_value);
183                 }
184
185                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
186                 {
187                         return System.Convert.ToDateTime (m_value);
188                 }
189
190                 decimal IConvertible.ToDecimal (IFormatProvider provider)
191                 {
192                         return System.Convert.ToDecimal (m_value);
193                 }
194
195                 double IConvertible.ToDouble (IFormatProvider provider)
196                 {
197                         return System.Convert.ToDouble (m_value);
198                 }
199
200                 short IConvertible.ToInt16 (IFormatProvider provider)
201                 {
202                         return System.Convert.ToInt16 (m_value);
203                 }
204
205                 int IConvertible.ToInt32 (IFormatProvider provider)
206                 {
207                         return System.Convert.ToInt32 (m_value);
208                 }
209
210                 long IConvertible.ToInt64 (IFormatProvider provider)
211                 {
212                         return System.Convert.ToInt64 (m_value);
213                 }
214
215                 sbyte IConvertible.ToSByte (IFormatProvider provider)
216                 {
217                         return System.Convert.ToSByte (m_value);
218                 }
219
220                 float IConvertible.ToSingle (IFormatProvider provider)
221                 {
222                         return System.Convert.ToSingle (m_value);
223                 }
224
225                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
226                 {
227                         return System.Convert.ToType (m_value, conversionType, provider);
228                 }
229
230                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
231                 {
232                         return m_value;
233                 }
234
235                 uint IConvertible.ToUInt32 (IFormatProvider provider)
236                 {
237                         return System.Convert.ToUInt32 (m_value);
238                 }
239
240                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
241                 {
242                         return System.Convert.ToUInt64 (m_value);
243                 }
244         }
245 }