2005-04-12 Dick Porter <dick@ximian.com>
[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 {
41
42         [CLSCompliant(false)]
43         public static class Nullable
44         {
45                 public static int Compare<T> (Nullable<T> left, Nullable<T> right)
46                 {
47                         IComparable icomparable = left.value as IComparable;
48                         if (icomparable == null)
49                                 throw new ArgumentException ("At least one object must implement IComparable.");
50                         if (left.has_value == false && right.has_value == false)
51                                 return 0;
52                         if (!left.has_value)
53                                 return -1;
54                         if (!right.has_value)
55                                 return 1;
56
57                         return icomparable.CompareTo (right.value);
58                 }
59
60                 public static bool Equals<T> (Nullable <T> value1, Nullable<T> value2)
61                 {
62                         return value1.Equals (value2);
63                 }
64
65                 public static Nullable<T> FromObject<T> (object value)
66                 {
67                         if (!(value is T))
68                                 throw new ArgumentException ("Object type can not be converted to target type.");
69
70                         return new Nullable<T> ((T) value);
71                 }
72
73                 public static T GetValueOrDefault<T>(Nullable<T> value)
74                 {
75                         return GetValueOrDefault<T> (value, default (T));
76                 }
77
78                 public static T GetValueOrDefault<T> (Nullable<T> value, T defaultValue)
79                 {
80                         if (!value.has_value)
81                                 return defaultValue;
82
83                         return value.value;
84                 }
85
86                 public static bool HasValue<T> (Nullable <T> value)
87                 {
88                         return value.has_value;
89                 }
90
91                 public static object ToObject<T> (Nullable<T> value)
92                 {
93                         if (!value.has_value)
94                                 return null;
95
96                         return (object)value.value;
97                 }
98         }
99         
100         [CLSCompliant(false)]
101         public struct Nullable<T> : IComparable, INullableValue
102         {
103                 internal T value;
104                 internal bool has_value;
105
106                 public Nullable (T value)
107                 {
108                         if (value == null)
109                                 this.has_value = false;
110                         else
111                                 this.has_value = true;
112                         
113                         this.value = value;
114                 }
115
116                 public bool HasValue {
117                         get { return has_value; }
118                 }
119
120                 public T Value {
121                         get { 
122                                 if (!has_value)
123                                         throw new InvalidOperationException ("Nullable object must have a value.");
124                                 
125                                 return value; 
126                         }
127                 }
128
129                 object INullableValue.Value {
130                         get {
131                                 return (object)Value;
132                         }
133                 }
134                 
135                 [Obsolete]
136                 public int CompareTo (Nullable<T> other)
137                 {
138                         return Nullable.Compare<T> (this, other);
139                 }
140
141                 [Obsolete]
142                 public int CompareTo (object other)
143                 {
144                         if (!(other is Nullable<T>))
145                                 throw new ArgumentException ("Object type can not be converted to target type.");
146
147                         return Nullable.Compare<T> (this, (Nullable<T>) other);
148                 }
149
150                 public override bool Equals (object other)
151                 {
152                         if (!(other is Nullable<T>))
153                                 return false;
154
155                         return Equals ((Nullable <T>) other);
156                 }
157
158                 public bool Equals (Nullable<T> other)
159                 {
160                         if (other.has_value != has_value)
161                                 return false;
162
163                         if (has_value == false)
164                                 return true;
165
166                         return other.value.Equals (value);
167                 }
168
169                 [Obsolete]
170                 public static Nullable<T> FromObject (object value)
171                 {
172                         return Nullable.FromObject<T> (value);
173                 }
174
175                 public override int GetHashCode ()
176                 {
177                         if (!has_value)
178                                 return 0;
179
180                         return value.GetHashCode ();
181                 }
182
183                 [Obsolete]
184                 public T GetValueOrDefault ()
185                 {
186                         return Nullable.GetValueOrDefault<T> (this, default (T));
187                 }
188
189                 [Obsolete]
190                 public T GetValueOrDefault (T def_value)
191                 {
192                         return Nullable.GetValueOrDefault<T> (this, def_value);
193                 }
194
195                 public object ToObject ()
196                 {
197                         if (!has_value)
198                                 return null;
199
200                         return (object)value;
201                 }
202
203                 public override string ToString ()
204                 {
205                         if (has_value)
206                                 return value.ToString ();
207                         else
208                                 return "";
209                 }
210
211                 public static implicit operator Nullable<T> (T value)
212                 {
213                         return new Nullable<T> (value);
214                 }
215
216                 public static explicit operator T (Nullable<T> value)
217                 {
218                         return value.Value;
219                 }
220
221                 public static bool operator == (Nullable<T> left, Nullable<T> right)
222                 {
223                         return left.Equals (right);
224                 }
225
226                 public static bool operator != (Nullable<T> left, Nullable<T> right)
227                 {
228                         return !left.Equals (right);
229                 }
230         }
231 }
232 #endif