| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.Collections.Generic; |
| 4 |
using System.Globalization; |
| 5 |
|
| 6 |
namespace System.Reactive |
| 7 |
{ |
| 8 |
/// <summary> |
| 9 |
/// Represents value with a timestamp on it. |
| 10 |
/// The timestamp typically represents the time the value was received, using an IScheduler's clock to obtain the current time. |
| 11 |
/// </summary> |
| 12 |
/// <typeparam name="T">The type of the value being timestamped.</typeparam> |
| 13 |
#if !NO_SERIALIZABLE |
| 14 |
[Serializable] |
| 15 |
#endif |
| 16 |
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Timestamped", Justification = "Reviewed and agreed upon.")] |
| 17 |
public struct Timestamped<T> : IEquatable<Timestamped<T>> |
| 18 |
{ |
| 19 |
private readonly DateTimeOffset _timestamp; |
| 20 |
private readonly T _value; |
| 21 |
|
| 22 |
/// <summary> |
| 23 |
/// Constructs a timestamped value. |
| 24 |
/// </summary> |
| 25 |
/// <param name="value">The value to be annotated with a timestamp.</param> |
| 26 |
/// <param name="timestamp">Timestamp associated with the value.</param> |
| 27 |
public Timestamped(T value, DateTimeOffset timestamp) |
| 28 |
{ |
| 29 |
_timestamp = timestamp; |
| 30 |
_value = value; |
| 31 |
} |
| 32 |
|
| 33 |
/// <summary> |
| 34 |
/// Gets the value. |
| 35 |
/// </summary> |
| 36 |
public T Value |
| 37 |
{ |
| 38 |
get { return _value; } |
| 39 |
} |
| 40 |
|
| 41 |
/// <summary> |
| 42 |
/// Gets the timestamp. |
| 43 |
/// </summary> |
| 44 |
public DateTimeOffset Timestamp |
| 45 |
{ |
| 46 |
get { return _timestamp; } |
| 47 |
} |
| 48 |
|
| 49 |
/// <summary> |
| 50 |
/// Determines whether the current Timestamped<T> value has the same Value and Timestamp as a specified Timestamped<T> value. |
| 51 |
/// </summary> |
| 52 |
/// <param name="other">An object to compare to the current Timestamped<T> value.</param> |
| 53 |
/// <returns>true if both Timestamped<T> values have the same Value and Timestamp; otherwise, false.</returns> |
| 54 |
public bool Equals(Timestamped<T> other) |
| 55 |
{ |
| 56 |
return other.Timestamp.Equals(Timestamp) && EqualityComparer<T>.Default.Equals(Value, other.Value); |
| 57 |
} |
| 58 |
|
| 59 |
/// <summary> |
| 60 |
/// Determines whether the two specified Timestamped<T> values have the same Value and Timestamp. |
| 61 |
/// </summary> |
| 62 |
/// <param name="first">The first Timestamped<T> value to compare.</param> |
| 63 |
/// <param name="second">The second Timestamped<T> value to compare.</param> |
| 64 |
/// <returns>true if the first Timestamped<T> value has the same Value and Timestamp as the second Timestamped<T> value; otherwise, false.</returns> |
| 65 |
public static bool operator ==(Timestamped<T> first, Timestamped<T> second) |
| 66 |
{ |
| 67 |
return first.Equals(second); |
| 68 |
} |
| 69 |
|
| 70 |
/// <summary> |
| 71 |
/// Determines whether the two specified Timestamped<T> values don't have the same Value and Timestamp. |
| 72 |
/// </summary> |
| 73 |
/// <param name="first">The first Timestamped<T> value to compare.</param> |
| 74 |
/// <param name="second">The second Timestamped<T> value to compare.</param> |
| 75 |
/// <returns>true if the first Timestamped<T> value has a different Value or Timestamp as the second Timestamped<T> value; otherwise, false.</returns> |
| 76 |
public static bool operator !=(Timestamped<T> first, Timestamped<T> second) |
| 77 |
{ |
| 78 |
return !first.Equals(second); |
| 79 |
} |
| 80 |
|
| 81 |
/// <summary> |
| 82 |
/// Determines whether the specified System.Object is equal to the current Timestamped<T>. |
| 83 |
/// </summary> |
| 84 |
/// <param name="obj">The System.Object to compare with the current Timestamped<T>.</param> |
| 85 |
/// <returns>true if the specified System.Object is equal to the current Timestamped<T>; otherwise, false.</returns> |
| 86 |
public override bool Equals(object obj) |
| 87 |
{ |
| 88 |
if (!(obj is Timestamped<T>)) |
| 89 |
return false; |
| 90 |
|
| 91 |
var other = (Timestamped<T>)obj; |
| 92 |
return this.Equals(other); |
| 93 |
} |
| 94 |
|
| 95 |
/// <summary> |
| 96 |
/// Returns the hash code for the current Timestamped<T> value. |
| 97 |
/// </summary> |
| 98 |
/// <returns>A hash code for the current Timestamped<T> value.</returns> |
| 99 |
public override int GetHashCode() |
| 100 |
{ |
| 101 |
var valueHashCode = Value == null ? 1979 : Value.GetHashCode(); |
| 102 |
|
| 103 |
return _timestamp.GetHashCode() ^ valueHashCode; |
| 104 |
} |
| 105 |
|
| 106 |
/// <summary> |
| 107 |
/// Returns a string representation of the current Timestamped<T> value. |
| 108 |
/// </summary> |
| 109 |
/// <returns>String representation of the current Timestamped<T> value.</returns> |
| 110 |
public override string ToString() |
| 111 |
{ |
| 112 |
return String.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Timestamp); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/// <summary> |
| 117 |
/// A helper class with a factory method for creating Timestamped<T> instances. |
| 118 |
/// </summary> |
| 119 |
public static class Timestamped |
| 120 |
{ |
| 121 |
/// <summary> |
| 122 |
/// Creates an instance of a Timestamped<T>. This is syntactic sugar that uses type inference |
| 123 |
/// to avoid specifying a type in a constructor call, which is very useful when using anonymous types. |
| 124 |
/// </summary> |
| 125 |
/// <param name="value">The value to be annotated with a timestamp.</param> |
| 126 |
/// <param name="timestamp">Timestamp associated with the value.</param> |
| 127 |
/// <returns>Creates a new timestamped value.</returns> |
| 128 |
public static Timestamped<T> Create<T>(T value, DateTimeOffset timestamp) |
| 129 |
{ |
| 130 |
return new Timestamped<T>(value, timestamp); |
| 131 |
} |
| 132 |
} |
| 133 |
} |