Skip to content

Commit b4e3564

Browse files
feature/api update (#29)
Updated collection creation and return types changed to IReadOnlyList
1 parent 5874ffb commit b4e3564

File tree

9 files changed

+61
-42
lines changed

9 files changed

+61
-42
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ root = true
22

33
[*]
44
max_line_length = 140
5-
insert_final_newline = true
5+
insert_final_newline = true

OnixLabs.Core.UnitTests/PreconditionTests.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,40 @@ public sealed class PreconditionTests
2323
[Fact(DisplayName = "Check should throw an ArgumentException when the condition is false")]
2424
public void CheckShouldProduceExpectedResult()
2525
{
26-
Assert.Throws<InvalidOperationException>(() => Check(false));
26+
// When
27+
InvalidOperationException exception = Assert.Throws<InvalidOperationException>(() => Check(false));
28+
29+
// Then
30+
Assert.Equal("Check requirement failed.", exception.Message);
2731
}
2832

2933
[Fact(DisplayName = "CheckNotNull should throw an ArgumentNullException when the condition is null")]
3034
public void CheckNotNullShouldProduceExpectedResult()
3135
{
32-
Assert.Throws<InvalidOperationException>(() => CheckNotNull<object>(null));
36+
// When
37+
InvalidOperationException exception = Assert.Throws<InvalidOperationException>(() => CheckNotNull<object>(null));
38+
39+
// Then
40+
Assert.Equal("Argument must not be null.", exception.Message);
3341
}
3442

3543
[Fact(DisplayName = "Require should throw an ArgumentException when the condition is false")]
3644
public void RequireShouldProduceExpectedResult()
3745
{
38-
Assert.Throws<ArgumentException>(() => Require(false));
46+
// When
47+
ArgumentException exception = Assert.Throws<ArgumentException>(() => Require(false));
48+
49+
// Then
50+
Assert.Equal("Argument requirement failed.", exception.Message);
3951
}
4052

4153
[Fact(DisplayName = "RequireNotNull should throw an ArgumentNullException when the condition is null")]
4254
public void RequireNotNullShouldProduceExpectedResult()
4355
{
44-
Assert.Throws<ArgumentNullException>(() => RequireNotNull<object>(null));
56+
// When
57+
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(() => RequireNotNull<object>(null));
58+
59+
// Then
60+
Assert.Equal("Argument must not be null.", exception.Message);
4561
}
4662
}

OnixLabs.Core/Collections/Collection.Empty.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static T[] EmptyArray<T>()
5151
/// <returns>Returns an empty immutable array.</returns>
5252
public static ImmutableArray<T> EmptyImmutableArray<T>()
5353
{
54-
return EmptyArray<T>().ToImmutableArray();
54+
return ImmutableArray<T>.Empty;
5555
}
5656

5757
/// <summary>
@@ -71,7 +71,7 @@ public static List<T> EmptyList<T>()
7171
/// <returns>Returns an empty immutable list.</returns>
7272
public static ImmutableList<T> EmptyImmutableList<T>()
7373
{
74-
return EmptyList<T>().ToImmutableList();
74+
return ImmutableList<T>.Empty;
7575
}
7676

7777
/// <summary>
@@ -93,7 +93,7 @@ public static Dictionary<TKey, TValue> EmptyDictionary<TKey, TValue>() where TKe
9393
/// <returns>Returns an empty immutable dictionary.</returns>
9494
public static ImmutableDictionary<TKey, TValue> EmptyImmutableDictionary<TKey, TValue>() where TKey : notnull
9595
{
96-
return EmptyDictionary<TKey, TValue>().ToImmutableDictionary();
96+
return ImmutableDictionary<TKey, TValue>.Empty;
9797
}
9898

