Repository navigation
Expand file tree
/
Copy pathOptionExtensions.cs
More file actions
186 lines (163 loc) · 7.31 KB
/
Copy pathOptionExtensions.cs
File metadata and controls
186 lines (163 loc) · 7.31 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
using System;
using System.Collections.Generic;
using System.Linq;
namespace DynamicData.Kernel
{
/// <summary>
/// Extensions for optional
/// </summary>
public static class OptionExtensions
{
/// <summary>
/// Returns the value if the nullable has a value, otherwise returns the result of the value selector
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="defaultValue">The default value</param>
/// <returns></returns>
public static T ValueOr<T>(this T? source, T defaultValue)
where T:struct
{
return source ?? defaultValue;
}
/// <summary>
/// Returns the value if the optional has a value, otherwise returns the result of the value selector
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="valueSelector">The value selector.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">valueSelector</exception>
public static T ValueOr<T>(this Optional<T> source, Func<T> valueSelector )
{
if (valueSelector == null) throw new ArgumentNullException(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"></typeparam>
/// <param name="source">The source.</param>
/// <returns></returns>
public static T ValueOrDefault<T>(this Optional<T> source)
{
return source.HasValue ? source.Value : default(T);
}
/// <summary>
/// Returns the value if the optional has a value, otherwise throws an exception as specified by the exception generator
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="exceptionGenerator">The exception generator.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">exceptionGenerator</exception>
public static T ValueOrThrow<T>(this Optional<T> source, Func<Exception> exceptionGenerator)
{
if (exceptionGenerator == null) throw new ArgumentNullException(nameof(exceptionGenerator));
if (source.HasValue)
return source.Value;
throw exceptionGenerator();
}
/// <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></returns>
/// <exception cref="System.ArgumentNullException">
/// converter
/// or
/// fallbackConverter
/// </exception>
public static TDestination ConvertOr<TSource, TDestination>(this Optional<TSource> source, Func<TSource, TDestination> converter, Func<TDestination> fallbackConverter)
{
if (converter == null) throw new ArgumentNullException(nameof(converter));
if (fallbackConverter == null) throw new ArgumentNullException(nameof(fallbackConverter));
return source.HasValue ? converter(source.Value) : fallbackConverter();
}
/// <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></returns>
/// <exception cref="System.ArgumentNullException">converter</exception>
public static Optional<TDestination> Convert<TSource, TDestination>(this Optional<TSource> source, Func<TSource, TDestination> converter)
{
if (converter == null) throw new ArgumentNullException(nameof(converter));
return source.HasValue ? converter(source.Value) : Optional.None<TDestination>();
}
/// <summary>
/// Filters where Optional<typeparam name="T"></typeparam> has a value
/// and return the values only
/// </summary>
/// <param name="source">The source.</param>
/// <returns></returns>
public static IEnumerable<T> SelectValues<T>(this IEnumerable<Optional<T>> source)
{
return source.Where(t => t.HasValue).Select(t => t.Value);
}
/// <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></returns>
public static Optional<TValue> Lookup<TValue, TKey>(this IDictionary<TKey, TValue> source, TKey key)
{
TValue contained;
bool result = source.TryGetValue(key, out 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></returns>
public static bool RemoveIfContained<TValue, TKey>(this IDictionary<TKey, TValue> source, TKey key)
{
if (source.ContainsKey(key))
return source.Remove(key);
return false;
}
/// <summary>
/// Overloads Enumerable.FirstOrDefault() and wraps the result in a Optional<typeparam>
/// <name>&gt;T</name>
/// </typeparam> container
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="selector">The selector.</param>
/// <returns></returns>
public static Optional<T> FirstOrOptional<T>(this IEnumerable<T> source, Func<T, bool> selector)
{
var result = source.FirstOrDefault(selector);
return !Equals(result, null) ? result : Optional.None<T>();
}
/// <summary>
/// Invokes the specified action when
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="action">The action.</param>
/// <returns></returns>
public static OptionElse IfHasValue<T>(this Optional<T> source, Action<T> action)
{
if (!source.HasValue) return new OptionElse();
action(source.Value);
return OptionElse.NoAction;
}
}
}