2003-01-03 Gonzalo Paniagua Javier <gonzalo@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 // I guess this is the Boolean class. This was written word for word
12 // off the Library specification for System.Boolean in the ECMA
13 // TC39 TG2 and TG3 working documents.
14 //
15 // The XML style documentation isn't that elegant, but it seems to 
16 // be the standard way according to the poorly documented C# 
17 // Programmer's Reference section on XML Documentation.
18 //
19 // This header and the one above it can be formatted however, just trying
20 // to keep it consistent w/ the existing mcs headers.
21 //
22 // Even though it's not in the ECMA docs, the .NET Framework Class Library
23 // says this implements IConvertible, but if it does it has some other
24 // member functions to implement. 
25 //
26
27 using System.Globalization;
28 namespace System {
29
30         /// <summary>
31         /// Represents the boolean values of logical true and false.
32         /// </summary>
33         [Serializable]
34         public struct Boolean : IComparable, IConvertible {
35                 
36                 /// <value>
37                 /// The String representation of Boolean False
38                 /// </value>    
39                 public static readonly string FalseString;
40
41                 /// <value>
42                 /// The String representation of Boolean True
43                 /// </value>    
44                 public static readonly string TrueString;
45       
46                 /// <value>
47                 /// Internal bool value for for this instance
48                 /// </value>
49
50                 internal bool value;
51         
52                 static Boolean () 
53                 {
54                         FalseString = "False";
55                         TrueString = "True";
56                 }
57
58                 /// <summary>
59                 /// Compares the current Boolean instance against another object.
60                 /// </summary>
61                 /// <remarks>
62                 /// Throws an ArgumentException if <c>obj</c> isn't null or 
63                 /// a Boolean.
64                 /// </remarks>
65                 /// <param name="obj">
66                 /// The object to compare against
67                 /// </param>
68                 /// <returns>
69                 /// An int reflecting the sort order of this instance as 
70                 /// compared to <c>obj</c>
71                 /// -1 if this instance is false and <c>obj</c> is true
72                 ///  0 if this instance is equal to <c>obj</c>
73                 ///  1 if this instance is true and <c>obj</c> is false, 
74                 ///    or <c>obj</c> is null
75                 /// </returns>
76                 public int CompareTo (object obj) 
77                 {
78                         if (obj == null)
79                                 return 1;
80                         
81                         if (!(obj is System.Boolean))
82                                 throw new ArgumentException
83                                 (Locale.GetText ("Object is not a Boolean and is not a null reference"));
84                         
85                         // for case #3
86                         if (obj == null || (value == true && (bool)obj == false))
87                                 return 1;
88             
89                         // for case #2, else it's #1
90                         return (value == (bool)obj) ? 0 : -1;
91                 }
92         
93                 /// <summary>
94                 /// Determines whether this instance and another object represent the
95                 /// same type and value.
96                 /// </summary>
97                 /// <param name="obj">
98                 /// The object to check against
99                 /// </param>
100                 /// <returns>
101                 /// true if this instnace and <c>obj</c> are same value, 
102                 /// otherwise false if it is not or null
103                 /// </returns>
104                 public override bool Equals (Object obj) 
105                 {
106                         if (obj == null || !(obj is System.Boolean))
107                                 return false;
108
109                         return ((bool)obj) == value;
110                 }
111         
112                 /// <summary>
113                 /// Generates a hashcode for this object.
114                 /// </summary>
115                 /// <returns>
116                 /// An Int32 value holding the hash code
117                 /// </returns>
118                 public override int GetHashCode () 
119                 {
120                         // Guess there's not too many ways to hash a Boolean
121                         return value ? 1 : 0;
122                 }
123
124                 /// <summary>
125                 /// Returns a given string as a boolean value. The string must be 
126                 /// equivalent to either TrueString or FalseString, with leading and/or
127                 /// trailing spaces, and is parsed case-insensitively.
128                 /// </summary>
129                 /// <remarks>
130                 /// Throws an ArgumentNullException if <c>val</c> is null, or a 
131                 /// FormatException if <c>val</c> doesn't match <c>TrueString</c> 
132                 /// or <c>FalseString</c>
133                 /// </remarks>
134                 /// <param name="val">
135                 /// The string value to parse
136                 /// </param>
137                 /// <returns>
138                 /// true if <c>val</c> is equivalent to TrueString, 
139                 /// otherwise false
140                 /// </returns>
141                 public static bool Parse (string val) 
142                 {
143                         if (val == null)
144                                 throw new ArgumentNullException (
145                                         Locale.GetText ("Value is a null reference"));
146             
147                         val = val.Trim ();
148             
149                         if (String.Compare (val, TrueString, true) == 0)
150                                 return true;
151             
152                         if (String.Compare (val, FalseString, true) == 0)
153                                 return false;
154             
155                         throw new FormatException (Locale.GetText (
156                                 "Value is not equivalent to either TrueString or FalseString"));
157                 }
158
159                 /// <summary>
160                 /// Returns a string representation of this Boolean object.
161                 /// </summary>
162                 /// <returns>
163                 /// <c>FalseString</c> if the instance value is false, otherwise 
164                 /// <c>TrueString</c>
165                 /// </returns>
166                 public override string ToString () 
167                 {
168                         return value ? TrueString : FalseString;
169                 }
170                 
171                 // =========== IConvertible Methods =========== //
172
173                 public TypeCode GetTypeCode () 
174                 { 
175                         return TypeCode.Boolean;
176                 }
177
178                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
179                 {
180                         return System.Convert.ToType(value, conversionType, provider);
181                 }
182                 
183                 bool IConvertible.ToBoolean (IFormatProvider provider)
184                 {
185                         return value;
186                 }
187                 
188                 byte IConvertible.ToByte (IFormatProvider provider)
189                 {
190                         return System.Convert.ToByte(value);
191                 }
192                 
193                 char IConvertible.ToChar (IFormatProvider provider)
194                 {
195                         throw new InvalidCastException();
196                 }
197                 
198                 [CLSCompliant(false)]
199                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
200                 {
201                         throw new InvalidCastException();
202                 }
203                 
204                 decimal IConvertible.ToDecimal (IFormatProvider provider)
205                 {
206                         return System.Convert.ToDecimal(value);
207                 }
208                 
209                 double IConvertible.ToDouble (IFormatProvider provider)
210                 {
211                         return System.Convert.ToDouble(value);
212                 }
213                 
214                 short IConvertible.ToInt16 (IFormatProvider provider)
215                 {
216                         return System.Convert.ToInt16(value);
217                 }
218                 
219                 int IConvertible.ToInt32 (IFormatProvider provider)
220                 {
221                         return System.Convert.ToInt32(value);
222                 }
223                 
224                 long IConvertible.ToInt64 (IFormatProvider provider)
225                 {
226                         return System.Convert.ToInt64(value);
227                 }
228                 
229                 [CLSCompliant(false)] 
230                 sbyte IConvertible.ToSByte (IFormatProvider provider)
231                 {
232                         return System.Convert.ToSByte(value);
233                 }
234                 
235                 float IConvertible.ToSingle (IFormatProvider provider)
236                 {
237                         return System.Convert.ToSingle(value);
238                 }
239                 
240                 public string ToString (IFormatProvider provider)
241                 {
242                         return ToString();
243                 }
244                 
245                 [CLSCompliant(false)]
246                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
247                 {
248                         return System.Convert.ToUInt16(value);
249                 }
250                 
251                 [CLSCompliant(false)]
252                 uint IConvertible.ToUInt32 (IFormatProvider provider)
253                 {
254                         return System.Convert.ToUInt32(value);
255                 }
256                 
257                 [CLSCompliant(false)]
258                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
259                 {
260                         return System.Convert.ToUInt64(value);
261                 }
262                 
263         } // System.Boolean
264
265 } // Namespace System