9999
/// <summary>
@@ -115,7 +115,7 @@ public static SortedDictionary<TKey, TValue> EmptySortedDictionary<TKey, TValue>
115115
/// <returns>Returns an empty immutable sorted dictionary.</returns>
116116
public static ImmutableSortedDictionary<TKey, TValue> EmptyImmutableSortedDictionary<TKey, TValue>() where TKey : notnull
117117
{
118-
return EmptySortedDictionary<TKey, TValue>().ToImmutableSortedDictionary();
118+
return ImmutableSortedDictionary<TKey, TValue>.Empty;
119119
}
120120

121121
/// <summary>
@@ -135,7 +135,7 @@ public static HashSet<T> EmptyHashSet<T>()
135135
/// <returns>Returns an empty immutable hash set.</returns>
136136
public static ImmutableHashSet<T> EmptyImmutableHashSet<T>()
137137
{
138-
return EmptyHashSet<T>().ToImmutableHashSet();
138+
return ImmutableHashSet<T>.Empty;
139139
}
140140

141141
/// <summary>
@@ -155,7 +155,7 @@ public static SortedSet<T> EmptySortedSet<T>()
155155
/// <returns>Returns an empty immutable sorted set.</returns>
156156
public static ImmutableSortedSet<T> EmptyImmutableSortedSet<T>()
157157
{
158-
return EmptySortedSet<T>().ToImmutableSortedSet();
158+
return ImmutableSortedSet<T>.Empty;
159159
}
160160

161161
/// <summary>
@@ -175,7 +175,7 @@ public static Stack<T> EmptyStack<T>()
175175
/// <returns>Returns an empty immutable stack.</returns>
176176
public static ImmutableStack<T> EmptyImmutableStack<T>()
177177
{
178-
return ImmutableStack.Create<T>();
178+
return ImmutableStack<T>.Empty;
179179
}
180180

181181
/// <summary>
@@ -195,6 +195,6 @@ public static Queue<T> EmptyQueue<T>()
195195
/// <returns>Returns an empty immutable queue.</returns>
196196
public static ImmutableQueue<T> EmptyImmutableQueue<T>()
197197
{
198-
return ImmutableQueue.Create<T>();
198+
return ImmutableQueue<T>.Empty;
199199
}
200200
}

OnixLabs.Core/Collections/Collection.Of.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static partial class Collection
2828
/// <returns>Returns an enumerable populated with the specified items.</returns>
2929
public static IEnumerable<T> EnumerableOf<T>(params T[] items)
3030
{
31-
return items.Copy();
31+
return items;
3232
}
3333

3434
/// <summary>
@@ -39,7 +39,7 @@ public static IEnumerable<T> EnumerableOf<T>(params T[] items)
3939
/// <returns>Returns an array populated with the specified items.</returns>
4040
public static T[] ArrayOf<T>(params T[] items)
4141
{
42-
return items.Copy();
42+
return items;
4343
}
4444

4545
/// <summary>
@@ -50,7 +50,7 @@ public static T[] ArrayOf<T>(params T[] items)
5050
/// <returns>Returns an immutable array populated with the specified items.</returns>
5151
public static ImmutableArray<T> ImmutableArrayOf<T>(params T[] items)
5252
{
53-
return ArrayOf(items).ToImmutableArray();
53+
return ImmutableArray.Create(items);
5454
}
5555

5656
/// <summary>
@@ -61,7 +61,7 @@ public static ImmutableArray<T> ImmutableArrayOf<T>(params T[] items)
6161
/// <returns>Returns a list populated with the specified items.</returns>
6262
public static List<T> ListOf<T>(params T[] items)
6363
{
64-
return new List<T>(items.Copy());
64+
return new List<T>(items);
6565
}
6666

6767
/// <summary>
@@ -72,7 +72,7 @@ public static List<T> ListOf<T>(params T[] items)
7272
/// <returns>Returns an immutable list populated with the specified items.</returns>
7373
public static ImmutableList<T> ImmutableListOf<T>(params T[] items)
7474
{
75-
return ListOf(items).ToImmutableList();
75+
return ImmutableList.Create(items);
7676
}
7777

