diff --git a/ICD.Common.Utils/Comparers/UndefinedVersionComparer.cs b/ICD.Common.Utils/Comparers/UndefinedVersionComparer.cs new file mode 100644 index 0000000..1476d51 --- /dev/null +++ b/ICD.Common.Utils/Comparers/UndefinedVersionComparer.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using ICD.Common.Utils.Extensions; + +namespace ICD.Common.Utils.Comparers +{ + /// + /// Undefined Versions have a value of 0.0.-1.-1 + /// This comparer Maxs Versions to 0.0.0.0 + /// + public sealed class UndefinedVersionComparer : IComparer + { + 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()); + } + } +} diff --git a/ICD.Common.Utils/Comparers/UndefinedVersionEqualityComparer.cs b/ICD.Common.Utils/Comparers/UndefinedVersionEqualityComparer.cs new file mode 100644 index 0000000..4841f73 --- /dev/null +++ b/ICD.Common.Utils/Comparers/UndefinedVersionEqualityComparer.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using ICD.Common.Utils.Extensions; + +namespace ICD.Common.Utils.Comparers +{ + /// + /// Undefined Versions have a value of 0.0.-1.-1 + /// This comparer Maxs Versions to 0.0.0.0 + /// + public sealed class UndefinedVersionEqualityComparer : IEqualityComparer + { + 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(); + } + } +} diff --git a/ICD.Common.Utils/Extensions/VersionExtensions.cs b/ICD.Common.Utils/Extensions/VersionExtensions.cs new file mode 100644 index 0000000..fc59a38 --- /dev/null +++ b/ICD.Common.Utils/Extensions/VersionExtensions.cs @@ -0,0 +1,37 @@ +using System; +using System.Text; + +namespace ICD.Common.Utils.Extensions +{ + public static class VersionExtensions + { + /// + /// Creates a new Version using 0 instead of -1 for omitted quadrants. + /// + /// + /// + 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)); + } + + /// + /// Formats the version to X.XXX.XXXX + /// + /// + /// + 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); + } + } +} diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj index ad6616e..d36e00c 100644 --- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj +++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj @@ -86,6 +86,8 @@ + + @@ -110,6 +112,7 @@ PreserveNewest + @@ -228,6 +231,7 @@ + diff --git a/ICD.Common.Utils/VersionSpan.cs b/ICD.Common.Utils/VersionSpan.cs new file mode 100644 index 0000000..2221c0c --- /dev/null +++ b/ICD.Common.Utils/VersionSpan.cs @@ -0,0 +1,29 @@ +using System; +using ICD.Common.Properties; +using ICD.Common.Utils.Extensions; + +namespace ICD.Common.Utils +{ + /// + /// Describes the difference between two versions + /// + public sealed class VersionSpan + { + public Version Start { get; set; } + public Version End { get; set; } + + /// + /// Returns true if the given version is included in the span, inclusively. + /// + /// + /// + public bool Contains([NotNull] Version version) + { + if (version == null) + throw new ArgumentNullException("version"); + + return version.ClearUndefined() >= Start.ClearUndefined() && + version.ClearUndefined() <= End.ClearUndefined(); + } + } +}