Skip to content

Commit 000a037

Browse files
authored
Merge pull request #43 from I-RzR-I/feature/Ext_122025_p1
Add ConcurrentDictionary, long, and type extension methods
2 parents 1d0737a + cf2bd18 commit 000a037

7 files changed

Lines changed: 196 additions & 49 deletions

File tree

docs/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
### **v4.2.0.8401** [[RzR](mailto:108324929+I-RzR-I@users.noreply.github.com)] 03-12-2025
2+
* [e0cfbed] (RzR) -> Auto commit uncommited files
3+
* [17cf91a] (RzR) -> Add type extension method: `IsAssignableFromPortable`.
4+
* [1b4d01c] (RzR) -> Add ConcurrentDictionary extension method: `AddOrUpdate`.
5+
* [e9ff67e] (RzR) -> Add long extension method: `AsReadableFileSize`.
6+
17
### **v4.1.0.8241** [[RzR](mailto:108324929+I-RzR-I@users.noreply.github.com)] 11-11-2025
28
* [0c9f8d8] (RzR) -> Auto commit uncommited files
39
* [75871a1] (RzR) -> Add INI file reader helper.

src/DomainCommonExtensions/ArraysExtensions/ConcurrentDictionaryExtensions.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.Collections.Concurrent;
2121
using System.Collections.Generic;
2222
using System.Linq;
23+
using DomainCommonExtensions.Utilities.Ensure;
2324

2425
#endregion
2526

@@ -32,6 +33,61 @@ namespace DomainCommonExtensions.ArraysExtensions
3233
/// =================================================================================================
3334
public static class ConcurrentDictionaryExtensions
3435
{
36+
/// -------------------------------------------------------------------------------------------------
37+
/// <summary>
38+
/// A ConcurrentDictionary&lt;TKey,TValue&gt; extension method that adds an or update key and value
39+
/// </summary>
40+
/// <typeparam name="TKey">Type of the key.</typeparam>
41+
/// <typeparam name="TValue">Type of the value.</typeparam>
42+
/// <param name="source">The source to act on.</param>
43+
/// <param name="key">The key.</param>
44+
/// <param name="value">The value.</param>
45+
/// =================================================================================================
46+
public static void AddOrUpdate<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> source,
47+
TKey key, TValue value)
48+
{
49+
DomainEnsure.IsNotNull(source, nameof(source));
50+
DomainEnsure.IsNotNull(key, nameof(key));
51+
52+
source.AddOrUpdate(key, value, (oldKey, oldValue) => value);
53+
}
54+
55+
/// -------------------------------------------------------------------------------------------------
56+
/// <summary>
57+
/// A ConcurrentDictionary&lt;TKey,TValue&gt; extension method that adds an or update key and value
58+
/// </summary>
59+
/// <typeparam name="TKey">Type of the key.</typeparam>
60+
/// <typeparam name="TValue">Type of the value.</typeparam>
61+
/// <param name="source">The source to act on.</param>
62+
/// <param name="keyValuePair">The key value pair.</param>
63+
/// =================================================================================================
64+
public static void AddOrUpdate<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> source,
65+
KeyValuePair<TKey, TValue> keyValuePair)
66+
{
67+
DomainEnsure.IsNotNull(source, nameof(source));
68+
DomainEnsure.IsNotNull(keyValuePair, nameof(keyValuePair));
69+
70+
source.AddOrUpdate(keyValuePair.Key, keyValuePair.Value);
71+
}
72+
73+
/// -------------------------------------------------------------------------------------------------
74+
/// <summary>
75+
/// A ConcurrentDictionary&lt;TKey,TValue&gt; extension method that adds an or update key and value
76+
/// </summary>
77+
/// <typeparam name="TKey">Type of the key.</typeparam>
78+
/// <typeparam name="TValue">Type of the value.</typeparam>
79+
/// <param name="source">The source to act on.</param>
80+
/// <param name="keyValuePairs">The key value pairs.</param>
81+
/// =================================================================================================
82+
public static void AddOrUpdate<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> source,
83+
IEnumerable<KeyValuePair<TKey, TValue>> keyValuePairs)
84+
{
85+
DomainEnsure.IsNotNull(source, nameof(source));
86+
DomainEnsure.IsNotNull(keyValuePairs, nameof(keyValuePairs));
87+
88+
keyValuePairs.ForEach(source.AddOrUpdate);
89+
}
90+
3591
/// -------------------------------------------------------------------------------------------------
3692
/// <summary>
3793
/// A ConcurrentDictionary&lt;TKey,TValue&gt; extension method that removes this object.

src/DomainCommonExtensions/CommonExtensions/Reflection/TypeExtensions.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
#region U S A G E S
1818

