// TimeTicks data type.
// Copyright (C) 2008-2010 Malcolm Crowe, Lex Li, and other contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.IO;
namespace GSF.Net.Snmp
{
///
/// TimeTicks type.
///
/// Represents SNMP TimeTicks type.
[DebuggerDisplay("{count} ({ToTimeSpan()})")]
public sealed class TimeTicks : ISnmpData, IEquatable
{
private readonly Counter32 _count;
///
/// Creates a instance with a specific count.
///
/// Count
[CLSCompliant(false)]
public TimeTicks(uint count)
{
_count = new Counter32(count);
}
///
/// Creates a instance with .
///
/// The time span.
public TimeTicks(TimeSpan span) : this((uint)(span.TotalMilliseconds / 10))
{
}
///
/// Creates a instance with raw bytes.
///
/// Raw bytes
internal TimeTicks(byte[] raw) : this(new Tuple(raw.Length, raw.Length.WritePayloadLength()), new MemoryStream(raw))
{
// IMPORTANT: for test project only.
}
///
/// Initializes a new instance of the class.
///
/// The length.
/// The stream.
public TimeTicks(Tuple length, Stream stream)
{
if (length == null)
{
throw new ArgumentNullException(nameof(length));
}
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
_count = new Counter32(length, stream);
}
///
/// Returns an that represents the current
///
///
[CLSCompliant(false)]
public uint ToUInt32()
{
return _count.ToUInt32();
}
///
/// Returns representation.
///
///
public TimeSpan ToTimeSpan()
{
long root = ToUInt32();
root *= 100000;
return new TimeSpan(root);
}
///
/// Type code.
///
public SnmpType TypeCode
{
get
{
return SnmpType.TimeTicks;
}
}
///
/// Determines whether the specified is equal to the current .
///
/// The to compare with the current .
/// true if the specified is equal to the current ; otherwise, false.
///
public override bool Equals(object obj)
{
return Equals(this, obj as TimeTicks);
}
///
/// Serves as a hash function for a particular type.
///
/// A hash code for the current .
public override int GetHashCode()
{
return _count.GetHashCode();
}
#region ISnmpData Members
///
/// Appends the bytes to .
///
/// The stream.
public void AppendBytesTo(Stream stream)
{
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
stream.AppendBytes(TypeCode, _count.GetLengthBytes(), _count.GetRaw());
}
#endregion
///
/// Indicates whether the current object is equal to another object of the same type.
///
/// An object to compare with this object.
/// true if the current object is equal to the parameter; otherwise, false.
///
public bool Equals(TimeTicks other)
{
return Equals(this, other);
}
///
/// The equality operator.
///
/// Left object
/// Right object
///
/// Returns true if the values of its operands are equal, false otherwise.
public static bool operator ==(TimeTicks left, TimeTicks right)
{
return Equals(left, right);
}
///
/// The inequality operator.
///
/// Left object
/// Right object
///
/// Returns true if the values of its operands are not equal, false otherwise.
public static bool operator !=(TimeTicks left, TimeTicks right)
{
return !(left == right);
}
///
/// Returns a that represents this .
///
///
public override string ToString()
{
return ToTimeSpan().ToString();
}
///
/// The comparison.
///
/// Left object
/// Right object
///
/// Returns true if the values of its operands are not equal, false otherwise.
private static bool Equals(TimeTicks left, TimeTicks right)
{
object lo = left;
object ro = right;
if (lo == ro)
{
return true;
}
if (lo == null || ro == null)
{
return false;
}
return left._count == right._count;
}
}
}