using System;
using System.Collections.Generic;
using System.Globalization;
namespace UniRx
{
///
/// Represents a value associated with time interval information.
/// The time interval can represent the time it took to produce the value, the interval relative to a previous value, the value's delivery time relative to a base, etc.
///
/// The type of the value being annotated with time interval information.
[Serializable]
public struct FrameInterval : IEquatable>
{
private readonly int _interval;
private readonly T _value;
///
/// Constructs a time interval value.
///
/// The value to be annotated with a time interval.
/// Time interval associated with the value.
public FrameInterval(T value, int interval)
{
_interval = interval;
_value = value;
}
///
/// Gets the value.
///
public T Value
{
get { return _value; }
}
///
/// Gets the interval.
///
public int Interval
{
get { return _interval; }
}
///
/// Determines whether the current FrameInterval<T> value has the same Value and Interval as a specified FrameInterval<T> value.
///
/// An object to compare to the current FrameInterval<T> value.
/// true if both FrameInterval<T> values have the same Value and Interval; otherwise, false.
public bool Equals(FrameInterval other)
{
return other.Interval.Equals(Interval) && EqualityComparer.Default.Equals(Value, other.Value);
}
///
/// Determines whether the two specified FrameInterval<T> values have the same Value and Interval.
///
/// The first FrameInterval<T> value to compare.
/// The second FrameInterval<T> value to compare.
/// true if the first FrameInterval<T> value has the same Value and Interval as the second FrameInterval<T> value; otherwise, false.
public static bool operator ==(FrameInterval first, FrameInterval second)
{
return first.Equals(second);
}
///
/// Determines whether the two specified FrameInterval<T> values don't have the same Value and Interval.
///
/// The first FrameInterval<T> value to compare.
/// The second FrameInterval<T> value to compare.
/// true if the first FrameInterval<T> value has a different Value or Interval as the second FrameInterval<T> value; otherwise, false.
public static bool operator !=(FrameInterval first, FrameInterval second)
{
return !first.Equals(second);
}
///
/// Determines whether the specified System.Object is equal to the current FrameInterval<T>.
///
/// The System.Object to compare with the current FrameInterval<T>.
/// true if the specified System.Object is equal to the current FrameInterval<T>; otherwise, false.
public override bool Equals(object obj)
{
if (!(obj is FrameInterval))
return false;
var other = (FrameInterval)obj;
return this.Equals(other);
}
///
/// Returns the hash code for the current FrameInterval<T> value.
///
/// A hash code for the current FrameInterval<T> value.
public override int GetHashCode()
{
var valueHashCode = Value == null ? 1963 : Value.GetHashCode();
return Interval.GetHashCode() ^ valueHashCode;
}
///
/// Returns a string representation of the current FrameInterval<T> value.
///
/// String representation of the current FrameInterval<T> value.
public override string ToString()
{
return String.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Interval);
}
}
}