19+
using DomainCommonExtensions.ArraysExtensions;
20+
using DomainCommonExtensions.DataTypeExtensions;
21+
using DomainCommonExtensions.Utilities.Ensure;
1922
using System;
2023
using System.Collections.Generic;
2124
using System.Linq;
2225
using System.Reflection;
2326
using System.Runtime.CompilerServices;
2427
using System.Text;
25-
using DomainCommonExtensions.ArraysExtensions;
26-
using DomainCommonExtensions.DataTypeExtensions;
27-
using DomainCommonExtensions.Utilities.Ensure;
2828

2929
// ReSharper disable ExpressionIsAlwaysNull
3030
// ReSharper disable PossibleMultipleEnumeration
@@ -581,5 +581,30 @@ public static bool HasAttribute<T>(this Type type, Func<T, bool> predicate) wher
581581

582582
return ((IEnumerable<T>)attributes).HasAny(predicate);
583583
}
584+
585+
586+
#if NETSTANDARD1_0_OR_GREATER
587+
588+
/// -------------------------------------------------------------------------------------------------
589+
/// <summary>
590+
/// A Type extension method that query if 'baseType' is assignable from portable.
591+
/// </summary>
592+
/// <param name="baseType">The baseType to act on.</param>
593+
/// <param name="derivedType">Type of the derived.</param>
594+
/// <returns>
595+
/// True if assignable from portable, false if not.
596+
/// </returns>
597+
/// =================================================================================================
598+
public static bool IsAssignableFromPortable(this Type baseType, Type derivedType)
599+
{
600+
601+
#if NETSTANDARD1_0 || NETSTANDARD1_5
602+
return baseType.GetTypeInfo().IsAssignableFrom(derivedType.GetTypeInfo());
603+
#else
604+
return baseType.IsAssignableFrom(derivedType);
605+
#endif
606+
}
607+
608+
#endif
584609
}
585610
}

src/DomainCommonExtensions/DataTypeExtensions/LongExtensions.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,49 @@ public static bool IsLessZero(this long? value)
138138
{
139139
return (value ?? 0) < 0;
140140
}
141+
142+
/// -------------------------------------------------------------------------------------------------
143+
/// <summary>
144+
/// A long extension method that converts a size to a readable file size.
145+
/// </summary>
146+
/// <param name="size">The size to act on.</param>
147+
/// <returns>
148+
/// A string.
149+
/// </returns>
150+
/// =================================================================================================
151+
public static string AsReadableFileSize(this long size)
152+
{
153+
if (size < 1024)
154+
{
155+
return (size).ToString("F0") + " bytes";
156+
}
157+
158+
if ((size >> 10) < 1024)
159+
{
160+
return (size / (float)1024).ToString("F1") + " KB";
161+
}
162+
163+
if ((size >> 20) < 1024)
164+
{
165+
return ((size >> 10) / (float)1024).ToString("F1") + " MB";
166+
}
167+
168+
if ((size >> 30) < 1024)
169+
{
170+
return ((size >> 20) / (float)1024).ToString("F1") + " GB";
171+
}
172+
173+
if ((size >> 40) < 1024)
174+
{
175+
return ((size >> 30) / (float)1024).ToString("F1") + " TB";
176+
}
177+
178+
if ((size >> 50) < 1024)
179+
{
180+
return ((size >> 40) / (float)1024).ToString("F1") + " PB";
181+
}
182+
183+
return ((size >> 50) / (float)1024).ToString("F0") + " EB";
184+
}
141185
}
142186
}

src/DomainCommonExtensions/DomainCommonExtensions.csproj

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<!--<TargetFramework>netstandard2.0</TargetFramework>-->
55
<TargetFrameworks>net40;net45;netstandard2.0;netstandard2.1</TargetFrameworks>
66
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
7-
<Authors>RzR</Authors>
8-
<Copyright>RzR</Copyright>
7+
<Authors>RzR</Authors>
8+
<Copyright>RzR</Copyright>
99
<Owners>RzR</Owners>
1010
<PackageLicenseFile>LICENSE</PackageLicenseFile>
1111
<PackageIcon>PackageIcon.png</PackageIcon>
@@ -20,63 +20,63 @@
2020
<Summary>The purpose of this repository/library is to provide the most relevant and used extension methods in the life cycle of application development that allow us to improve our code, and writing speed, and use more efficiently dev team time during this period for more complex functionality.</Summary>
2121
<PackageTitle>RzR.Shared.DomainCommonExtensions (DomainCommonExtensions)</PackageTitle>
2222
<Title>RzR.Shared.DomainCommonExtensions (DomainCommonExtensions)</Title>
23-
<LangVersion>9.0</LangVersion>
24-
<Language>en-US</Language>
25-
<PackageIconUrl />
26-
<RepositoryType>GIT</RepositoryType>
23+
<LangVersion>9.0</LangVersion>
24+
<Language>en-US</Language>
25+
<PackageIconUrl />
26+
<RepositoryType>GIT</RepositoryType>
2727
</PropertyGroup>
28-
29-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
30-
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
31-
</PropertyGroup>
3228

