diff options
author | Atsushi Eno <atsushieno@veritas-vos-liberabit.com> | 2012-11-12 22:47:31 +0400 |
---|---|---|
committer | Atsushi Eno <atsushieno@veritas-vos-liberabit.com> | 2012-11-12 22:52:17 +0400 |
commit | d1174f3f8979321a9182925df460e07e08157b41 (patch) | |
tree | d16fb2fc191bf68ff0e2aac600adf71aba8cad01 /Rx.NET/System.Reactive.Linq/Reactive/Timestamped.cs | |
parent | d90a52595e24b1216c89f6cb5f245262db1810ae (diff) |
partial import of ca05fdeb565e: Reactive Extensions OSS V1.0
Diffstat (limited to 'Rx.NET/System.Reactive.Linq/Reactive/Timestamped.cs')
-rw-r--r-- | Rx.NET/System.Reactive.Linq/Reactive/Timestamped.cs | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/Rx.NET/System.Reactive.Linq/Reactive/Timestamped.cs b/Rx.NET/System.Reactive.Linq/Reactive/Timestamped.cs new file mode 100644 index 0000000..896c451 --- /dev/null +++ b/Rx.NET/System.Reactive.Linq/Reactive/Timestamped.cs @@ -0,0 +1,115 @@ +// 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 +{ + /// <summary> + /// 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. + /// </summary> + /// <typeparam name="T">The type of the value being timestamped.</typeparam> +#if !NO_SERIALIZABLE + [Serializable] +#endif + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Timestamped", Justification = "Reviewed and agreed upon.")] + public struct Timestamped<T> : IEquatable<Timestamped<T>> + { + private readonly DateTimeOffset _timestamp; + private readonly T _value; + + /// <summary> + /// Constructs a timestamped value. + /// </summary> + /// <param name="value">The value to be annotated with a timestamp.</param> + /// <param name="timestamp">Timestamp associated with the value.</param> + public Timestamped(T value, DateTimeOffset timestamp) + { + _timestamp = timestamp; + _value = value; + } + + /// <summary> + /// Gets the value. + /// </summary> + public T Value + { + get { return _value; } + } + + /// <summary> + /// Gets the timestamp. + /// </summary> + public DateTimeOffset Timestamp + { + get { return _timestamp; } + } + + /// <summary> + /// Determines whether the current Timestamped<T> value has the same Value and Timestamp as a specified Timestamped<T> value. + /// </summary> + /// <param name="other">An object to compare to the current Timestamped<T> value.</param> + /// <returns>true if both Timestamped<T> values have the same Value and Timestamp; otherwise, false.</returns> + public bool Equals(Timestamped<T> other) + { + return other.Timestamp.Equals(Timestamp) && EqualityComparer<T>.Default.Equals(Value, other.Value); + } + + /// <summary> + /// Determines whether the two specified Timestamped<T> values have the same Value and Timestamp. + /// </summary> + /// <param name="first">The first Timestamped<T> value to compare.</param> + /// <param name="second">The second Timestamped<T> value to compare.</param> + /// <returns>true if the first Timestamped<T> value has the same Value and Timestamp as the second Timestamped<T> value; otherwise, false.</returns> + public static bool operator ==(Timestamped<T> first, Timestamped<T> second) + { + return first.Equals(second); + } + + /// <summary> + /// Determines whether the two specified Timestamped<T> values don't have the same Value and Timestamp. + /// </summary> + /// <param name="first">The first Timestamped<T> value to compare.</param> + /// <param name="second">The second Timestamped<T> value to compare.</param> + /// <returns>true if the first Timestamped<T> value has a different Value or Timestamp as the second Timestamped<T> value; otherwise, false.</returns> + public static bool operator !=(Timestamped<T> first, Timestamped<T> second) + { + return !first.Equals(second); + } + + /// <summary> + /// Determines whether the specified System.Object is equal to the current Timestamped<T>. + /// </summary> + /// <param name="obj">The System.Object to compare with the current Timestamped<T>.</param> + /// <returns>true if the specified System.Object is equal to the current Timestamped<T>; otherwise, false.</returns> + public override bool Equals(object obj) + { + if (!(obj is Timestamped<T>)) + return false; + + var other = (Timestamped<T>)obj; + return this.Equals(other); + } + + /// <summary> + /// Returns the hash code for the current Timestamped<T> value. + /// </summary> + /// <returns>A hash code for the current Timestamped<T> value.</returns> + public override int GetHashCode() + { + var valueHashCode = Value == null ? 1979 : Value.GetHashCode(); + + return _timestamp.GetHashCode() ^ valueHashCode; + } + + /// <summary> + /// Returns a string representation of the current Timestamped<T> value. + /// </summary> + /// <returns>String representation of the current Timestamped<T> value.</returns> + public override string ToString() + { + return String.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Timestamp); + } + } +} |