Repository navigation
Expand file tree
/
Copy pathOptionExtensions.cs
More file actions
270 lines (237 loc) · 10.6 KB
/
Copy pathOptionExtensions.cs
File metadata and controls
270 lines (237 loc) · 10.6 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright (c) 2011-2023 Roland Pheasant. All rights reserved.
// Roland Pheasant licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System.Linq;
namespace DynamicData.Kernel;
/// <summary>
/// Extensions for optional.
/// </summary>
public static class OptionExtensions
{
/// <summary>
/// Converts the specified source.
/// </summary>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
/// <param name="source">The source.</param>
/// <param name="converter">The converter.</param>
/// <returns>The converted value.</returns>
/// <exception cref="ArgumentNullException">converter.</exception>
public static Optional<TDestination> Convert<TSource, TDestination>(this in Optional<TSource> source, Func<TSource, TDestination> converter)
where TSource : notnull
where TDestination : notnull
{
converter.ThrowArgumentNullExceptionIfNull(nameof(converter));
return source.HasValue ? converter(source.Value) : Optional.None<TDestination>();
}
/// <summary>
/// Attempts to converts the specified source, but the conversion might result in a None value.
/// </summary>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
/// <param name="source">The source.</param>
/// <param name="converter">The converter that returns an optional value.</param>
/// <returns>The converted value.</returns>
/// <exception cref="ArgumentNullException">converter.</exception>
public static Optional<TDestination> Convert<TSource, TDestination>(this in Optional<TSource> source, Func<TSource, Optional<TDestination>> converter)
where TSource : notnull
where TDestination : notnull
{
converter.ThrowArgumentNullExceptionIfNull(nameof(converter));
return source.HasValue ? converter(source.Value) : Optional.None<TDestination>();
}
/// <summary>
/// Converts the option value if it has a value, otherwise returns the result of the fallback converter.
/// </summary>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
/// <param name="source">The source.</param>
/// <param name="converter">The converter.</param>
/// <param name="fallbackConverter">The fallback converter.</param>
/// <returns>The destination value.</returns>
/// <exception cref="ArgumentNullException">
/// converter
/// or
/// fallbackConverter.
/// </exception>
public static TDestination? ConvertOr<TSource, TDestination>(this in Optional<TSource> source, Func<TSource?, TDestination?> converter, Func<TDestination?> fallbackConverter)
where TSource : notnull
{
converter.ThrowArgumentNullExceptionIfNull(nameof(converter));
fallbackConverter.ThrowArgumentNullExceptionIfNull(nameof(fallbackConverter));
return source.HasValue ? converter(source.Value) : fallbackConverter();
}
/// <summary>
/// Returns the original optional if it has a value, otherwise returns the result of the fallback operation.
/// </summary>
/// <typeparam name="T">The type of the source.</typeparam>
/// <param name="source">The source.</param>
/// <param name="fallbackOperation">The fallback operation.</param>
/// <returns>The original value or the result of the fallback operation.</returns>
/// <exception cref="ArgumentNullException">
/// converter
/// or
/// fallbackOperation.
/// </exception>
public static Optional<T> OrElse<T>(this in Optional<T> source, Func<Optional<T>> fallbackOperation)
where T : notnull
{
fallbackOperation.ThrowArgumentNullExceptionIfNull(nameof(fallbackOperation));
return source.HasValue ? source : fallbackOperation();
}
/// <summary>
/// Overloads Enumerable.FirstOrDefault() and wraps the result in a Optional<typeparam>
/// <name>&gt;T</name>
/// </typeparam> container.
/// </summary>
/// <typeparam name="T">The type of the item.</typeparam>
/// <param name="source">The source.</param>
/// <param name="selector">The selector.</param>
/// <returns>The first value or none.</returns>
public static Optional<T> FirstOrOptional<T>(this IEnumerable<T> source, Func<T, bool> selector)
where T : notnull
{
source.ThrowArgumentNullExceptionIfNull(nameof(source));
selector.ThrowArgumentNullExceptionIfNull(nameof(selector));
foreach (var item in source.Where(item => selector(item)))
{
return Optional<T>.Create(item);
}
return Optional.None<T>();
}
/// <summary>
/// Invokes the specified action when.
/// </summary>
/// <typeparam name="T">The type of the item.</typeparam>
/// <param name="source">The source.</param>
/// <param name="action">The action.</param>
/// <returns>The optional else extension.</returns>
public static OptionElse IfHasValue<T>(this in Optional<T> source, Action<T> action)
where T : notnull
{
if (!source.HasValue || source.Value is null)
{
return new OptionElse();
}
action.ThrowArgumentNullExceptionIfNull(nameof(action));
action(source.Value);
return OptionElse.NoAction;
}
/// <summary>
/// Invokes the specified action when.
/// </summary>
/// <typeparam name="T">The type of the item.</typeparam>
/// <param name="source">The source.</param>
/// <param name="action">The action.</param>
/// <returns>The optional else extension.</returns>
public static OptionElse IfHasValue<T>(this Optional<T>? source, Action<T> action)
where T : notnull
{
if (!source.HasValue)
{
return new OptionElse();
}
if (!source.Value.HasValue)
{
return new OptionElse();
}
action.ThrowArgumentNullExceptionIfNull(nameof(action));
action(source.Value.Value);
return OptionElse.NoAction;
}
/// <summary>
/// Overloads a TryGetValue of the dictionary wrapping the result as an Optional.<typeparam>
/// <name>&gt;TValue</name>
/// </typeparam>
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="key">The key.</param>
/// <returns>The option of the looked up value.</returns>
public static Optional<TValue> Lookup<TValue, TKey>(this IDictionary<TKey, TValue> source, TKey key)
where TValue : notnull
{
source.ThrowArgumentNullExceptionIfNull(nameof(source));
var result = source.TryGetValue(key, out var contained);
return result ? contained : Optional.None<TValue>();
}
/// <summary>
/// Removes item if contained in the cache.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="key">The key.</param>
/// <returns>If the item was removed.</returns>
public static bool RemoveIfContained<TValue, TKey>(this IDictionary<TKey, TValue> source, TKey key)
{
source.ThrowArgumentNullExceptionIfNull(nameof(source));
return source.ContainsKey(key) && source.Remove(key);
}
/// <summary>
/// Filters where Optional has a value
/// and return the values only.
/// </summary>
/// <typeparam name="T">The type of item.</typeparam>
/// <param name="source">The source.</param>
/// <returns>An enumerable of the selected items.</returns>
public static IEnumerable<T> SelectValues<T>(this IEnumerable<Optional<T>> source)
where T : notnull => source.Where(t => t.HasValue && t.Value is not null).Select(t => t.Value!);
/// <summary>
/// Returns the value if the nullable has a value, otherwise returns the result of the value selector.
/// </summary>
/// <typeparam name="T">The type of the item.</typeparam>
/// <param name="source">The source.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value or the default value.</returns>
public static T ValueOr<T>(this T? source, T defaultValue)
where T : struct => source ?? defaultValue;
/// <summary>
/// Returns the value if the optional has a value, otherwise returns the result of the value selector.
/// </summary>
/// <typeparam name="T">The type of the item.</typeparam>
/// <param name="source">The source.</param>
/// <param name="valueSelector">The value selector.</param>
/// <returns>If the value or a provided default.</returns>
/// <exception cref="ArgumentNullException">valueSelector.</exception>
public static T ValueOr<T>(this in Optional<T> source, Func<T> valueSelector)
where T : notnull
{
valueSelector.ThrowArgumentNullExceptionIfNull(nameof(valueSelector));
return source.HasValue ? source.Value : valueSelector();
}
/// <summary>
/// Returns the value if the optional has a value, otherwise returns the default value of T.
/// </summary>
/// <typeparam name="T">The type of the item.</typeparam>
/// <param name="source">The source.</param>
/// <returns>The value or default.</returns>
public static T? ValueOrDefault<T>(this in Optional<T> source)
where T : notnull
{
if (source.HasValue)
{
return source.Value;
}
return default;
}
/// <summary>
/// Returns the value if the optional has a value, otherwise throws an exception as specified by the exception generator.
/// </summary>
/// <typeparam name="T">The type of the item.</typeparam>
/// <param name="source">The source.</param>
/// <param name="exceptionGenerator">The exception generator.</param>
/// <returns>The value.</returns>
/// <exception cref="ArgumentNullException">exceptionGenerator.</exception>
public static T ValueOrThrow<T>(this in Optional<T> source, Func<Exception> exceptionGenerator)
where T : notnull
{
exceptionGenerator.ThrowArgumentNullExceptionIfNull(nameof(exceptionGenerator));
if (source.HasValue && source.Value is not null)
{
return source.Value;
}
throw exceptionGenerator();
}
}