// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Globalization;
namespace System.Reactive
{
    /// 
    /// 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.
#if !NO_SERIALIZABLE
    [Serializable]
#endif
    public struct TimeInterval : IEquatable>
    {
        private readonly TimeSpan _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 TimeInterval(T value, TimeSpan interval)
        {
            _interval = interval;
            _value = value;
        }
        /// 
        /// Gets the value.
        /// 
        public T Value
        {
            get { return _value; }
        }
        /// 
        /// Gets the interval.
        /// 
        public TimeSpan Interval
        {
            get { return _interval; }
        }
        /// 
        /// Determines whether the current TimeInterval<T> value has the same Value and Interval as a specified TimeInterval<T> value.
        /// 
        /// An object to compare to the current TimeInterval<T> value.
        /// true if both TimeInterval<T> values have the same Value and Interval; otherwise, false.
        public bool Equals(TimeInterval other)
        {
            return other.Interval.Equals(Interval) && EqualityComparer.Default.Equals(Value, other.Value);
        }
        /// 
        /// Determines whether the two specified TimeInterval<T> values have the same Value and Interval.
        /// 
        /// The first TimeInterval<T> value to compare.
        /// The second TimeInterval<T> value to compare.
        /// true if the first TimeInterval<T> value has the same Value and Interval as the second TimeInterval<T> value; otherwise, false.
        public static bool operator ==(TimeInterval first, TimeInterval second)
        {
            return first.Equals(second);
        }
        /// 
        /// Determines whether the two specified TimeInterval<T> values don't have the same Value and Interval.
        /// 
        /// The first TimeInterval<T> value to compare.
        /// The second TimeInterval<T> value to compare.
        /// true if the first TimeInterval<T> value has a different Value or Interval as the second TimeInterval<T> value; otherwise, false.
        public static bool operator !=(TimeInterval first, TimeInterval second)
        {
            return !first.Equals(second);
        }
        /// 
        /// Determines whether the specified System.Object is equal to the current TimeInterval<T>.
        /// 
        /// The System.Object to compare with the current TimeInterval<T>.
        /// true if the specified System.Object is equal to the current TimeInterval<T>; otherwise, false.
        public override bool Equals(object obj)
        {
            if (!(obj is TimeInterval))
                return false;
            var other = (TimeInterval)obj;
            return this.Equals(other);
        }
        /// 
        /// Returns the hash code for the current TimeInterval<T> value.
        /// 
        /// A hash code for the current TimeInterval<T> value.
        public override int GetHashCode()
        {
            var valueHashCode = Value == null ? 1963 : Value.GetHashCode();
            return Interval.GetHashCode() ^ valueHashCode;
        }
        /// 
        /// Returns a string representation of the current TimeInterval<T> value.
        /// 
        /// String representation of the current TimeInterval<T> value.
        public override string ToString()
        {
            return String.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Interval);
        }
    }
}