* TimeSpan.cs: In 4.0 if the part parsed as days exceeds the allowed
[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 using System.Runtime.InteropServices;
45
46 namespace System
47 {
48         /// <summary>
49         /// Represents the boolean values of logical true and false.
50         /// </summary>
51         [Serializable]
52         [ComVisible (true)]
53         public struct Boolean : IComparable, IConvertible, IComparable <bool>, IEquatable <bool>
54         {
55                 /// <value>
56                 /// The String representation of Boolean False
57                 /// </value>    
58                 public static readonly string FalseString = "False";
59
60                 /// <value>
61                 /// The String representation of Boolean True
62                 /// </value>    
63                 public static readonly string TrueString = "True";
64
65                 internal bool m_value;
66
67                 /// <summary>
68                 /// Compares the current Boolean instance against another object.
69                 /// </summary>
70                 /// <remarks>
71                 /// Throws an ArgumentException if <c>obj</c> isn't null or 
72                 /// a Boolean.
73                 /// </remarks>
74                 /// <param name="obj">
75                 /// The object to compare against
76                 /// </param>
77                 /// <returns>
78                 /// An int reflecting the sort order of this instance as 
79                 /// compared to <c>obj</c>
80                 /// -1 if this instance is false and <c>obj</c> is true
81                 ///  0 if this instance is equal to <c>obj</c>
82                 ///  1 if this instance is true and <c>obj</c> is false, 
83                 ///    or <c>obj</c> is null
84                 /// </returns>
85                 public int CompareTo (object obj)
86                 {
87                         if (obj == null)
88                                 return 1;
89
90                         if (!(obj is System.Boolean))
91                                 throw new ArgumentException (Locale.GetText (
92                                         "Object is not a Boolean."));
93
94                         bool value = (bool) obj;
95
96                         // for case #3
97                         if (m_value && !value)
98                                 return 1;
99
100                         // for case #2, else it's #1
101                         return (m_value == value) ? 0 : -1;
102                 }
103
104                 /// <summary>
105                 /// Determines whether this instance and another object represent the
106                 /// same type and value.
107                 /// </summary>
108                 /// <param name="obj">
109                 /// The object to check against
110                 /// </param>
111                 /// <returns>
112                 /// true if this instnace and <c>obj</c> are same value, 
113                 /// otherwise false if it is not or null
114                 /// </returns>
115                 public override bool Equals (Object obj)
116                 {
117                         if (obj == null || !(obj is System.Boolean))
118                                 return false;
119
120                         bool value = (bool) obj;
121
122                         return m_value ? value : !value;
123                 }
124
125                 public int CompareTo (bool value)
126                 {
127                         if (m_value == value)
128                                 return 0;
129                         return !m_value ? -1 : 1;
130                 }
131
132                 public bool Equals (bool obj)
133                 {
134                         return m_value == obj;
135                 }
136
137                 /// <summary>
138                 /// Generates a hashcode for this object.
139                 /// </summary>
140                 /// <returns>
141                 /// An Int32 value holding the hash code
142                 /// </returns>
143                 public override int GetHashCode ()
144                 {
145                         // Guess there's not too many ways to hash a Boolean
146                         return m_value ? 1 : 0;
147                 }
148
149                 /// <summary>
150                 /// Returns a given string as a boolean value. The string must be 
151                 /// equivalent to either TrueString or FalseString, with leading and/or
152                 /// trailing spaces, and is parsed case-insensitively.
153                 /// </summary>
154                 /// <remarks>
155                 /// Throws an ArgumentNullException if <c>val</c> is null, or a 
156                 /// FormatException if <c>val</c> doesn't match <c>TrueString</c> 
157                 /// or <c>FalseString</c>
158                 /// </remarks>
159                 /// <param name="val">
160                 /// The string value to parse
161                 /// </param>
162                 /// <returns>
163                 /// true if <c>val</c> is equivalent to TrueString, 
164                 /// otherwise false
165                 /// </returns>
166                 public static bool Parse (string value)
167                 {
168                         if (value == null)
169                                 throw new ArgumentNullException ("value");
170
171                         value = value.Trim ();
172
173                         if (String.Compare (value, TrueString, true, CultureInfo.InvariantCulture) == 0)
174                                 return true;
175
176                         if (String.Compare (value, FalseString, true, CultureInfo.InvariantCulture) == 0)
177                                 return false;
178
179                         throw new FormatException (Locale.GetText (
180                                 "Value is not equivalent to either TrueString or FalseString."));
181                 }
182
183                 public static bool TryParse (string value, out bool result)
184                 {
185                         result = false;
186                         if (value == null)
187                                 return false;
188
189                         value = value.Trim ();
190
191                         if (String.Compare (value, TrueString, true, CultureInfo.InvariantCulture) == 0) {
192                                 result = true;
193                                 return true;
194                         }
195
196                         if (String.Compare (value, FalseString, true, CultureInfo.InvariantCulture) == 0) {
197                                 // result = false; // already set at false by default
198                                 return true;
199                         }
200
201                         return false;
202                 }
203
204                 /// <summary>
205                 /// Returns a string representation of this Boolean object.
206                 /// </summary>
207                 /// <returns>
208                 /// <c>FalseString</c> if the instance value is false, otherwise 
209                 /// <c>TrueString</c>
210                 /// </returns>
211                 public override string ToString ()
212                 {
213                         return m_value ? TrueString : FalseString;
214                 }
215
216                 // =========== IConvertible Methods =========== //
217                 public TypeCode GetTypeCode ()
218                 {
219                         return TypeCode.Boolean;
220                 }
221
222                 object IConvertible.ToType (Type targetType, IFormatProvider provider)
223                 {
224                         if (targetType == null)
225                                 throw new ArgumentNullException ("targetType");
226                         return System.Convert.ToType (m_value, targetType, provider, false);
227                 }
228
229                 bool IConvertible.ToBoolean (IFormatProvider provider)
230                 {
231                         return m_value;
232                 }
233
234                 byte IConvertible.ToByte (IFormatProvider provider)
235                 {
236                         return System.Convert.ToByte (m_value);
237                 }
238
239                 char IConvertible.ToChar (IFormatProvider provider)
240                 {
241                         throw new InvalidCastException ();
242                 }
243
244                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
245                 {
246                         throw new InvalidCastException ();
247                 }
248
249                 decimal IConvertible.ToDecimal (IFormatProvider provider)
250                 {
251                         return System.Convert.ToDecimal (m_value);
252                 }
253
254                 double IConvertible.ToDouble (IFormatProvider provider)
255                 {
256                         return System.Convert.ToDouble (m_value);
257                 }
258
259                 short IConvertible.ToInt16 (IFormatProvider provider)
260                 {
261                         return System.Convert.ToInt16 (m_value);
262                 }
263
264                 int IConvertible.ToInt32 (IFormatProvider provider)
265                 {
266                         return System.Convert.ToInt32 (m_value);
267                 }
268
269                 long IConvertible.ToInt64 (IFormatProvider provider)
270                 {
271                         return System.Convert.ToInt64 (m_value);
272                 }
273
274                 sbyte IConvertible.ToSByte (IFormatProvider provider)
275                 {
276                         return System.Convert.ToSByte (m_value);
277                 }
278
279                 float IConvertible.ToSingle (IFormatProvider provider)
280                 {
281                         return System.Convert.ToSingle (m_value);
282                 }
283
284                 public string ToString (IFormatProvider provider)
285                 {
286                         return ToString ();
287                 }
288
289                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
290                 {
291                         return System.Convert.ToUInt16 (m_value);
292                 }
293
294                 uint IConvertible.ToUInt32 (IFormatProvider provider)
295                 {
296                         return System.Convert.ToUInt32 (m_value);
297                 }
298
299                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
300                 {
301                         return System.Convert.ToUInt64 (m_value);
302                 }
303         }
304 }