ElasticVersion.cs
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
// A simple version implementation based on
// https://github.com/maxhauser/semver/blob/master/src/Semver/SemVersion.cs // MIT License // Copyright (c) 2013 Max Hauser // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using System.Globalization; using System.Runtime.Serialization; using System.Security.Permissions; using System.Text.RegularExpressions; namespace Elastic { /// <summary> /// An Elastic product version /// </summary> public sealed class ElasticVersion : IEquatable<ElasticVersion>, IComparable<ElasticVersion>, IComparable { private static Regex VersionRegex = new Regex( @"^(?<major>\d+)(\.(?<minor>\d+))?(\.(?<patch>\d+))?(\-(?<pre>[0-9A-Za-z]+))?$", RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture); public ElasticVersion(object version) : this(version.ToString()) { } public ElasticVersion(string version) { var match = VersionRegex.Match(version); if (!match.Success) throw new ArgumentException(string.Format("Invalid version '{0}'", version), "version"); var major = int.Parse(match.Groups["major"].Value, CultureInfo.InvariantCulture); var minorMatch = match.Groups["minor"]; int minor = 0; if (minorMatch.Success) minor = int.Parse(minorMatch.Value, CultureInfo.InvariantCulture); var patchMatch = match.Groups["patch"]; int patch = 0; if (patchMatch.Success) patch = int.Parse(patchMatch.Value, CultureInfo.InvariantCulture); var prerelease = match.Groups["pre"].Value; this.Major = major; this.Minor = minor; this.Patch = patch; this.Prerelease = prerelease; } public ElasticVersion(int major, int minor, int patch, string prerelease) { this.Major = major; this.Minor = minor; this.Patch = patch; this.Prerelease = prerelease; } public static bool TryParse(string version, out ElasticVersion ElasticVersion) { try { ElasticVersion = new ElasticVersion(version); return true; } catch (Exception) { ElasticVersion = null; return false; } } public static bool Equals(ElasticVersion versionA, ElasticVersion versionB) { if (ReferenceEquals(versionA, null)) return ReferenceEquals(versionB, null); return versionA.Equals(versionB); } public static int Compare(ElasticVersion versionA, ElasticVersion versionB) { if (ReferenceEquals(versionA, null)) return ReferenceEquals(versionB, null) ? 0 : -1; return versionA.CompareTo(versionB); } public ElasticVersion Change(int? major = null, int? minor = null, int? patch = null, string prerelease = null) { return new ElasticVersion( major ?? this.Major, minor ?? this.Minor, patch ?? this.Patch, prerelease ?? this.Prerelease); } public int Major { get; private set; } public int Minor { get; private set; } public int Patch { get; private set; } public string Prerelease { get; private set; } public override string ToString() { var version = "" + Major + "." + Minor + "." + Patch; if (!String.IsNullOrEmpty(Prerelease)) version += "-" + Prerelease; return version; } public int CompareTo(object obj) { return CompareTo((ElasticVersion)obj); } public int CompareTo(ElasticVersion other) { if (ReferenceEquals(other, null)) return 1; var r = this.CompareByPrecedence(other); return r; } public bool PrecedenceMatches(ElasticVersion other) { return CompareByPrecedence(other) == 0; } public int CompareByPrecedence(ElasticVersion other) { if (ReferenceEquals(other, null)) return 1; var r = this.Major.CompareTo(other.Major); if (r != 0) return r; r = this.Minor.CompareTo(other.Minor); if (r != 0) return r; r = this.Patch.CompareTo(other.Patch); if (r != 0) return r; r = CompareComponent(this.Prerelease, other.Prerelease, true); return r; } static int CompareComponent(string a, string b, bool lower = false) { var aEmpty = String.IsNullOrEmpty(a); var bEmpty = String.IsNullOrEmpty(b); if (aEmpty && bEmpty) return 0; if (aEmpty) return lower ? 1 : -1; if (bEmpty) return lower ? -1 : 1; var aComps = a.Split('.'); var bComps = b.Split('.'); var minLen = Math.Min(aComps.Length, bComps.Length); for (int i = 0; i < minLen; i++) { var ac = aComps[i]; var bc = bComps[i]; int anum, bnum; var isanum = Int32.TryParse(ac, out anum); var isbnum = Int32.TryParse(bc, out bnum); int r; if (isanum && isbnum) { r = anum.CompareTo(bnum); if (r != 0) return anum.CompareTo(bnum); } else { if (isanum) return -1; if (isbnum) return 1; r = String.CompareOrdinal(ac, bc); if (r != 0) return r; } } return aComps.Length.CompareTo(bComps.Length); } public override bool Equals(object obj) { if (ReferenceEquals(obj, null)) return false; if (ReferenceEquals(this, obj)) return true; var other = (ElasticVersion)obj; return Equals(other); } public bool Equals(ElasticVersion other) { if (other == null) return false; return this.Major == other.Major && this.Minor == other.Minor && this.Patch == other.Patch && string.Equals(this.Prerelease, other.Prerelease, StringComparison.Ordinal); } public override int GetHashCode() { unchecked { int result = this.Major.GetHashCode(); result = result * 31 + this.Minor.GetHashCode(); result = result * 31 + this.Patch.GetHashCode(); result = result * 31 + this.Prerelease.GetHashCode(); return result; } } public static bool operator ==(ElasticVersion left, ElasticVersion right) { return ElasticVersion.Equals(left, right); } public static bool operator !=(ElasticVersion left, ElasticVersion right) { return !ElasticVersion.Equals(left, right); } public static bool operator >(ElasticVersion left, ElasticVersion right) { return ElasticVersion.Compare(left, right) > 0; } public static bool operator >=(ElasticVersion left, ElasticVersion right) { return left == right || left > right; } public static bool operator <(ElasticVersion left, ElasticVersion right) { return ElasticVersion.Compare(left, right) < 0; } public static bool operator <=(ElasticVersion left, ElasticVersion right) { return left == right || left < right; } } } |