| 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 a value associated with time interval information. |
| 10 |
/// 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. |
| 11 |
/// </summary> |
| 12 |
/// <typeparam name="T">The type of the value being annotated with time interval information.</typeparam> |
| 13 |
#if !NO_SERIALIZABLE |
| 14 |
[Serializable] |
| 15 |
#endif |
| 16 |
public struct TimeInterval<T> : IEquatable<TimeInterval<T>> |
| 17 |
{ |
| 18 |
private readonly TimeSpan _interval; |
| 19 |
private readonly T _value; |
| 20 |
|
| 21 |
/// <summary> |
| 22 |
/// Constructs a time interval value. |
| 23 |
/// </summary> |
| 24 |
/// <param name="value">The value to be annotated with a time interval.</param> |
| 25 |
/// <param name="interval">Time interval associated with the value.</param> |
| 26 |
public TimeInterval(T value, TimeSpan interval) |
| 27 |
{ |
| 28 |
_interval = interval; |
| 29 |
_value = value; |
| 30 |
} |
| 31 |
|
| 32 |
/// <summary> |
| 33 |
/// Gets the value. |
| 34 |
/// </summary> |
| 35 |
public T Value |
| 36 |
{ |
| 37 |
get { return _value; } |
| 38 |
} |
| 39 |
|
| 40 |
/// <summary> |
| 41 |
/// Gets the interval. |
| 42 |
/// </summary> |
| 43 |
public TimeSpan Interval |
| 44 |
{ |
| 45 |
get { return _interval; } |
| 46 |
} |
| 47 |
|
| 48 |
/// <summary> |
| 49 |
/// Determines whether the current TimeInterval<T> value has the same Value and Interval as a specified TimeInterval<T> value. |
| 50 |
/// </summary> |
| 51 |
/// <param name="other">An object to compare to the current TimeInterval<T> value.</param> |
| 52 |
/// <returns>true if both TimeInterval<T> values have the same Value and Interval; otherwise, false.</returns> |
| 53 |
public bool Equals(TimeInterval<T> other) |
| 54 |
{ |
| 55 |
return other.Interval.Equals(Interval) && EqualityComparer<T>.Default.Equals(Value, other.Value); |
| 56 |
} |
| 57 |
|
| 58 |
/// <summary> |
| 59 |
/// Determines whether the two specified TimeInterval<T> values have the same Value and Interval. |
| 60 |
/// </summary> |
| 61 |
/// <param name="first">The first TimeInterval<T> value to compare.</param> |
| 62 |
/// <param name="second">The second TimeInterval<T> value to compare.</param> |
| 63 |
/// <returns>true if the first TimeInterval<T> value has the same Value and Interval as the second TimeInterval<T> value; otherwise, false.</returns> |
| 64 |
public static bool operator ==(TimeInterval<T> first, TimeInterval<T> second) |
| 65 |
{ |
| 66 |
return first.Equals(second); |
| 67 |
} |
| 68 |
|
| 69 |
/// <summary> |
| 70 |
/// Determines whether the two specified TimeInterval<T> values don't have the same Value and Interval. |
| 71 |
/// </summary> |
| 72 |
/// <param name="first">The first TimeInterval<T> value to compare.</param> |
| 73 |
/// <param name="second">The second TimeInterval<T> value to compare.</param> |
| 74 |
/// <returns>true if the first TimeInterval<T> value has a different Value or Interval as the second TimeInterval<T> value; otherwise, false.</returns> |
| 75 |
public static bool operator !=(TimeInterval<T> first, TimeInterval<T> second) |
| 76 |
{ |
| 77 |
return !first.Equals(second); |
| 78 |
} |
| 79 |
|
| 80 |
/// <summary> |
| 81 |
/// Determines whether the specified System.Object is equal to the current TimeInterval<T>. |
| 82 |
/// </summary> |
| 83 |
/// <param name="obj">The System.Object to compare with the current TimeInterval<T>.</param> |
| 84 |
/// <returns>true if the specified System.Object is equal to the current TimeInterval<T>; otherwise, false.</returns> |
| 85 |
public override bool Equals(object obj) |
| 86 |
{ |
| 87 |
if (!(obj is TimeInterval<T>)) |
| 88 |
return false; |
| 89 |
|
| 90 |
var other = (TimeInterval<T>)obj; |
| 91 |
return this.Equals(other); |
| 92 |
} |
| 93 |
|
| 94 |
/// <summary> |
| 95 |
/// Returns the hash code for the current TimeInterval<T> value. |
| 96 |
/// </summary> |
| 97 |
/// <returns>A hash code for the current TimeInterval<T> value.</returns> |
| 98 |
public override int GetHashCode() |
| 99 |
{ |
| 100 |
var valueHashCode = Value == null ? 1963 : Value.GetHashCode(); |
| 101 |
|
| 102 |
return Interval.GetHashCode() ^ valueHashCode; |
| 103 |
} |
| 104 |
|
| 105 |
/// <summary> |
| 106 |
/// Returns a string representation of the current TimeInterval<T> value. |
| 107 |
/// </summary> |
| 108 |
/// <returns>String representation of the current TimeInterval<T> value.</returns> |
| 109 |
public override string ToString() |
| 110 |
{ |
| 111 |
return String.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Interval); |
| 112 |
} |
| 113 |
} |
| 114 |
} |