imported everything from my branch (which is slightly harmless).
[mono.git] / mcs / class / corlib / System / Nullable.cs
1 //
2 // System.Nullable
3 //
4 // Martin Baulig (martin@ximian.com)
5 //
6 // (C) 2004 Novell, Inc.
7 //
8
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Reflection;
33 #if NET_2_0
34 using System.Collections.Generic;
35 #endif
36 using System.Runtime.CompilerServices;
37
38 #if NET_2_0
39 namespace System {
40         public static class Nullable {
41                 public static int Compare<T> (Nullable<T> left, Nullable<T> right)
42                 {
43                         IComparable icomparable = left.value as IComparable;
44                         if (icomparable == null)
45                                 throw new ArgumentException ("At least one object must implement IComparable.");
46                         if (left.has_value == false && right.has_value == false)
47                                 return 0;
48                         if (!left.has_value)
49                                 return -1;
50                         if (!right.has_value)
51                                 return 1;
52
53                         return icomparable.CompareTo (right.value);
54                 }
55
56                 public static bool Equals<T> (Nullable <T> value1, Nullable<T> value2)
57                 {
58                         return value1.Equals (value2);
59                 }
60
61                 public static Nullable<T> FromObject<T> (object value)
62                 {
63                         if (!(value is T))
64                                 throw new ArgumentException ("Object type can not be converted to target type.");
65
66                         return new Nullable<T> ((T) value);
67                 }
68
69                 public static T GetValueOrDefault<T>(Nullable<T> value)
70                 {
71                         return GetValueOrDefault<T> (value, default (T));
72                 }
73
74                 public static T GetValueOrDefault<T> (Nullable<T> value, T defaultValue)
75                 {
76                         if (!value.has_value)
77                                 return defaultValue;
78
79                         return value.value;
80                 }
81
82                 public static bool HasValue<T> (Nullable <T> value)
83                 {
84                         return value.has_value;
85                 }
86
87                 public static object ToObject<T> (Nullable<T> value)
88                 {
89                         if (!value.has_value)
90                                 return null;
91
92                         return (object)value.value;
93                 }
94         }
95         
96         public struct Nullable<T> : IComparable, INullableValue
97         {
98                 internal T value;
99                 internal bool has_value;
100
101                 public Nullable (T value)
102                 {
103                         if (value == null)
104                                 this.has_value = false;
105                         else
106                                 this.has_value = true;
107                         
108                         this.value = value;
109                 }
110
111                 public bool HasValue {
112                         get { return has_value; }
113                 }
114
115                 public T Value {
116                         get { 
117                                 if (!has_value)
118                                         throw new InvalidOperationException ("Nullable object must have a value.");
119                                 
120                                 return value; 
121                         }
122                 }
123
124                 object INullableValue.Value {
125                         get {
126                                 return (object)Value;
127                         }
128                 }
129                 
130                 [Obsolete]
131                 public int CompareTo (Nullable<T> other)
132                 {
133                         return Nullable.Compare<T> (this, other);
134                 }
135
136                 [Obsolete]
137                 public int CompareTo (object other)
138                 {
139                         if (!(other is Nullable<T>))
140                                 throw new ArgumentException ("Object type can not be converted to target type.");
141
142                         return Nullable.Compare<T> (this, (Nullable<T>) other);
143                 }
144
145                 public override bool Equals (object other)
146                 {
147                         if (!(other is Nullable<T>))
148                                 return false;
149
150                         return Equals ((Nullable <T>) other);
151                 }
152
153                 public bool Equals (Nullable<T> other)
154                 {
155                         if (other.has_value != has_value)
156                                 return false;
157
158                         if (has_value == false)
159                                 return true;
160
161                         return other.value.Equals (value);
162                 }
163
164                 [Obsolete]
165                 public static Nullable<T> FromObject (object value)
166                 {
167                         return Nullable.FromObject<T> (value);
168                 }
169
170                 public override int GetHashCode ()
171                 {
172                         if (!has_value)
173                                 return 0;
174
175                         return value.GetHashCode ();
176                 }
177
178                 [Obsolete]
179                 public T GetValueOrDefault ()
180                 {
181                         return Nullable.GetValueOrDefault<T> (this, default (T));
182                 }
183
184                 [Obsolete]
185                 public T GetValueOrDefault (T def_value)
186                 {
187                         return Nullable.GetValueOrDefault<T> (this, def_value);
188                 }
189
190                 public object ToObject ()
191                 {
192                         if (!has_value)
193                                 return null;
194
195                         return (object)value;
196                 }
197
198                 public override string ToString ()
199                 {
200                         if (has_value)
201                                 return value.ToString ();
202                         else
203                                 return "";
204                 }
205
206                 public static implicit operator Nullable<T> (T value)
207                 {
208                         return new Nullable<T> (value);
209                 }
210
211                 public static explicit operator T (Nullable<T> value)
212                 {
213                         return value.Value;
214                 }
215
216                 public static bool operator == (Nullable<T> left, Nullable<T> right)
217                 {
218                         return left.Equals (right);
219                 }
220
221                 public static bool operator != (Nullable<T> left, Nullable<T> right)
222                 {
223                         return !left.Equals (right);
224                 }
225         }
226 }
227 #endif