| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | |
|
| | 6 | | namespace Azure |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Represents an HTTP ETag. |
| | 10 | | /// </summary> |
| | 11 | | public readonly struct ETag : IEquatable<ETag> |
| | 12 | | { |
| | 13 | | private const char QuoteCharacter = '"'; |
| | 14 | | private const string QuoteString = "\""; |
| | 15 | |
|
| | 16 | | private readonly string _value; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Creates a new instance of <see cref="ETag"/>. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="etag">The string value of the ETag.</param> |
| 46 | 22 | | public ETag(string etag) => _value = etag; |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Compares equality of two <see cref="ETag"/> instances. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="left">The <see cref="ETag"/> to compare.</param> |
| | 28 | | /// <param name="right">The <see cref="ETag"/> to compare to.</param> |
| | 29 | | /// <returns><c>true</c> if values of both ETags are equal, otherwise <c>false</c>.</returns> |
| 8 | 30 | | public static bool operator ==(ETag left, ETag right) => left.Equals(right); |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Compares inequality of two <see cref="ETag"/> instances. |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="left">The <see cref="ETag"/> to compare.</param> |
| | 36 | | /// <param name="right">The <see cref="ETag"/> to compare to.</param> |
| | 37 | | /// <returns><c>true</c> if values of both ETags are not equal, otherwise <c>false</c>.</returns> |
| 4 | 38 | | public static bool operator !=(ETag left, ETag right) => !left.Equals(right); |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Instance of <see cref="ETag"/> with the value. <code>*</code> |
| | 42 | | /// </summary> |
| 2 | 43 | | public static readonly ETag All = new ETag("*"); |
| | 44 | |
|
| | 45 | | /// <inheritdoc /> |
| | 46 | | public bool Equals(ETag other) |
| | 47 | | { |
| 26 | 48 | | return string.Equals(_value, other._value, StringComparison.Ordinal); |
| | 49 | | } |
| | 50 | | /// <summary> |
| | 51 | | /// Indicates whether the value of current <see cref="ETag"/> is equal to the provided string.</summary> |
| | 52 | | /// <param name="other">An object to compare with this object.</param> |
| | 53 | | /// <returns>true if the current object is equal to the <paramref name="other">other</paramref> parameter; other |
| | 54 | | public bool Equals(string? other) |
| | 55 | | { |
| 0 | 56 | | return string.Equals(_value, other, StringComparison.Ordinal); |
| | 57 | | } |
| | 58 | |
|
| | 59 | | /// <inheritdoc /> |
| | 60 | | public override bool Equals(object? obj) |
| | 61 | | { |
| 6 | 62 | | return (obj is ETag other) && Equals(other); |
| | 63 | | } |
| | 64 | |
|
| | 65 | | /// <inheritdoc /> |
| | 66 | | public override int GetHashCode() |
| | 67 | | { |
| 12 | 68 | | return _value?.GetHashCode() ?? 0; |
| | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// |
| | 73 | | /// </summary> |
| | 74 | | /// <returns>The string representation of this <see cref="ETag"/>.</returns> |
| | 75 | | public override string ToString() |
| | 76 | | { |
| 8 | 77 | | return _value ?? "<null>"; |
| | 78 | | } |
| | 79 | |
|
| | 80 | | internal static ETag Parse(string value) |
| | 81 | | { |
| 12 | 82 | | if (value == All._value) |
| | 83 | | { |
| 2 | 84 | | return All; |
| | 85 | | } |
| 10 | 86 | | else if (value.StartsWith("W/", StringComparison.Ordinal)) |
| | 87 | | { |
| 2 | 88 | | throw new NotSupportedException("Weak ETags are not supported."); |
| | 89 | | } |
| 8 | 90 | | else if (!value.StartsWith(QuoteString, StringComparison.Ordinal) || |
| 8 | 91 | | !value.EndsWith(QuoteString, StringComparison.Ordinal)) |
| | 92 | | { |
| 2 | 93 | | throw new ArgumentException("The value should be equal to * or be wrapped in quotes", nameof(value)); |
| | 94 | | } |
| | 95 | |
|
| 6 | 96 | | return new ETag(value.Trim(QuoteCharacter)); |
| | 97 | | } |
| | 98 | | } |
| | 99 | | } |