-
Notifications
You must be signed in to change notification settings - Fork 13
/
Optional.cs
216 lines (188 loc) · 4.05 KB
/
Optional.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* Date: 14.7.2015, Time: 0:44 */
using System;
using System.Collections;
using System.Collections.Generic;
namespace IllidanS4.SharpUtils.ObjectModel
{
/// <summary>
/// Represents an optional value. Similar to <see cref="System.Nullable{T}"/> except that the generic type parameter is not constrained in any way.
/// Conforms to ECMA TR/89.
/// </summary>
public struct Optional<T> : IComparable, IComparable<T>, IComparable<Optional<T>?>, IComparable<Optional<T>>, IEquatable<T>, IEquatable<Optional<T>?>, IEquatable<Optional<T>>, IEnumerable<T>
{
private readonly bool hasValue;
private readonly T value;
public Optional(T value)
{
this.value = value;
hasValue = true;
}
public int CompareTo(object other)
{
if(other == null)
{
return hasValue ? 1 : 0;
}else{
return CompareTo((Optional<T>)other);
}
}
public int CompareTo(Optional<T>? other)
{
if(other == null)
{
return hasValue ? 1 : 0;
}else{
return CompareTo(other.Value);
}
}
public int CompareTo(Optional<T> other)
{
if(other.hasValue)
{
return CompareTo(other.value);
}else{
return hasValue ? 1 : 0;
}
}
public int CompareTo(T other)
{
if(!hasValue)
{
return -1;
}
var comp = value as IComparable<T>;
if(comp != null)
{
return comp.CompareTo(other);
}
var comp2 = value as IComparable;
if(comp != null)
{
return comp.CompareTo(other);
}
throw new ArgumentException("Argument doesn't implement IComparable.");
}
public override bool Equals(object other)
{
if(other == null)
{
return !hasValue;
}else if(other is Optional<T>)
{
return Equals((Optional<T>)other);
}else if(other is T)
{
return Equals((T)other);
}else{
return false;
}
}
public bool Equals(Optional<T>? other)
{
if(other == null)
{
return !hasValue;
}
return Equals(other.Value);
}
public bool Equals(Optional<T> other)
{
if(other.hasValue)
{
return Equals(other.value);
}else{
return !hasValue;
}
}
public bool Equals(T other)
{
if(!hasValue)
{
return false;
}
var eq = value as IEquatable<T>;
if(eq != null)
{
return eq.Equals(other);
}
return value.Equals(other);
}
public static Optional<T> FromNullable<T2>(T2? value) where T2 : struct, T
{
if(value == null)
{
return default(Optional<T>);
}else{
return new Optional<T>(value.Value);
}
}
public static T2? ToNullable<T2>(Optional<T> value) where T2 : struct, T
{
if(!value.hasValue) return null;
var val = value.value;
return Extensions.FastCast<T, T2>(val);
}
public static T FromOptional(Optional<T> value)
{
return value.Value;
}
public static Optional<T> ToOptional(T value)
{
return new Optional<T>(value);
}
public override int GetHashCode()
{
if(!hasValue) return 0;
return value.GetHashCode();
}
public T GetValueOrDefault()
{
return GetValueOrDefault(default(T));
}
public T GetValueOrDefault(T alternateDefaultValue)
{
if(hasValue) return value;
return alternateDefaultValue;
}
public bool HasValue{
get{
return hasValue;
}
}
public static bool operator ==(Optional<T> left, Optional<T> right)
{
return left.Equals(right);
}
public static bool operator !=(Optional<T> left, Optional<T> right)
{
return !left.Equals(right);
}
public static explicit operator T(Optional<T> value)
{
return FromOptional(value);
}
public static implicit operator Optional<T>(T value)
{
return ToOptional(value);
}
public override string ToString()
{
if(hasValue) return value.ToString();
return String.Empty;
}
public T Value{
get{
if(hasValue) return value;
throw new InvalidOperationException("This object doesn't contain any value.");
}
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
if(hasValue) yield return value;
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<T>)this).GetEnumerator();
}
}
}