1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
namespace Microsoft.Reactive.Testing
{
/// <summary>
/// Record of a value including the virtual time it was produced on.
/// </summary>
/// <typeparam name="T">Type of the value.</typeparam>
#if !NO_DEBUGGER_ATTRIBUTES
[DebuggerDisplay("{Value}@{Time}")]
#endif
#if !NO_SERIALIZABLE
[Serializable]
#endif
public struct Recorded<T> : IEquatable<Recorded<T>>
{
private readonly long _time;
private readonly T _value;
/// <summary>
/// Gets the virtual time the value was produced on.
/// </summary>
public long Time { get { return _time; } }
/// <summary>
/// Gets the recorded value.
/// </summary>
public T Value { get { return _value; } }
/// <summary>
/// Creates a new object recording the production of the specified value at the given virtual time.
/// </summary>
/// <param name="time">Virtual time the value was produced on.</param>
/// <param name="value">Value that was produced.</param>
public Recorded(long time, T value)
{
_time = time;
_value = value;
}
/// <summary>
/// Checks whether the given recorded object is equal to the current instance.
/// </summary>
/// <param name="other">Recorded object to check for equality.</param>
/// <returns>true if both objects are equal; false otherwise.</returns>
public bool Equals(Recorded<T> other)
{
return Time == other.Time && EqualityComparer<T>.Default.Equals(Value, other.Value);
}
/// <summary>
/// Determines whether the two specified Recorded<T> values have the same Time and Value.
/// </summary>
/// <param name="left">The first Recorded<T> value to compare.</param>
/// <param name="right">The second Recorded<T> value to compare.</param>
/// <returns>true if the first Recorded<T> value has the same Time and Value as the second Recorded<T> value; otherwise, false.</returns>
public static bool operator ==(Recorded<T> left, Recorded<T> right)
{
return left.Equals(right);
}
/// <summary>
/// Determines whether the two specified Recorded<T> values don't have the same Time and Value.
/// </summary>
/// <param name="left">The first Recorded<T> value to compare.</param>
/// <param name="right">The second Recorded<T> value to compare.</param>
/// <returns>true if the first Recorded<T> value has a different Time or Value as the second Recorded<T> value; otherwise, false.</returns>
public static bool operator !=(Recorded<T> left, Recorded<T> right)
{
return !left.Equals(right);
}
/// <summary>
/// Determines whether the specified System.Object is equal to the current Recorded<T> value.
/// </summary>
/// <param name="obj">The System.Object to compare with the current Recorded<T> value.</param>
/// <returns>true if the specified System.Object is equal to the current Recorded<T> value; otherwise, false.</returns>
public override bool Equals(object obj)
{
if (obj is Recorded<T>)
return Equals((Recorded<T>)obj);
return false;
}
/// <summary>
/// Returns the hash code for the current Recorded<T> value.
/// </summary>
/// <returns>A hash code for the current Recorded<T> value.</returns>
public override int GetHashCode()
{
return Time.GetHashCode() + EqualityComparer<T>.Default.GetHashCode(Value);
}
/// <summary>
/// Returns a string representation of the current Recorded<T> value.
/// </summary>
/// <returns>String representation of the current Recorded<T> value.</returns>
public override string ToString()
{
return Value.ToString() + "@" + Time.ToString(CultureInfo.CurrentCulture);
}
}
}
|