Code
·
119 lines
·
3392 bytes
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
119using System;
using System.Text;
namespace NEbml.Core
{
/// <summary>
/// Defines the EBML element description.
/// </summary>
public class ElementDescriptor
{
/// <summary>
/// Initializes a new instance of the <code>ElementDescriptor</code> class.
/// </summary>
/// <param name="identifier"></param>
/// <param name="name"></param>
/// <param name="type"></param>
public ElementDescriptor(ulong identifier, string name, ElementType type)
: this(VInt.FromEncoded(identifier), name, type)
{
}
/// <summary>
/// Initializes a new instance of the <code>ElementDescriptor</code> class.
/// </summary>
/// <param name="identifier"></param>
/// <param name="name"></param>
/// <param name="type"></param>
public ElementDescriptor(long identifier, string name, ElementType type)
: this(VInt.FromEncoded((ulong)identifier), name, type)
{
}
/// <summary>
/// Initializes a new instance of the <code>ElementDescriptor</code> class.
/// </summary>
/// <param name="identifier">the element identifier</param>
/// <param name="name">the element name or <code>null</code> if the name is not known</param>
/// <param name="type">the element type or <code>null</code> if the type is not known</param>
/// <exception cref="ArgumentNullException">if <code>identifier</code> is <code>null</code></exception>
private ElementDescriptor(VInt identifier, string name, ElementType type)
{
if (!identifier.IsValidIdentifier)
throw new ArgumentException("Value is not valid identifier", nameof(identifier));
Identifier = identifier;
Name = name;
Type = type;
}
/// <summary>
/// Returns the element identifier.
/// </summary>
/// <value>the element identifier in the encoded form</value>
public VInt Identifier { get; }
/// <summary>
/// Returns the element name.
/// </summary>
/// <value>the element name or <code>null</code> if the name is not known</value>
public string Name { get; }
/// <summary>
/// Returns the element type.
/// </summary>
/// <value>the element type or <code>null</code> if the type is not known</value>
public ElementType Type { get; }
public override int GetHashCode()
{
int result = 17;
result = 37*result + Identifier.GetHashCode();
result = 37*result + (Name == null ? 0 : Name.GetHashCode());
result = 37*result + (Type == ElementType.None ? 0 : Type.GetHashCode());
return result;
}
public override bool Equals(object? obj)
{
if (this == obj)
{
return true;
}
if (obj is ElementDescriptor o2)
{
return Equals(Identifier, o2.Identifier)
&& Equals(Name, o2.Name)
&& Equals(Type, o2.Type);
}
return false;
}
public override string ToString()
{
var buffer = new StringBuilder();
buffer.Append("ElementDescriptor(");
buffer.Append("identifier=").Append(Identifier);
if (Name != null)
{
buffer.Append(',').Append("name=").Append(Name);
}
if (Type != ElementType.None)
{
buffer.Append(',').Append("type=").Append(Type);
}
buffer.Append(')');
return buffer.ToString();
}
/// <summary>
/// Returns a new descriptor with updated name.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public ElementDescriptor Named(string name)
{
return new ElementDescriptor(Identifier, name, Type);
}
}
}