7878
/// <summary>
@@ -84,7 +84,7 @@ public static ImmutableList<T> ImmutableListOf<T>(params T[] items)
8484
/// <returns>Returns a dictionary populated with the specified items.</returns>
8585
public static Dictionary<TKey, TValue> DictionaryOf<TKey, TValue>(params KeyValuePair<TKey, TValue>[] items) where TKey : notnull
8686
{
87-
return new Dictionary<TKey, TValue>(items.Copy());
87+
return new Dictionary<TKey, TValue>(items);
8888
}
8989

9090
/// <summary>
@@ -109,7 +109,7 @@ public static Dictionary<TKey, TValue> DictionaryOf<TKey, TValue>(params (TKey k
109109
public static ImmutableDictionary<TKey, TValue> ImmutableDictionaryOf<TKey, TValue>(
110110
params KeyValuePair<TKey, TValue>[] items) where TKey : notnull
111111
{
112-
return DictionaryOf(items).ToImmutableDictionary();
112+
return ImmutableDictionary.CreateRange(items);
113113
}
114114

115115
/// <summary>
@@ -122,7 +122,7 @@ public static ImmutableDictionary<TKey, TValue> ImmutableDictionaryOf<TKey, TVal
122122
public static ImmutableDictionary<TKey, TValue> ImmutableDictionaryOf<TKey, TValue>(
123123
params (TKey key, TValue value)[] items) where TKey : notnull
124124
{
125-
return DictionaryOf(items).ToImmutableDictionary();
125+
return ImmutableDictionary.CreateRange(items.Select(item => new KeyValuePair<TKey, TValue>(item.key, item.value)));
126126
}
127127

128128
/// <summary>
@@ -161,7 +161,7 @@ public static SortedDictionary<TKey, TValue> SortedDictionaryOf<TKey, TValue>(
161161
public static ImmutableSortedDictionary<TKey, TValue> ImmutableSortedDictionaryOf<TKey, TValue>(
162162
params KeyValuePair<TKey, TValue>[] items) where TKey : notnull
163163
{
164-
return SortedDictionaryOf(items).ToImmutableSortedDictionary();
164+
return ImmutableSortedDictionary.CreateRange(items);
165165
}
166166

167167
/// <summary>
@@ -174,7 +174,7 @@ public static ImmutableSortedDictionary<TKey, TValue> ImmutableSortedDictionaryO
174174
public static ImmutableSortedDictionary<TKey, TValue> ImmutableSortedDictionaryOf<TKey, TValue>(
175175
params (TKey key, TValue value)[] items) where TKey : notnull
176176
{
177-
return SortedDictionaryOf(items).ToImmutableSortedDictionary();
177+
return ImmutableSortedDictionary.CreateRange(items.Select(item => new KeyValuePair<TKey, TValue>(item.key, item.value)));
178178
}
179179

180180
/// <summary>
@@ -185,7 +185,7 @@ public static ImmutableSortedDictionary<TKey, TValue> ImmutableSortedDictionaryO
185185
/// <returns>Returns a hash set populated with the specified items.</returns>
186186
public static HashSet<T> HashSetOf<T>(params T[] items)
187187
{
188-
return new HashSet<T>(items.Copy());
188+
return new HashSet<T>(items);
189189
}
190190

191191
/// <summary>
@@ -196,7 +196,7 @@ public static HashSet<T> HashSetOf<T>(params T[] items)
196196
/// <returns>Returns an immutable hash set populated with the specified items.</returns>
197197
public static ImmutableHashSet<T> ImmutableHashSetOf<T>(params T[] items)
198198
{
199-
return HashSetOf(items).ToImmutableHashSet();
199+
return ImmutableHashSet.Create(items);
200200
}
201201

202202
/// <summary>
@@ -207,7 +207,7 @@ public static ImmutableHashSet<T> ImmutableHashSetOf<T>(params T[] items)
207207
/// <returns>Returns a sorted set populated with the specified items.</returns>
208208
public static SortedSet<T> SortedSetOf<T>(params T[] items)
209209
{
210-
return new SortedSet<T>(items.Copy());
210+
return new SortedSet<T>(items);
211211
}
212212

213213
/// <summary>
@@ -218,7 +218,7 @@ public static SortedSet<T> SortedSetOf<T>(params T[] items)
218218
/// <returns>Returns an immutable sorted set populated with the specified items.</returns>
219219
public static ImmutableSortedSet<T> ImmutableSortedSetOf<T>(params T[] items)
220220
{
221-
return SortedSetOf(items).ToImmutableSortedSet();
221+
return ImmutableSortedSet.Create(items);
222222
}
223223

224224
/// <summary>
@@ -229,7 +229,7 @@ public static ImmutableSortedSet<T> ImmutableSortedSetOf<T>(params T[] items)
229229
/// <returns>Returns a stack populated with the specified items.</returns>
230230
public static Stack<T> StackOf<T>(params T[] items)
231231
{
232-
return new Stack<T>(items.Copy());
232+
return new Stack<T>(items);
233233
}
234234

235235
/// <summary>
@@ -240,7 +240,7 @@ public static Stack<T> StackOf<T>(params T[] items)
240240
/// <returns>Returns an immutable stack populated with the specified items.</returns>
241241
public static ImmutableStack<T> ImmutableStackOf<T>(params T[] items)
242242
{
243-
return ImmutableStack.Create(items.Copy());
243+
return ImmutableStack.Create(items);
244244
}
245245

246246
/// <summary>
@@ -251,7 +251,7 @@ public static ImmutableStack<T> ImmutableStackOf<T>(params T[] items)
251251
/// <returns>Returns a queue populated with the specified items.</returns>
252252
public static Queue<T> QueueOf<T>(params T[] items)
253253
{
254-
return new Queue<T>(items.Copy());
254+
return new Queue<T>(items);
255255
}
256256

257257
/// <summary>
@@ -262,6 +262,6 @@ public static Queue<T> QueueOf<T>(params T[] items)
262262
/// <returns>Returns an immutable queue populated with the specified items.</returns>
263263
public static ImmutableQueue<T> ImmutableQueueOf<T>(params T[] items)
264264
{
265-
return ImmutableQueue.Create(items.Copy());
265+
return ImmutableQueue.Create(items);
266266
}
267267
}

OnixLabs.Core/Enumeration.Get.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,22 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
using System.Collections.Generic;
1516
using System.Collections.Immutable;
1617
using System.Linq;
1718
using System.Reflection;
1819
using OnixLabs.Core.Linq;
1920

2021
namespace OnixLabs.Core;
2122

23+
// ReSharper disable ReturnTypeCanBeEnumerable.Global
2224
public abstract partial class Enumeration<T>
2325
{
2426
/// <summary>
2527
/// Gets all of the enumeration entries for the current type.
2628
/// </summary>
2729
/// <returns>Returns all of the enumeration entries for the current type.</returns>
28-
public static ImmutableHashSet<T> GetAll()
30+
public static IReadOnlySet<T> GetAll()
2931
{
3032
return typeof(T)
3133
.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)
@@ -38,7 +40,7 @@ public static ImmutableHashSet<T> GetAll()
3840
/// Gets all of the enumeration entries for the current type.
3941
/// </summary>
4042
/// <returns>Returns all of the enumeration entries for the current type.</returns>
41-
public static ImmutableHashSet<(int Value, string Name)> GetEntries()
43+
public static IReadOnlySet<(int Value, string Name)> GetEntries()
4244
{
4345
return GetAll().Select(entry => entry.ToEntry()).ToImmutableHashSet();
4446
}
@@ -47,7 +49,7 @@ public static ImmutableHashSet<T> GetAll()
4749
/// Gets all of the enumeration names for the current type.
4850
/// </summary>
4951
/// <returns>Returns all of the enumeration names for the current type.</returns>
50-
public static ImmutableHashSet<string> GetNames()
52+
public static IReadOnlySet<string> GetNames()
5153
{
5254
return GetAll().Select(entry => entry.Name).ToImmutableHashSet();
5355
}
@@ -56,7 +58,7 @@ public static ImmutableHashSet<string> GetNames()
5658
/// Gets all of the enumeration values for the current type.
5759
/// </summary>
5860
/// <returns>Returns all of the enumeration values for the current type.</returns>
59-
public static ImmutableHashSet<int> GetValues()
61+
public static IReadOnlySet<int> GetValues()
6062
{
6163
return GetAll().Select(entry => entry.Value).ToImmutableHashSet();
6264
}

OnixLabs.Core/Preconditions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static class Preconditions
2929
/// <param name="message">The exception message to throw in the event that the condition fails.</param>
3030
/// <exception cref="InvalidOperationException">If the condition fails.</exception>
3131
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
32-
public static void Check(bool condition, string message = "Check failed.")
32+
public static void Check(bool condition, string message = "Check requirement failed.")
3333
{
3434
if (!condition) throw new InvalidOperationException(message);
3535
}
@@ -43,7 +43,7 @@ public static void Check(bool condition, string message = "Check failed.")
4343
/// <returns>Returns the specified value as non-nullable in the event that the value is not null.</returns>
4444
/// <exception cref="InvalidOperationException">If the condition fails because the value is null.</exception>
4545
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
46-
public static T CheckNotNull<T>(T? value, string message = "Null check failed.") where T : notnull
46+
public static T CheckNotNull<T>(T? value, string message = "Argument must not be null.") where T : notnull
4747
{
4848
return value ?? throw new InvalidOperationException(message);
4949
}
@@ -71,8 +71,9 @@ public static void Require(bool condition, string message = "Argument requiremen
7171
/// <returns>Returns the specified value as non-nullable in the event that the value is not null.</returns>
7272
/// <exception cref="ArgumentNullException">If the condition fails because the value is null.</exception>
7373
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
74-
public static T RequireNotNull<T>(T? value, string message = "Argument must be null.", string? parameterName = null) where T : notnull
74+
public static T RequireNotNull<T>(T? value, string message = "Argument must not be null.", string? parameterName = null)
75+
where T : notnull
7576
{
76-
return value ?? throw new ArgumentNullException(message, parameterName);
77+
return value ?? throw new ArgumentNullException(parameterName, message);
7778
}
7879
}

OnixLabs.Playground/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace OnixLabs.Playground;
1616

1717
internal static class Program
1818
{
19-
private static void Main(string[] args)
19+
public static void Main()
2020
{
2121
}
2222
}

OnixLabs.Security.Cryptography/MerkleTree.Generic.Get.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public MerkleTree ToMerkleTree()
3333
/// Obtains the leaf values from the current <see cref="MerkleTree{T}"/>.
3434
/// </summary>
3535
/// <returns>Returns an <see cref="IEnumerable{T}"/> containing the leaf values from the current <see cref="MerkleTree{T}"/>.</returns>
36-
public ImmutableList<T> GetLeafValues()
36+
public IReadOnlyList<T> GetLeafValues()
3737
{
3838
ICollection<T> result = EmptyList<T>();
3939
CollectLeafValues(this, result);

OnixLabs.Security.Cryptography/MerkleTree.Get.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public abstract partial class MerkleTree
2323
/// Obtains the leaf hashes from the current <see cref="MerkleTree"/>.
2424
/// </summary>
2525
/// <returns>Returns an <see cref="IEnumerable{T}"/> containing the leaf hashes from the current <see cref="MerkleTree"/>.</returns>
26-
public ImmutableList<Hash> GetLeafHashes()
26+
public IReadOnlyList<Hash> GetLeafHashes()
2727
{
2828
ICollection<Hash> result = EmptyList<Hash>();
2929
CollectLeafHashes(this, result);

0 commit comments

Comments
 (0)