Repository navigation
Expand file tree
/
Copy pathOptional.cs
More file actions
204 lines (186 loc) · 6.82 KB
/
Copy pathOptional.cs
File metadata and controls
204 lines (186 loc) · 6.82 KB
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
using System;
using System.Collections.Generic;
namespace DynamicData.Kernel
{
/// <summary>
/// Optional factory class
/// </summary>
public static class Optional
{
/// <summary>
///Wraps the specified value in an Optional container
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The value.</param>
/// <returns></returns>
public static Optional<T> Some<T>(T value)
{
return new Optional<T>(value);
}
/// <summary>
/// Returns an None optional value for the specified type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Optional<T> None<T>()
{
return Optional<T>.None;
}
}
/// <summary>
/// The equivalent of a nullable type which works on value and reference types
/// </summary>
/// <typeparam name="T">The underlying value type of the <see cref="T:System.Nullable`1"/> generic type.</typeparam><filterpriority>1</filterpriority>
public struct Optional<T> : IEquatable<Optional<T>>
{
private readonly T _value;
/// <summary>
/// The default valueless optional
/// </summary>
public static readonly Optional<T> None = default(Optional<T>);
/// <summary>
/// Initializes a new instance of the <see cref="Optional{T}"/> struct.
/// </summary>
/// <param name="value">The value.</param>
internal Optional(T value)
{
if (ReferenceEquals(value, null))
{
HasValue = false;
_value = default(T);
}
else
{
_value = value;
HasValue = true;
}
}
/// <summary>
/// Creates the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
public static Optional<T> Create(T value)
{
return new Optional<T>(value);
}
/// <summary>
/// Gets a value indicating whether the current <see cref="T:System.Nullable`1"/> object has a value.
/// </summary>
///
/// <returns>
/// true if the current <see cref="T:System.Nullable`1"/> object has a value; false if the current <see cref="T:System.Nullable`1"/> object has no value.
/// </returns>
public bool HasValue { get; }
/// <summary>
/// Gets the value of the current <see cref="T:System.Nullable`1"/> value.
/// </summary>
///
/// <returns>
/// The value of the current <see cref="T:System.Nullable`1"/> object if the <see cref="P:System.Nullable`1.HasValue"/> property is true. An exception is thrown if the <see cref="P:System.Nullable`1.HasValue"/> property is false.
/// </returns>
/// <exception cref="T:System.InvalidOperationException">The <see cref="P:System.Nullable`1.HasValue"/> property is false.</exception>
public T Value
{
get
{
if (!HasValue)
{
throw new InvalidOperationException("Optional<T> has no value");
}
return _value;
}
}
/// <summary>
/// Implicit cast from the vale to the optional
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
public static implicit operator Optional<T>(T value)
{
return new Optional<T>(value);
}
/// <summary>
/// Explicit cast from option to valiue
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
public static explicit operator T(Optional<T> value)
{
return value.Value;
}
#region Equality members
/// <summary>
/// Implements the operator ==.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator ==(Optional<T> left, Optional<T> right)
{
return left.Equals(right);
}
/// <summary>
/// Implements the operator !=.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator !=(Optional<T> left, Optional<T> right)
{
return !left.Equals(right);
}
/// <summary>
/// Equalses the specified other.
/// </summary>
/// <param name="other">The other.</param>s
/// <returns></returns>
public bool Equals(Optional<T> other)
{
if (!HasValue) return !other.HasValue;
if (!other.HasValue) return false;
return HasValue.Equals(other.HasValue) && EqualityComparer<T>.Default.Equals(_value, other._value);
}
/// <summary>
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is Optional<T> && Equals((Optional<T>)obj);
}
/// <summary>
/// Retrieves the hash code of the object returned by the <see cref="P:System.Nullable`1.Value"/> property.
/// </summary>
///
/// <returns>
/// The hash code of the object returned by the <see cref="P:System.Nullable`1.Value"/> property if the <see cref="P:System.Nullable`1.HasValue"/> property is true, or zero if the <see cref="P:System.Nullable`1.HasValue"/> property is false.
/// </returns>
public override int GetHashCode()
{
unchecked
{
return (HasValue.GetHashCode() * 397) ^ EqualityComparer<T>.Default.GetHashCode(_value);
}
}
#endregion
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public override string ToString()
{
return !HasValue ? "<None>" : _value.ToString();
}
}
}