Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Timestamped.cs

raw
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&lt;T&gt; value has the same Value and Timestamp as a specified Timestamped&lt;T&gt; value.
51 /// </summary>
52 /// <param name="other">An object to compare to the current Timestamped&lt;T&gt; value.</param>
53 /// <returns>true if both Timestamped&lt;T&gt; 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&lt;T&gt; values have the same Value and Timestamp.
61 /// </summary>
62 /// <param name="first">The first Timestamped&lt;T&gt; value to compare.</param>
63 /// <param name="second">The second Timestamped&lt;T&gt; value to compare.</param>
64 /// <returns>true if the first Timestamped&lt;T&gt; value has the same Value and Timestamp as the second Timestamped&lt;T&gt; 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&lt;T&gt; values don't have the same Value and Timestamp.
72 /// </summary>
73 /// <param name="first">The first Timestamped&lt;T&gt; value to compare.</param>
74 /// <param name="second">The second Timestamped&lt;T&gt; value to compare.</param>
75 /// <returns>true if the first Timestamped&lt;T&gt; value has a different Value or Timestamp as the second Timestamped&lt;T&gt; 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&lt;T&gt;.
83 /// </summary>
84 /// <param name="obj">The System.Object to compare with the current Timestamped&lt;T&gt;.</param>
85 /// <returns>true if the specified System.Object is equal to the current Timestamped&lt;T&gt;; 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&lt;T&gt; value.
97 /// </summary>
98 /// <returns>A hash code for the current Timestamped&lt;T&gt; 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&lt;T&gt; value.
108 /// </summary>
109 /// <returns>String representation of the current Timestamped&lt;T&gt; 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&lt;T&gt; instances.
118 /// </summary>
119 public static class Timestamped
120 {
121 /// <summary>
122 /// Creates an instance of a Timestamped&lt;T&gt;. 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 }