|
| 1 | +namespace net.openstack.Core |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using net.openstack.Core.Domain.Converters; |
| 5 | + |
| 6 | + /// <summary> |
| 7 | + /// Represents a unique identifier within the context of a cloud services provider. |
| 8 | + /// </summary> |
| 9 | + /// <typeparam name="T">The resource identifier type.</typeparam> |
| 10 | + /// <threadsafety static="true" instance="false"/> |
| 11 | + /// <preliminary/> |
| 12 | + public abstract class ResourceIdentifier<T> : IEquatable<T> |
| 13 | + where T : ResourceIdentifier<T> |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// This is the backing field for the <see cref="Value"/> property. |
| 17 | + /// </summary> |
| 18 | + private readonly string _id; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Initialized a new instance of the <see cref="ResourceIdentifier{T}"/> class |
| 22 | + /// with the specified identifier. |
| 23 | + /// </summary> |
| 24 | + /// <param name="id">The resource identifier value.</param> |
| 25 | + /// <exception cref="ArgumentNullException">If <paramref name="id"/> is <c>null</c>.</exception> |
| 26 | + /// <exception cref="ArgumentException">If <paramref name="id"/> is empty.</exception> |
| 27 | + protected ResourceIdentifier(string id) |
| 28 | + { |
| 29 | + if (id == null) |
| 30 | + throw new ArgumentNullException("id"); |
| 31 | + if (string.IsNullOrEmpty(id)) |
| 32 | + throw new ArgumentException("id cannot be empty"); |
| 33 | + |
| 34 | + _id = id; |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Determines whether two specified resource identifiers have the same value. |
| 39 | + /// </summary> |
| 40 | + /// <param name="left">The first resource identifier to compare, or <c>null</c>.</param> |
| 41 | + /// <param name="right">The second resource identifier to compare, or <c>null</c>.</param> |
| 42 | + /// <returns><c>true</c> if the value of <paramref name="left"/> is the same as the value of <paramref name="right"/>; otherwise, <c>false</c>.</returns> |
| 43 | + public static bool operator ==(ResourceIdentifier<T> left, ResourceIdentifier<T> right) |
| 44 | + { |
| 45 | + if (object.ReferenceEquals(left, null)) |
| 46 | + return object.ReferenceEquals(right, null); |
| 47 | + else if (object.ReferenceEquals(right, null)) |
| 48 | + return false; |
| 49 | + |
| 50 | + return left.Equals(right); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Determines whether two specified resource identifiers have different values. |
| 55 | + /// </summary> |
| 56 | + /// <param name="left">The first resource identifier to compare, or <c>null</c>.</param> |
| 57 | + /// <param name="right">The second resource identifier to compare, or <c>null</c>.</param> |
| 58 | + /// <returns><c>true</c> if the value of <paramref name="left"/> is different from the value of <paramref name="right"/>; otherwise, <c>false</c>.</returns> |
| 59 | + public static bool operator !=(ResourceIdentifier<T> left, ResourceIdentifier<T> right) |
| 60 | + { |
| 61 | + return !(left == right); |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Gets the value of this resource identifier. |
| 66 | + /// </summary> |
| 67 | + public string Value |
| 68 | + { |
| 69 | + get |
| 70 | + { |
| 71 | + return _id; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// <inheritdoc/> |
| 76 | + /// <remarks> |
| 77 | + /// The default implementation uses <see cref="StringComparer.Ordinal"/> to compare |
| 78 | + /// the <see cref="Value"/> property of two identifiers. |
| 79 | + /// |
| 80 | + /// <note type="implement"> |
| 81 | + /// This method may be overridden to change the way unique identifiers are compared. |
| 82 | + /// </note> |
| 83 | + /// </remarks> |
| 84 | + public virtual bool Equals(T other) |
| 85 | + { |
| 86 | + if (object.ReferenceEquals(other, null)) |
| 87 | + return false; |
| 88 | + |
| 89 | + return StringComparer.Ordinal.Equals(_id, other._id); |
| 90 | + } |
| 91 | + |
| 92 | + /// <inheritdoc/> |
| 93 | + public override bool Equals(object obj) |
| 94 | + { |
| 95 | + return this.Equals(obj as T); |
| 96 | + } |
| 97 | + |
| 98 | + /// <inheritdoc/> |
| 99 | + /// <remarks> |
| 100 | + /// The default implementation uses <see cref="StringComparer.Ordinal"/> to calculate |
| 101 | + /// and return a hash code from the <see cref="Value"/> property. |
| 102 | + /// |
| 103 | + /// <note type="implement"> |
| 104 | + /// This method may be overridden to change the way unique identifiers are compared. |
| 105 | + /// </note> |
| 106 | + /// </remarks> |
| 107 | + public override int GetHashCode() |
| 108 | + { |
| 109 | + return StringComparer.Ordinal.GetHashCode(_id); |
| 110 | + } |
| 111 | + |
| 112 | + /// <inheritdoc/> |
| 113 | + public override string ToString() |
| 114 | + { |
| 115 | + return _id; |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Provides support for serializing and deserializing <see cref="ResourceIdentifier{T}"/> |
| 120 | + /// objects to JSON string values. |
| 121 | + /// </summary> |
| 122 | + /// <threadsafety static="true" instance="false"/> |
| 123 | + protected abstract class ConverterBase : SimpleStringJsonConverter<T> |
| 124 | + { |
| 125 | + /// <remarks> |
| 126 | + /// This method uses <see cref="Value"/> for serialization. |
| 127 | + /// </remarks> |
| 128 | + /// <inheritdoc/> |
| 129 | + protected override string ConvertToString(T obj) |
| 130 | + { |
| 131 | + return obj.Value; |
| 132 | + } |
| 133 | + |
| 134 | + /// <remarks> |
| 135 | + /// If <paramref name="str"/> is <c>null</c> or an empty string, this method returns <c>null</c>. |
| 136 | + /// Otherwise, this method uses <see cref="FromValue"/> for deserialization. |
| 137 | + /// </remarks> |
| 138 | + /// <inheritdoc/> |
| 139 | + protected override T ConvertToObject(string str) |
| 140 | + { |
| 141 | + if (string.IsNullOrEmpty(str)) |
| 142 | + return null; |
| 143 | + |
| 144 | + return FromValue(str); |
| 145 | + } |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// Creates a resource identifier with the given value. |
| 149 | + /// </summary> |
| 150 | + /// <param name="id">The resource identifier value. This value is never <c>null</c> or empty.</param> |
| 151 | + /// <returns>An instance of <typeparamref name="T"/> corresponding representing the specified <paramref name="id"/>.</returns> |
| 152 | + protected abstract T FromValue(string id); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments