-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathResourceIdentifier`1.cs
More file actions
155 lines (139 loc) · 6.01 KB
/
Copy pathResourceIdentifier`1.cs
File metadata and controls
155 lines (139 loc) · 6.01 KB
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
namespace net.openstack.Core
{
using System;
using net.openstack.Core.Domain.Converters;
/// <summary>
/// Represents a unique identifier within the context of a cloud services provider.
/// </summary>
/// <typeparam name="T">The resource identifier type.</typeparam>
/// <threadsafety static="true" instance="false"/>
/// <preliminary/>
public abstract class ResourceIdentifier<T> : IEquatable<T>
where T : ResourceIdentifier<T>
{
/// <summary>
/// This is the backing field for the <see cref="Value"/> property.
/// </summary>
private readonly string _id;
/// <summary>
/// Initializes a new instance of the <see cref="ResourceIdentifier{T}"/> class
/// with the specified identifier.
/// </summary>
/// <param name="id">The resource identifier value.</param>
/// <exception cref="ArgumentNullException">If <paramref name="id"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If <paramref name="id"/> is empty.</exception>
protected ResourceIdentifier(string id)
{
if (id == null)
throw new ArgumentNullException("id");
if (string.IsNullOrEmpty(id))
throw new ArgumentException("id cannot be empty");
_id = id;
}
/// <summary>
/// Determines whether two specified resource identifiers have the same value.
/// </summary>
/// <param name="left">The first resource identifier to compare, or <see langword="null"/>.</param>
/// <param name="right">The second resource identifier to compare, or <see langword="null"/>.</param>
/// <returns><see langword="true"/> if the value of <paramref name="left"/> is the same as the value of <paramref name="right"/>; otherwise, <see langword="false"/>.</returns>
public static bool operator ==(ResourceIdentifier<T> left, ResourceIdentifier<T> right)
{
if (object.ReferenceEquals(left, null))
return object.ReferenceEquals(right, null);
else if (object.ReferenceEquals(right, null))
return false;
return left.Equals(right);
}
/// <summary>
/// Determines whether two specified resource identifiers have different values.
/// </summary>
/// <param name="left">The first resource identifier to compare, or <see langword="null"/>.</param>
/// <param name="right">The second resource identifier to compare, or <see langword="null"/>.</param>
/// <returns><see langword="true"/> if the value of <paramref name="left"/> is different from the value of <paramref name="right"/>; otherwise, <see langword="false"/>.</returns>
public static bool operator !=(ResourceIdentifier<T> left, ResourceIdentifier<T> right)
{
return !(left == right);
}
/// <summary>
/// Gets the value of this resource identifier.
/// </summary>
public string Value
{
get
{
return _id;
}
}
/// <inheritdoc/>
/// <remarks>
/// The default implementation uses <see cref="StringComparer.Ordinal"/> to compare
/// the <see cref="Value"/> property of two identifiers.
///
/// <note type="implement">
/// This method may be overridden to change the way unique identifiers are compared.
/// </note>
/// </remarks>
public virtual bool Equals(T other)
{
if (object.ReferenceEquals(other, null))
return false;
return StringComparer.Ordinal.Equals(_id, other._id);
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
return this.Equals(obj as T);
}
/// <inheritdoc/>
/// <remarks>
/// The default implementation uses <see cref="StringComparer.Ordinal"/> to calculate
/// and return a hash code from the <see cref="Value"/> property.
///
/// <note type="implement">
/// This method may be overridden to change the way unique identifiers are compared.
/// </note>
/// </remarks>
public override int GetHashCode()
{
return StringComparer.Ordinal.GetHashCode(_id);
}
/// <inheritdoc/>
public override string ToString()
{
return _id;
}
/// <summary>
/// Provides support for serializing and deserializing <see cref="ResourceIdentifier{T}"/>
/// objects to JSON string values.
/// </summary>
/// <threadsafety static="true" instance="false"/>
protected abstract class ConverterBase : SimpleStringJsonConverter<T>
{
/// <remarks>
/// This method uses <see cref="Value"/> for serialization.
/// </remarks>
/// <inheritdoc/>
protected override string ConvertToString(T obj)
{
return obj.Value;
}
/// <remarks>
/// If <paramref name="str"/> is <see langword="null"/> or an empty string, this method returns <see langword="null"/>.
/// Otherwise, this method uses <see cref="FromValue"/> for deserialization.
/// </remarks>
/// <inheritdoc/>
protected override T ConvertToObject(string str)
{
if (string.IsNullOrEmpty(str))
return null;
return FromValue(str);
}
/// <summary>
/// Creates a resource identifier with the given value.
/// </summary>
/// <param name="id">The resource identifier value. This value is never <see langword="null"/> or empty.</param>
/// <returns>An instance of <typeparamref name="T"/> corresponding representing the specified <paramref name="id"/>.</returns>
protected abstract T FromValue(string id);
}
}
}