Either.cs
5.78 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2017. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
using System;
namespace Leap.Unity {
/// <summary>
/// A data structure that represents either a value of type A or
/// a value of type B. The value can never be both A and B.
/// Neither A nor B can ever be null.
/// </summary>
public struct Either<A, B> : IEquatable<Either<A, B>>, IComparable, IComparable<Either<A, B>> {
/// <summary>
/// Returns whether or not this Either contains the first value.
/// </summary>
public readonly bool isA;
/// <summary>
/// Returns whether or not this Either contains the second value.
/// </summary>
public bool isB {
get {
return !isA;
}
}
private readonly A _a;
private readonly B _b;
/// <summary>
/// Returns a Maybe that contains the value of A if it exists,
/// or no value if it doesn't.
/// </summary>
public Maybe<A> a {
get {
if (isA) {
return Maybe<A>.Some(_a);
} else {
return Maybe<A>.None;
}
}
}
/// <summary>
/// Returns a Maybe that contains the value of B if it exists,
/// or no value if it doesn't.
/// </summary>
public Maybe<B> b {
get {
if (isA) {
return Maybe<B>.None;
} else {
return Maybe<B>.Some(_b);
}
}
}
/// <summary>
/// Constructs an Either with a value of A.
/// </summary>
public Either(A a) {
if (a == null) {
throw new ArgumentNullException("Cannot initialize an Either with a null value.");
}
isA = true;
_a = a;
_b = default(B);
}
/// <summary>
/// Constructs an Either with a value of B.
/// </summary>
public Either(B b) {
if (b == null) {
throw new ArgumentNullException("Cannot initialize an Either with a null value.");
}
isA = false;
_b = b;
_a = default(A);
}
/// <summary>
/// Calls the first delegate with the value of A if it is present,
/// else calls the second delegate with the value of B.
/// </summary>
public void Match(Action<A> ifA, Action<B> ifB) {
if (isA) {
if (ifA != null) ifA(_a);
} else {
if (ifB != null) ifB(_b);
}
}
/// <summary>
/// If this either contains the value of A, the out argument is filled with
/// that value and this method returns true, else it returns false.
/// </summary>
public bool TryGetA(out A a) {
a = _a;
return isA;
}
/// <summary>
/// If this either contains the value of B, the out argument is filled with
/// that value and this method returns true, else it returns false.
/// </summary>
public bool TryGetB(out B b) {
b = _b;
return !isA;
}
public override int GetHashCode() {
if (isA) {
return _a.GetHashCode();
} else {
return _b.GetHashCode();
}
}
public override bool Equals(object obj) {
if (obj is Either<A, B>) {
return Equals((Either<A, B>)obj);
} else {
return false;
}
}
public bool Equals(Either<A, B> other) {
if (isA != other.isA) {
return false;
} else if (isA) {
return _a.Equals(other._a);
} else {
return _b.Equals(other._b);
}
}
public int CompareTo(object obj) {
if (!(obj is Either<A, B>)) {
throw new ArgumentException();
} else {
return CompareTo((Either<A, B>)obj);
}
}
public int CompareTo(Either<A, B> other) {
if (isA != other.isA) {
return isA ? -1 : 1;
} else if (isA) {
IComparable<A> ca = _a as IComparable<A>;
if (ca != null) {
return ca.CompareTo(other._a);
} else {
IComparable c = _a as IComparable;
if (c != null) {
return c.CompareTo(other._b);
} else {
return 0;
}
}
} else {
IComparable<B> cb = _b as IComparable<B>;
if (cb != null) {
return cb.CompareTo(other._b);
} else {
IComparable c = _b as IComparable;
if (c != null) {
return c.CompareTo(other._b);
} else {
return 0;
}
}
}
}
public static bool operator ==(Either<A, B> either0, Either<A, B> either1) {
return either0.Equals(either1);
}
public static bool operator !=(Either<A, B> either0, Either<A, B> either1) {
return !either0.Equals(either1);
}
public static bool operator >(Either<A, B> either0, Either<A, B> either1) {
return either0.CompareTo(either1) > 0;
}
public static bool operator >=(Either<A, B> either0, Either<A, B> either1) {
return either0.CompareTo(either1) >= 0;
}
public static bool operator <(Either<A, B> either0, Either<A, B> either1) {
return either0.CompareTo(either1) < 0;
}
public static bool operator <=(Either<A, B> either0, Either<A, B> either1) {
return either0.CompareTo(either1) <= 0;
}
public static implicit operator Either<A, B>(A a) {
return new Either<A, B>(a);
}
public static implicit operator Either<A, B>(B b) {
return new Either<A, B>(b);
}
}
}