-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathIdentifier.cs
More file actions
133 lines (116 loc) · 3.75 KB
/
Copy pathIdentifier.cs
File metadata and controls
133 lines (116 loc) · 3.75 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
using System;
using Newtonsoft.Json;
using OpenStack.Serialization;
namespace OpenStack
{
/// <summary>
/// Represents a uniqe identifier.
/// <para>
/// This is a Guid which has implicit and explicit conversions to/from Guid and String.
/// </para>
/// </summary>
[JsonConverter(typeof(IdentifierConverter))]
public class Identifier
{
private readonly string _id;
/// <summary>
/// Initializes a new instance of the <see cref="Identifier"/> class.
/// </summary>
/// <param name="id">The identifier.</param>
public Identifier(string id)
{
_id = id;
}
/// <summary>
/// Initializes a new instance of the <see cref="Identifier"/> class.
/// </summary>
/// <param name="id">The identifier.</param>
public Identifier(Guid id)
{
_id = id.ToString("D");
}
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public override string ToString()
{
return _id;
}
#region Conversions
/// <summary>
/// Performs an implicit conversion from <see cref="Guid"/> to <see cref="Identifier"/>.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator Identifier(Guid id)
{
return new Identifier(id);
}
/// <summary>
/// Performs an implicit conversion from <see cref="Identifier"/> to <see cref="System.String"/>.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator string(Identifier id)
{
return id?.ToString();
}
/// <summary>
/// Performs an implicit conversion from <see cref="System.String"/> to <see cref="Identifier"/>.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator Identifier(string id)
{
return new Identifier(id);
}
#endregion
#region Equality
private bool Equals(Identifier other)
{
return _id.Equals(other._id);
}
/// <inheritdoc />
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
var stringOther = obj as string;
if (stringOther != null)
{
Guid idGuid;
return Guid.TryParse(stringOther, out idGuid) && Equals(new Identifier(idGuid));
}
var guidOther = obj as Guid?;
if (guidOther != null)
return Equals(new Identifier(guidOther.Value));
var otherId = obj as Identifier;
return otherId != null && Equals(otherId);
}
/// <inheritdoc />
public override int GetHashCode()
{
return _id.GetHashCode();
}
/// <inheritdoc />
public static bool operator ==(Identifier left, Identifier right)
{
return Equals(left, right);
}
/// <inheritdoc />
public static bool operator !=(Identifier left, Identifier right)
{
return !Equals(left, right);
}
#endregion
}
}