mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
feat: Created extension class, comparer class and VersionSpan class to for Devices' version and model validation.
This commit is contained in:
committed by
Chris Cameron
parent
e5fc154858
commit
0574314580
26
ICD.Common.Utils/Comparers/UndefinedVersionComparer.cs
Normal file
26
ICD.Common.Utils/Comparers/UndefinedVersionComparer.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using ICD.Common.Utils.Extensions;
|
||||||
|
|
||||||
|
namespace ICD.Common.Utils.Comparers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Undefined Versions have a value of 0.0.-1.-1
|
||||||
|
/// This comparer Maxs Versions to 0.0.0.0
|
||||||
|
/// </summary>
|
||||||
|
public sealed class UndefinedVersionComparer : IComparer<Version>
|
||||||
|
{
|
||||||
|
private static UndefinedVersionComparer s_Instance;
|
||||||
|
|
||||||
|
public static UndefinedVersionComparer Instance
|
||||||
|
{
|
||||||
|
get { return s_Instance = s_Instance ?? new UndefinedVersionComparer(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Compare(Version x, Version y)
|
||||||
|
{
|
||||||
|
return x.ClearUndefined()
|
||||||
|
.CompareTo(y.ClearUndefined());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using ICD.Common.Utils.Extensions;
|
||||||
|
|
||||||
|
namespace ICD.Common.Utils.Comparers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Undefined Versions have a value of 0.0.-1.-1
|
||||||
|
/// This comparer Maxs Versions to 0.0.0.0
|
||||||
|
/// </summary>
|
||||||
|
public sealed class UndefinedVersionEqualityComparer : IEqualityComparer<Version>
|
||||||
|
{
|
||||||
|
private static UndefinedVersionEqualityComparer s_Instance;
|
||||||
|
|
||||||
|
public static UndefinedVersionEqualityComparer Instance
|
||||||
|
{
|
||||||
|
get { return s_Instance = s_Instance ?? new UndefinedVersionEqualityComparer(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Equals(Version x, Version y)
|
||||||
|
{
|
||||||
|
return x.ClearUndefined()
|
||||||
|
.Equals(y.ClearUndefined());
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetHashCode(Version version)
|
||||||
|
{
|
||||||
|
return version.ClearUndefined()
|
||||||
|
.GetHashCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
37
ICD.Common.Utils/Extensions/VersionExtensions.cs
Normal file
37
ICD.Common.Utils/Extensions/VersionExtensions.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace ICD.Common.Utils.Extensions
|
||||||
|
{
|
||||||
|
public static class VersionExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new Version using 0 instead of -1 for omitted quadrants.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static Version ClearUndefined(this Version extends)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
return new Version(Math.Max(0, extends.Major),
|
||||||
|
Math.Max(0, extends.Minor),
|
||||||
|
Math.Max(0, extends.Build),
|
||||||
|
Math.Max(0, extends.Revision));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Formats the version to X.XXX.XXXX
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string ToCrestronString(this Version extends)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
return string.Format("{0}.{1:D3}.{2:D4}", extends.Major, extends.Minor, extends.Build);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -86,6 +86,8 @@
|
|||||||
<Compile Include="Comparers\FileNameEqualityComparer.cs" />
|
<Compile Include="Comparers\FileNameEqualityComparer.cs" />
|
||||||
<Compile Include="Comparers\PredicateComparer.cs" />
|
<Compile Include="Comparers\PredicateComparer.cs" />
|
||||||
<Compile Include="Comparers\SequenceComparer.cs" />
|
<Compile Include="Comparers\SequenceComparer.cs" />
|
||||||
|
<Compile Include="Comparers\UndefinedVersionComparer.cs" />
|
||||||
|
<Compile Include="Comparers\UndefinedVersionEqualityComparer.cs" />
|
||||||
<Compile Include="eConsoleColor.cs" />
|
<Compile Include="eConsoleColor.cs" />
|
||||||
<Compile Include="DateTimeUtils.cs" />
|
<Compile Include="DateTimeUtils.cs" />
|
||||||
<Compile Include="Email\EmailClient.cs" />
|
<Compile Include="Email\EmailClient.cs" />
|
||||||
@@ -110,6 +112,7 @@
|
|||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
<Compile Include="Extensions\CollectionExtensions.cs" />
|
<Compile Include="Extensions\CollectionExtensions.cs" />
|
||||||
|
<Compile Include="Extensions\VersionExtensions.cs" />
|
||||||
<Compile Include="TimeZoneInfo\IcdTimeZoneInfo.cs" />
|
<Compile Include="TimeZoneInfo\IcdTimeZoneInfo.cs" />
|
||||||
<Compile Include="ObfuscationSettings.cs" />
|
<Compile Include="ObfuscationSettings.cs" />
|
||||||
<Compile Include="Extensions\BoolExtensions.cs" />
|
<Compile Include="Extensions\BoolExtensions.cs" />
|
||||||
@@ -228,6 +231,7 @@
|
|||||||
<Compile Include="TryUtils.cs" />
|
<Compile Include="TryUtils.cs" />
|
||||||
<Compile Include="UriQueryBuilder.cs" />
|
<Compile Include="UriQueryBuilder.cs" />
|
||||||
<Compile Include="UriUtils.cs" />
|
<Compile Include="UriUtils.cs" />
|
||||||
|
<Compile Include="VersionSpan.cs" />
|
||||||
<Compile Include="Xml\AbstractGenericXmlConverter.cs" />
|
<Compile Include="Xml\AbstractGenericXmlConverter.cs" />
|
||||||
<Compile Include="Xml\AbstractXmlConverter.cs" />
|
<Compile Include="Xml\AbstractXmlConverter.cs" />
|
||||||
<Compile Include="Xml\DefaultXmlConverter.cs" />
|
<Compile Include="Xml\DefaultXmlConverter.cs" />
|
||||||
|
|||||||
29
ICD.Common.Utils/VersionSpan.cs
Normal file
29
ICD.Common.Utils/VersionSpan.cs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
using System;
|
||||||
|
using ICD.Common.Properties;
|
||||||
|
using ICD.Common.Utils.Extensions;
|
||||||
|
|
||||||
|
namespace ICD.Common.Utils
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Describes the difference between two versions
|
||||||
|
/// </summary>
|
||||||
|
public sealed class VersionSpan
|
||||||
|
{
|
||||||
|
public Version Start { get; set; }
|
||||||
|
public Version End { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if the given version is included in the span, inclusively.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="version"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public bool Contains([NotNull] Version version)
|
||||||
|
{
|
||||||
|
if (version == null)
|
||||||
|
throw new ArgumentNullException("version");
|
||||||
|
|
||||||
|
return version.ClearUndefined() >= Start.ClearUndefined() &&
|
||||||
|
version.ClearUndefined() <= End.ClearUndefined();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user