|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +namespace Microsoft.DurableTask; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// The version of a durable task. |
| 8 | +/// </summary> |
| 9 | +public readonly struct TaskVersion : IEquatable<TaskVersion> |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Initializes a new instance of the <see cref="TaskVersion"/> struct. |
| 13 | + /// </summary> |
| 14 | + /// <param name="version">The version of the task. Providing <c>null</c> will result in the default struct.</param> |
| 15 | + public TaskVersion(string version) |
| 16 | + { |
| 17 | + if (version == null) |
| 18 | + { |
| 19 | + this.Version = null!; |
| 20 | + } |
| 21 | + else |
| 22 | + { |
| 23 | + this.Version = version; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Gets the version of a task. |
| 29 | + /// </summary> |
| 30 | + public string Version { get; } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Implicitly converts a <see cref="TaskVersion"/> into a <see cref="string"/> of the <see cref="Version"/> property value. |
| 34 | + /// </summary> |
| 35 | + /// <param name="value">The <see cref="TaskVersion"/> to be converted into a <see cref="string"/>.</param> |
| 36 | + public static implicit operator string(TaskVersion value) => value.Version; |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Implicitly converts a <see cref="string"/> into a <see cref="TaskVersion"/>. |
| 40 | + /// </summary> |
| 41 | + /// <param name="value">The <see cref="string"/> to convert into a <see cref="TaskVersion"/>.</param> |
| 42 | + public static implicit operator TaskVersion(string value) => new TaskVersion(value); |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Compares two <see cref="TaskVersion"/> structs for equality. |
| 46 | + /// </summary> |
| 47 | + /// <param name="a">The first <see cref="TaskVersion"/> to compare.</param> |
| 48 | + /// <param name="b">The second <see cref="TaskVersion"/> to compare.</param> |
| 49 | + /// <returns><c>true</c> if the two <see cref="TaskVersion"/> objects are equal; otherwise <c>false</c>.</returns> |
| 50 | + public static bool operator ==(TaskVersion a, TaskVersion b) |
| 51 | + { |
| 52 | + return a.Equals(b); |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Compares two <see cref="TaskVersion"/> structs for inequality. |
| 57 | + /// </summary> |
| 58 | + /// <param name="a">The first <see cref="TaskVersion"/> to compare.</param> |
| 59 | + /// <param name="b">The second <see cref="TaskVersion"/> to compare.</param> |
| 60 | + /// <returns><c>false</c> if the two <see cref="TaskVersion"/> objects are equal; otherwise <c>true</c>.</returns> |
| 61 | + public static bool operator !=(TaskVersion a, TaskVersion b) |
| 62 | + { |
| 63 | + return !a.Equals(b); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Gets a value indicating whether to <see cref="TaskVersion"/> objects |
| 68 | + /// are equal using value semantics. |
| 69 | + /// </summary> |
| 70 | + /// <param name="other">The other <see cref="TaskVersion"/> to compare to.</param> |
| 71 | + /// <returns><c>true</c> if the two <see cref="TaskVersion"/> are equal using value semantics; otherwise <c>false</c>.</returns> |
| 72 | + public bool Equals(TaskVersion other) |
| 73 | + { |
| 74 | + return string.Equals(this.Version, other.Version, StringComparison.OrdinalIgnoreCase); |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Gets a value indicating whether to <see cref="TaskVersion"/> objects |
| 79 | + /// are equal using value semantics. |
| 80 | + /// </summary> |
| 81 | + /// <param name="obj">The other object to compare to.</param> |
| 82 | + /// <returns><c>true</c> if the two objects are equal using value semantics; otherwise <c>false</c>.</returns> |
| 83 | + public override bool Equals(object? obj) |
| 84 | + { |
| 85 | + if (obj is not TaskVersion other) |
| 86 | + { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + return this.Equals(other); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Calculates a hash code value for the current <see cref="TaskVersion"/> instance. |
| 95 | + /// </summary> |
| 96 | + /// <returns>A 32-bit hash code value.</returns> |
| 97 | + public override int GetHashCode() |
| 98 | + { |
| 99 | + return StringComparer.OrdinalIgnoreCase.GetHashCode(this.Version); |
| 100 | + } |
| 101 | +} |
0 commit comments