// 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 value with a timestamp on it.
    /// The timestamp typically represents the time the value was received, using an IScheduler's clock to obtain the current time.
    /// 
    /// The type of the value being timestamped.
#if !NO_SERIALIZABLE
    [Serializable]
#endif
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Timestamped", Justification = "Reviewed and agreed upon.")]
    public struct Timestamped : IEquatable>
    {
        private readonly DateTimeOffset _timestamp;
        private readonly T _value;
        /// 
        /// Constructs a timestamped value.
        /// 
        /// The value to be annotated with a timestamp.
        /// Timestamp associated with the value.
        public Timestamped(T value, DateTimeOffset timestamp)
        {
            _timestamp = timestamp;
            _value = value;
        }
        /// 
        /// Gets the value.
        /// 
        public T Value
        {
            get { return _value; }
        }
        /// 
        /// Gets the timestamp.
        /// 
        public DateTimeOffset Timestamp
        {
            get { return _timestamp; }
        }
        /// 
        /// Determines whether the current Timestamped<T> value has the same Value and Timestamp as a specified Timestamped<T> value.
        /// 
        /// An object to compare to the current Timestamped<T> value.
        /// true if both Timestamped<T> values have the same Value and Timestamp; otherwise, false.
        public bool Equals(Timestamped other)
        {
            return other.Timestamp.Equals(Timestamp) && EqualityComparer.Default.Equals(Value, other.Value);
        }
        /// 
        /// Determines whether the two specified Timestamped<T> values have the same Value and Timestamp.
        /// 
        /// The first Timestamped<T> value to compare.
        /// The second Timestamped<T> value to compare.
        /// true if the first Timestamped<T> value has the same Value and Timestamp as the second Timestamped<T> value; otherwise, false.
        public static bool operator ==(Timestamped first, Timestamped second)
        {
            return first.Equals(second);
        }
        /// 
        /// Determines whether the two specified Timestamped<T> values don't have the same Value and Timestamp.
        /// 
        /// The first Timestamped<T> value to compare.
        /// The second Timestamped<T> value to compare.
        /// true if the first Timestamped<T> value has a different Value or Timestamp as the second Timestamped<T> value; otherwise, false.
        public static bool operator !=(Timestamped first, Timestamped second)
        {
            return !first.Equals(second);
        }
        /// 
        /// Determines whether the specified System.Object is equal to the current Timestamped<T>.
        /// 
        /// The System.Object to compare with the current Timestamped<T>.
        /// true if the specified System.Object is equal to the current Timestamped<T>; otherwise, false.
        public override bool Equals(object obj)
        {
            if (!(obj is Timestamped))
                return false;
            var other = (Timestamped)obj;
            return this.Equals(other);
        }
        /// 
        /// Returns the hash code for the current Timestamped<T> value.
        /// 
        /// A hash code for the current Timestamped<T> value.
        public override int GetHashCode()
        {
            var valueHashCode = Value == null ? 1979 : Value.GetHashCode();
            return _timestamp.GetHashCode() ^ valueHashCode;
        }
        /// 
        /// Returns a string representation of the current Timestamped<T> value.
        /// 
        /// String representation of the current Timestamped<T> value.
        public override string ToString()
        {
            return String.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Timestamp);
        }
    }
    /// 
    /// A helper class with a factory method for creating Timestamped<T> instances.
    /// 
    public static class Timestamped
    {
        /// 
        /// Creates an instance of a Timestamped<T>.  This is syntactic sugar that uses type inference
        /// to avoid specifying a type in a constructor call, which is very useful when using anonymous types.
        /// 
        /// The value to be annotated with a timestamp.
        /// Timestamp associated with the value.
        /// Creates a new timestamped value.
        public static Timestamped Create(T value, DateTimeOffset timestamp)
        {
            return new Timestamped(value, timestamp);
        }
    }
}