forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaybe.cs
More file actions
179 lines (153 loc) · 6.09 KB
/
Maybe.cs
File metadata and controls
179 lines (153 loc) · 6.09 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
using System;
namespace Simple.Data
{
using System.Collections.Generic;
public static class Maybe
{
public static Maybe<T> Some<T>(T value)
{
return Maybe<T>.Some(value);
}
}
public abstract class Maybe<T> : IEquatable<Maybe<T>>
{
public static readonly Maybe<T> None = new NoneClass();
public static Maybe<T> Some(T value)
{
return new SomeClass(value);
}
public abstract bool HasValue { get; }
class NoneClass : Maybe<T>
{
public override T Value
{
get { return default(T); }
}
public override bool HasValue
{
get { return false; }
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
public override bool Equals(Maybe<T> other)
{
return other is NoneClass;
}
/// <summary>
/// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
/// </returns>
/// <filterpriority>2</filterpriority>
public override string ToString()
{
return string.Empty;
}
/// <summary>
/// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
/// </returns>
/// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
public override bool Equals(object obj)
{
return obj is NoneClass;
}
public override int GetHashCode()
{
return 0;
}
}
class SomeClass : Maybe<T>
{
private readonly T _value;
public SomeClass(T value)
{
_value = value;
}
public override T Value
{
get { return _value; }
}
public override bool HasValue
{
get { return true; }
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
public override bool Equals(Maybe<T> other)
{
return other != null && other.HasValue && EqualityComparer<T>.Default.Equals(other.Value, _value);
}
/// <summary>
/// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
/// </returns>
/// <filterpriority>2</filterpriority>
public override string ToString()
{
return _value.ToString();
}
/// <summary>
/// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
/// </returns>
/// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
public override bool Equals(object obj)
{
return Equals(obj as Maybe<T>);
}
public override int GetHashCode()
{
return _value.GetHashCode();
}
}
public abstract T Value
{
get;
}
//public static implicit operator bool(Maybe<T> maybe)
//{
// return maybe is SomeClass;
//}
public static implicit operator Maybe<T>(T value)
{
return new SomeClass(value);
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
public abstract bool Equals(Maybe<T> other);
public static bool operator ==(Maybe<T> left, Maybe<T> right)
{
return Equals(left, right);
}
public static bool operator !=(Maybe<T> left, Maybe<T> right)
{
return !Equals(left, right);
}
public abstract override bool Equals(object obj);
public abstract override int GetHashCode();
public abstract override string ToString();
}
}