2005-01-27 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System / Boolean.cs
1 //
2 // System.Boolean.cs
3 //
4 // Author:
5 //   Derek Holden (dholden@draper.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32 // Testing SVN 
33 //
34 // I guess this is the Boolean class. This was written word for word
35 // off the Library specification for System.Boolean in the ECMA
36 // TC39 TG2 and TG3 working documents.
37 //
38 // The XML style documentation isn't that elegant, but it seems to 
39 // be the standard way according to the poorly documented C# 
40 // Programmer's Reference section on XML Documentation.
41 //
42
43 using System.Globalization;
44
45 namespace System
46 {
47         /// <summary>
48         /// Represents the boolean values of logical true and false.
49         /// </summary>
50         [Serializable]
51         public struct Boolean : IComparable, IConvertible
52 #if NET_2_0
53                 , IComparable <bool>
54 #endif
55         {
56                 /// <value>
57                 /// The String representation of Boolean False
58                 /// </value>    
59                 public static readonly string FalseString = "False";
60
61                 /// <value>
62                 /// The String representation of Boolean True
63                 /// </value>    
64                 public static readonly string TrueString = "True";
65
66                 internal bool m_value;
67
68                 /// <summary>
69                 /// Compares the current Boolean instance against another object.
70                 /// </summary>
71                 /// <remarks>
72                 /// Throws an ArgumentException if <c>obj</c> isn't null or 
73                 /// a Boolean.
74                 /// </remarks>
75                 /// <param name="obj">
76                 /// The object to compare against
77                 /// </param>
78                 /// <returns>
79                 /// An int reflecting the sort order of this instance as 
80                 /// compared to <c>obj</c>
81                 /// -1 if this instance is false and <c>obj</c> is true
82                 ///  0 if this instance is equal to <c>obj</c>
83                 ///  1 if this instance is true and <c>obj</c> is false, 
84                 ///    or <c>obj</c> is null
85                 /// </returns>
86                 public int CompareTo (object obj)
87                 {
88                         if (obj == null)
89                                 return 1;
90
91                         if (!(obj is System.Boolean))
92                                 throw new ArgumentException (Locale.GetText (
93                                         "Object is not a Boolean."));
94
95                         // for case #3
96                         if (obj == null || (m_value == true && (bool) obj == false))
97                                 return 1;
98
99                         // for case #2, else it's #1
100                         return (m_value == (bool) obj) ? 0 : -1;
101                 }
102
103                 /// <summary>
104                 /// Determines whether this instance and another object represent the
105                 /// same type and value.
106                 /// </summary>
107                 /// <param name="obj">
108                 /// The object to check against
109                 /// </param>
110                 /// <returns>
111                 /// true if this instnace and <c>obj</c> are same value, 
112                 /// otherwise false if it is not or null
113                 /// </returns>
114                 public override bool Equals (Object obj)
115                 {
116                         if (obj == null || !(obj is System.Boolean))
117                                 return false;
118
119                         return ((m_value) ? ((bool) obj) : !((bool) obj));
120                 }
121
122 #if NET_2_0
123                 public int CompareTo (bool value)
124                 {
125                         return (m_value == value) ? 0 : -1;
126                 }
127
128                 public bool Equals (bool value)
129                 {
130                         return m_value == value;
131                 }
132 #endif
133
134                 /// <summary>
135                 /// Generates a hashcode for this object.
136                 /// </summary>
137                 /// <returns>
138                 /// An Int32 value holding the hash code
139                 /// </returns>
140                 public override int GetHashCode ()
141                 {
142                         // Guess there's not too many ways to hash a Boolean
143                         return m_value ? 1 : 0;
144                 }
145
146                 /// <summary>
147                 /// Returns a given string as a boolean value. The string must be 
148                 /// equivalent to either TrueString or FalseString, with leading and/or
149                 /// trailing spaces, and is parsed case-insensitively.
150                 /// </summary>
151                 /// <remarks>
152                 /// Throws an ArgumentNullException if <c>val</c> is null, or a 
153                 /// FormatException if <c>val</c> doesn't match <c>TrueString</c> 
154                 /// or <c>FalseString</c>
155                 /// </remarks>
156                 /// <param name="val">
157                 /// The string value to parse
158                 /// </param>
159                 /// <returns>
160                 /// true if <c>val</c> is equivalent to TrueString, 
161                 /// otherwise false
162                 /// </returns>
163                 public static bool Parse (string value)
164                 {
165                         if (value == null)
166                                 throw new ArgumentNullException ("value");
167
168                         value = value.Trim ();
169
170                         if (String.Compare (value, TrueString, true, CultureInfo.InvariantCulture) == 0)
171                                 return true;
172
173                         if (String.Compare (value, FalseString, true, CultureInfo.InvariantCulture) == 0)
174                                 return false;
175
176                         throw new FormatException (Locale.GetText (
177                                 "Value is not equivalent to either TrueString or FalseString."));
178                 }
179
180 #if NET_2_0
181                 public static bool TryParse (string value, out bool result)
182                 {
183                         result = false;
184                         if (value == null)
185                                 return false;
186
187                         value = value.Trim ();
188
189                         if (String.Compare (value, TrueString, true, CultureInfo.InvariantCulture) == 0) {
190                                 result = true;
191                                 return true;
192                         }
193
194                         if (String.Compare (value, FalseString, true, CultureInfo.InvariantCulture) == 0) {
195                                 // result = false; // already set at false by default
196                                 return true;
197                         }
198
199                         return false;
200                 }
201 #endif
202
203                 /// <summary>
204                 /// Returns a string representation of this Boolean object.
205                 /// </summary>
206                 /// <returns>
207                 /// <c>FalseString</c> if the instance value is false, otherwise 
208                 /// <c>TrueString</c>
209                 /// </returns>
210                 public override string ToString ()
211                 {
212                         return m_value ? TrueString : FalseString;
213                 }
214
215                 // =========== IConvertible Methods =========== //
216                 public TypeCode GetTypeCode ()
217                 {
218                         return TypeCode.Boolean;
219                 }
220
221                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
222                 {
223                         return System.Convert.ToType (m_value, conversionType, provider);
224                 }
225
226                 bool IConvertible.ToBoolean (IFormatProvider provider)
227                 {
228                         return m_value;
229                 }
230
231                 byte IConvertible.ToByte (IFormatProvider provider)
232                 {
233                         return System.Convert.ToByte (m_value);
234                 }
235
236                 char IConvertible.ToChar (IFormatProvider provider)
237                 {
238                         throw new InvalidCastException ();
239                 }
240
241                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
242                 {
243                         throw new InvalidCastException ();
244                 }
245
246                 decimal IConvertible.ToDecimal (IFormatProvider provider)
247                 {
248                         return System.Convert.ToDecimal (m_value);
249                 }
250
251                 double IConvertible.ToDouble (IFormatProvider provider)
252                 {
253                         return System.Convert.ToDouble (m_value);
254                 }
255
256                 short IConvertible.ToInt16 (IFormatProvider provider)
257                 {
258                         return System.Convert.ToInt16 (m_value);
259                 }
260
261                 int IConvertible.ToInt32 (IFormatProvider provider)
262                 {
263                         return System.Convert.ToInt32 (m_value);
264                 }
265
266                 long IConvertible.ToInt64 (IFormatProvider provider)
267                 {
268                         return System.Convert.ToInt64 (m_value);
269                 }
270
271                 sbyte IConvertible.ToSByte (IFormatProvider provider)
272                 {
273                         return System.Convert.ToSByte (m_value);
274                 }
275
276                 float IConvertible.ToSingle (IFormatProvider provider)
277                 {
278                         return System.Convert.ToSingle (m_value);
279                 }
280
281                 public string ToString (IFormatProvider provider)
282                 {
283                         return ToString ();
284                 }
285
286                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
287                 {
288                         return System.Convert.ToUInt16 (m_value);
289                 }
290
291                 uint IConvertible.ToUInt32 (IFormatProvider provider)
292                 {
293                         return System.Convert.ToUInt32 (m_value);
294                 }
295
296                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
297                 {
298                         return System.Convert.ToUInt64 (m_value);
299                 }
300         }
301 }