using System;
using System.Xml.Linq;
using System.Globalization;
namespace DataFileAnalyzer
{
///
/// The key of a data file.
/// This is only used on a local computer. Never shared between users.
///
public struct DataFileKey : IComparable, IComparable
{
///
/// The invalid key value
///
public static readonly UInt64 InvalidKeyValue = 0xFFFFFFFFFFFFFFFF;
///
/// The invalid key
///
public static readonly DataFileKey Invalid = new DataFileKey(InvalidKeyValue);
///
/// The value of the key
///
private UInt64 m_keyValue;
///
/// The value of the key
///
public UInt64 KeyValue { get { return m_keyValue; } set { m_keyValue = value; } }
///
/// Constructor
///
public DataFileKey(UInt64 _value) { m_keyValue = _value; }
///
/// Comparison
///
public override bool Equals(object _other)
{
return
_other.GetType() == typeof(DataFileKey) &&
m_keyValue == ((DataFileKey)_other).m_keyValue;
}
///
/// Comparison
///
public static bool operator ==(DataFileKey k1, DataFileKey k2)
{
return k1.Equals(k2);
}
///
/// Comparison
///
public static bool operator !=(DataFileKey k1, DataFileKey k2)
{
return !k1.Equals(k2);
}
///
/// Hash code
///
public override int GetHashCode()
{
return KeyValue.GetHashCode();
}
///
/// ToString
///
public override string ToString()
{
return KeyValue.ToString("X16");
}
///
/// Is it an invalid key?
///
public bool IsInvalid()
{
return m_keyValue == InvalidKeyValue;
}
///
/// Set this key as invalid
///
public void SetInvalid()
{
m_keyValue = InvalidKeyValue;
}
///
/// Save to an XML element
///
public static bool Save(DataFileKey _key, XElement _element)
{
_element.SetAttributeValue("Key", _key.ToString());
return true;
}
///
/// Load from an XML element
///
public static DataFileKey Load(XElement _element)
{
UInt64 keyValue;
if (UInt64.TryParse(_element.Attribute("Key").Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out keyValue))
return new DataFileKey(keyValue);
return DataFileKey.Invalid;
}
public int CompareTo(object obj)
{
if (!(obj is DataFileKey))
return 1;
return CompareTo((DataFileKey)obj);
}
public int CompareTo(DataFileKey other)
{
return ToString().CompareTo(other.ToString());
}
}
}