33-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
34-
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
35-
</PropertyGroup>
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
30+
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
31+
</PropertyGroup>
32+
33+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
34+
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
35+
</PropertyGroup>
3636

3737
<ItemGroup>
3838
<Compile Include="..\shared\GeneralAssemblyInfo.cs" Link="Properties\GeneralAssemblyInfo.cs" />
3939
</ItemGroup>
4040

4141
<ItemGroup>
42-
<None Include="..\..\assets\PackageIcon.png">
43-
<Pack>True</Pack>
44-
<PackagePath></PackagePath>
45-
</None>
46-
<None Include="..\..\LICENSE">
47-
<Pack>True</Pack>
48-
<PackagePath></PackagePath>
49-
</None>
42+
<None Include="..\..\assets\PackageIcon.png">
43+
<Pack>True</Pack>
44+
<PackagePath></PackagePath>
45+
</None>
46+
<None Include="..\..\LICENSE">
47+
<Pack>True</Pack>
48+
<PackagePath></PackagePath>
49+
</None>
5050
</ItemGroup>
5151

5252
<ItemGroup>
53-
<PackageReference Include="CodeSource" Version="2.0.0" />
54-
<!--<PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.3" />-->
53+
<PackageReference Include="CodeSource" Version="4.0.0.2838" />
54+
<!--<PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.3" />-->
5555
</ItemGroup>
5656

57-
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'netstandard2.1'">
58-
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions">
59-
<Version>3.1.32</Version>
60-
</PackageReference>
61-
<PackageReference Include="System.Text.Encodings.Web">
62-
<Version>4.7.2</Version>
63-
</PackageReference>
64-
<PackageReference Include="System.Text.Json">
65-
<Version>4.7.2</Version>
66-
</PackageReference>
67-
</ItemGroup>
57+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'netstandard2.1'">
58+
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions">
59+
<Version>3.1.32</Version>
60+
</PackageReference>
61+
<PackageReference Include="System.Text.Encodings.Web">
62+
<Version>4.7.2</Version>
63+
</PackageReference>
64+
<PackageReference Include="System.Text.Json">
65+
<Version>4.7.2</Version>
66+
</PackageReference>
67+
</ItemGroup>
6868

69-
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
70-
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
71-
</ItemGroup>
69+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
70+
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
71+
</ItemGroup>
7272

73-
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
74-
</ItemGroup>
73+
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
74+
</ItemGroup>
7575

76-
<ItemGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^((?!net40).)*$'))">
77-
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
78-
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
79-
</ItemGroup>
76+
<ItemGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^((?!net40).)*$'))">
77+
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
78+
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
79+
</ItemGroup>
8080

8181
<ProjectExtensions>
8282
<VisualStudio>

src/shared/GeneralAssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@
4747
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]
4848
#endif
4949

50-
[assembly: AssemblyVersion("4.1.0.8241")]
51-
[assembly: AssemblyFileVersion("4.1.0.8241")]
52-
[assembly: AssemblyInformationalVersion("4.1.0.8241")]
50+
[assembly: AssemblyVersion("4.2.0.8401")]
51+
[assembly: AssemblyFileVersion("4.2.0.8401")]
52+
[assembly: AssemblyInformationalVersion("4.2.0.8401")]

src/tests/DataTypeTests/DataTests/IntTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,21 @@ public void SetFlagIntTest()
8787
Assert.IsTrue(isSetFlag2);
8888
Assert.AreEqual(8, flag2);
8989
}
90+
91+
[DataRow(0, "0 bytes")]
92+
[DataRow(1024, "1.0 KB")]
93+
[DataRow(1902021, "1.8 MB")]
94+
[DataRow(99999999, "95.4 MB")]
95+
[DataRow(1073741824, "1.0 GB")]
96+
[DataRow(1610612736, "1.5 GB")]
97+
[DataRow(1099511627776, "1.0 TB")]
98+
[TestMethod]
99+
public void AsReadableFileSize_Test(long size, string excepted)
100+
{
101+
var result = size.AsReadableFileSize();
102+
103+
Assert.IsNotNull(result);
104+
Assert.AreEqual(excepted, result);
105+
}
90106
}
91107
}

0 commit comments

Comments
 (0)