using System;
using System.Text;
namespace NEbml.Core
{
///
/// Defines the EBML element description.
///
public class ElementDescriptor
{
///
/// Initializes a new instance of the ElementDescriptor class.
///
///
///
///
public ElementDescriptor(ulong identifier, string name, ElementType type)
: this(VInt.FromEncoded(identifier), name, type)
{
}
///
/// Initializes a new instance of the ElementDescriptor class.
///
///
///
///
public ElementDescriptor(long identifier, string name, ElementType type)
: this(VInt.FromEncoded((ulong)identifier), name, type)
{
}
///
/// Initializes a new instance of the ElementDescriptor class.
///
/// the element identifier
/// the element name or null if the name is not known
/// the element type or null if the type is not known
/// if identifier is null
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;
}
///
/// Returns the element identifier.
///
/// the element identifier in the encoded form
public VInt Identifier { get; }
///
/// Returns the element name.
///
/// the element name or <code>null</code> if the name is not known
public string Name { get; }
///
/// Returns the element type.
///
/// the element type or <code>null</code> if the type is not known
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();
}
///
/// Returns a new descriptor with updated name.
///
///
///
public ElementDescriptor Named(string name)
{
return new ElementDescriptor(Identifier, name, Type);
}
}
}