using System;
namespace Chernobyl.Measures.Time
{
///
/// A unit of time in the International System of Units (SI).
///
public struct Milliseconds : ITime, IEquatable, IComparable
{
///
/// Initializes a new instance of the struct.
///
/// The raw value of the measurement as a
/// .
public Milliseconds(double value) : this()
{
Value = value;
}
///
/// Converts this unit of time to .
///
/// This unit of measurement in .
public Seconds ToSeconds()
{
return this;
}
///
/// Performs an implicit conversion from to
/// .
///
/// The amount of time in .
/// The result of the conversion.
public static implicit operator Milliseconds(Seconds seconds)
{
return new Milliseconds(seconds.Value * 1000);
}
///
/// Performs an implicit conversion from to
/// .
///
/// The amount of time in .
/// The result of the conversion.
public static implicit operator Seconds(Milliseconds seconds)
{
return new Seconds(seconds.Value * .001);
}
///
/// Converts from to a floating point
/// representation of milliseconds.
///
/// The Milliseconds to convert to float
/// The representative float.
public static implicit operator double(Milliseconds milliseconds)
{
return milliseconds.Value;
}
///
/// Allows for a double to be explicitly casted to a .
///
/// The value to be casted.
/// The casted object.
public static explicit operator Milliseconds(double value)
{
return new Milliseconds(value);
}
///
/// Adds two .
///
/// The left side of the operation.
/// The right side of the operation.
/// The result of the operation.
public static Milliseconds operator +(Milliseconds left, Milliseconds right)
{
return new Milliseconds(left.Value + right.Value);
}
///
/// Subtracts two .
///
/// The left side of the operation.
/// The right side of the operation.
/// The result of the operation.
public static Milliseconds operator -(Milliseconds left, Milliseconds right)
{
return new Milliseconds(left.Value - right.Value);
}
///
/// Multiplies two .
///
/// The left side of the operation.
/// The right side of the operation.
/// The result of the operation.
public static Milliseconds operator *(Milliseconds left, Milliseconds right)
{
return new Milliseconds(left.Value * right.Value);
}
///
/// Divides two .
///
/// The left side of the operation.
/// The right side of the operation.
/// The result of the operation.
public static Milliseconds operator /(Milliseconds left, Milliseconds right)
{
return new Milliseconds(left.Value / right.Value);
}
///
/// Negates a .
///
/// The item to negate.
/// The result of the operation.
public static Milliseconds operator -(Milliseconds item)
{
return new Milliseconds(-item.Value);
}
///
/// Pluses a .
///
/// The item to plus.
/// The result of the operation.
public static Milliseconds operator +(Milliseconds item)
{
return new Milliseconds(+item.Value);
}
///
/// Checks if two are equal to each other.
///
/// The left side of the operation.
/// The right side of the operation.
/// True if the two are equal, false if otherwise.
public static bool operator ==(Milliseconds left, Milliseconds right)
{
return left.Equals(right);
}
///
/// Checks if two are equal to each other.
///
/// The left side of the operation.
/// The right side of the operation.
/// True if the two are different, false if otherwise.
public static bool operator !=(Milliseconds left, Milliseconds right)
{
return !(left == right);
}
///
/// Checks if one is less than another.
///
/// The left side of the operation.
/// The right side of the operation.
/// True if the first operand is less than the millisecond, false if
/// otherwise.
public static bool operator <(Milliseconds left, Milliseconds right)
{
return left.Value < right.Value;
}
///
/// Checks if one is more than another.
///
/// The left side of the operation.
/// The right side of the operation.
/// True if the first operand is more than the millisecond, false if
/// otherwise.
public static bool operator >(Milliseconds left, Milliseconds right)
{
return left.Value > right.Value;
}
///
/// Checks if one is less than or equal to
/// another.
///
/// The left side of the operation.
/// The right side of the operation.
/// True if the first operand is less than or equal to the
/// millisecond, false if otherwise.
public static bool operator <=(Milliseconds left, Milliseconds right)
{
return left.Value <= right.Value;
}
///
/// Checks if one is more than or equal to
/// another.
///
/// The left side of the operation.
/// The right side of the operation.
/// True if the first operand is more than or equal to the
/// millisecond, false if otherwise.
public static bool operator >=(Milliseconds left, Milliseconds right)
{
return left.Value >= right.Value;
}
///
/// Gets the hash code of the .
///
/// The hash code of the number.
public override int GetHashCode()
{
return Value.GetHashCode();
}
///
/// 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(Milliseconds other)
{
return Value == other.Value;
}
///
/// Checks this for equality with object passed in.
///
/// The object to compare to.
/// True if the object is equal to this ,
/// false if otherwise.
public override bool Equals(object obj)
{
return (obj is Milliseconds && Equals((Milliseconds)obj));
}
///
/// Compares the current object with another object of the same type.
///
/// An object to compare with this object.
/// A 32-bit signed integer that indicates the relative order
/// of the objects being compared. The return value has the following
/// meanings: if the value is less than zero then this object is less
/// than the parameter. If it is zero then
/// this object is equal to . If it is greater
/// than zero then this object is greater than .
///
public int CompareTo(Milliseconds other)
{
return Value.CompareTo(other.Value);
}
///
/// Returns a that represents this instance. For
/// example, 32.0 milliseconds will be returned as "32.0 ms".
///
/// A that represents this instance.
public override string ToString()
{
return Value + " ms";
}
///
/// The raw value of the measurement as a .
///
public double Value { get; private set; }
}
}