2002-01-14 Miguel de Icaza <miguel@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         // The .NET Framework SDK lists this as implementing IConvertible,
34         // though it's not done in the ECMA spec.
35         public struct Boolean : IComparable { //, IConvertible {
36                 
37                 /// <value>
38                 /// The String representation of Boolean False
39                 /// </value>    
40                 public static readonly string FalseString;
41
42                 /// <value>
43                 /// The String representation of Boolean True
44                 /// </value>    
45                 public static readonly string TrueString;
46       
47                 /// <value>
48                 /// Internal bool value for for this instance
49                 /// </value>
50                 //
51                 // HACK: we tag it as public, so the source will compile.               
52                 public bool value;
53         
54                 static Boolean () 
55                 {
56                         FalseString = "False";
57                         TrueString = "True";
58                 }
59
60                 /// <summary>
61                 /// Compares the current Boolean instance against another object.
62                 /// </summary>
63                 /// <remarks>
64                 /// Throws an ArgumentException if <c>obj</c> isn't null or 
65                 /// a Boolean.
66                 /// </remarks>
67                 /// <param name="obj">
68                 /// The object to compare against
69                 /// </param>
70                 /// <returns>
71                 /// An int reflecting the sort order of this instance as 
72                 /// compared to <c>obj</c>
73                 /// -1 if this instance is false and <c>obj</c> is true
74                 ///  0 if this instance is equal to <c>obj</c>
75                 ///  1 if this instance is true and <c>obj</c> is false, 
76                 ///    or <c>obj</c> is null
77                 /// </returns>
78                 public int CompareTo (object obj) 
79                 {
80                         if (obj == null)
81                                 return 1;
82                         
83                         if (!(obj is System.Boolean))
84                                 throw new ArgumentException
85                                 (Locale.GetText ("Object is not a Boolean and is not a null reference"));
86                         
87                         // for case #3
88                         if (obj == null || (value == true && (bool)obj == false))
89                                 return 1;
90             
91                         // for case #2, else it's #1
92                         return (value == (bool)obj) ? 0 : -1;
93                 }
94         
95                 /// <summary>
96                 /// Determines whether this instance and another object represent the
97                 /// same type and value.
98                 /// </summary>
99                 /// <param name="obj">
100                 /// The object to check against
101                 /// </param>
102                 /// <returns>
103                 /// true if this instnace and <c>obj</c> are same value, 
104                 /// otherwise false if it is not or null
105                 /// </returns>
106                 public override bool Equals (Object obj) 
107                 {
108                         if (obj == null || !(obj is System.Boolean))
109                                 return false;
110
111                         return ((bool)obj) == value;
112                 }
113         
114                 /// <summary>
115                 /// Generates a hashcode for this object.
116                 /// </summary>
117                 /// <returns>
118                 /// An Int32 value holding the hash code
119                 /// </returns>
120                 public override int GetHashCode () 
121                 {
122                         // Guess there's not too many ways to hash a Boolean
123                         return value ? 1 : 0;
124                 }
125
126                 /// <summary>
127                 /// Returns a given string as a boolean value. The string must be 
128                 /// equivalent to either TrueString or FalseString, with leading and/or
129                 /// trailing spaces, and is parsed case-insensitively.
130                 /// </summary>
131                 /// <remarks>
132                 /// Throws an ArgumentNullException if <c>val</c> is null, or a 
133                 /// FormatException if <c>val</c> doesn't match <c>TrueString</c> 
134                 /// or <c>FalseString</c>
135                 /// </remarks>
136                 /// <param name="val">
137                 /// The string value to parse
138                 /// </param>
139                 /// <returns>
140                 /// true if <c>val</c> is equivalent to TrueString, 
141                 /// otherwise false
142                 /// </returns>
143                 public static bool Parse (string val) 
144                 {
145                         if (val == null)
146                                 throw new ArgumentNullException (
147                                         Locale.GetText ("Value is a null reference"));
148             
149                         val = val.Trim ();
150             
151                         if (String.Compare (val, TrueString, true) == 0)
152                                 return true;
153             
154                         if (String.Compare (val, FalseString, true) == 0)
155                                 return false;
156             
157                         throw new FormatException (Locale.GetText (
158                                 "Value is not equivalent to either TrueString or FalseString"));
159                 }
160
161                 /// <summary>
162                 /// Returns a string representation of this Boolean object.
163                 /// </summary>
164                 /// <returns>
165                 /// <c>FalseString</c> if the instance value is false, otherwise 
166                 /// <c>TrueString</c>
167                 /// </returns>
168                 public override string ToString () 
169                 {
170                         return value ? TrueString : FalseString;
171                 }
172                 
173                 // =========== IConvertible Methods =========== //
174
175                 public TypeCode GetTypeCode () 
176                 { 
177                         return TypeCode.Boolean;
178                 }
179
180         } // System.Boolean
181
182 } // Namespace System