// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using System;
namespace System.Windows.Controls.DataVisualization
{
///
/// A value in units.
///
public struct UnitValue : IComparable
{
///
/// Returns a UnitValue representing an invalid value.
///
/// UnitValue instance.
public static UnitValue NaN()
{
return new UnitValue { Value = double.NaN };
}
///
/// Instantiates a new instance of the UnitValue struct.
///
/// The value associated with the units.
/// The units associated with the value.
public UnitValue(double value, Unit unit) : this()
{
Value = value;
Unit = unit;
}
///
/// Gets the value associated with the units.
///
public double Value { get; private set; }
///
/// Gets the units associated with the value.
///
public Unit Unit { get; private set; }
///
/// Compares two unit values to determine if they are equal or not.
///
/// The object being compared.
/// A number smaller than zero if the obj is larger than this
/// object. A number equal to 0 if they are equal. A number greater
/// than zero if this unit value is greater than obj.
public int CompareTo(object obj)
{
UnitValue unitValue = (UnitValue) obj;
if (unitValue.Unit != this.Unit)
{
throw new InvalidOperationException("Cannot compare two unit values with different units.");
}
return this.Value.CompareTo(unitValue.Value);
}
///
/// Determines if two values are equal.
///
/// The other value.
/// A value indicating whether values are equal.
public override bool Equals(object obj)
{
if (!(obj is UnitValue))
{
return false;
}
UnitValue unitValue = (UnitValue)obj;
if ((Object.ReferenceEquals(unitValue.Value, this.Value) || Object.Equals(unitValue.Value, this.Value)) && unitValue.Unit == this.Unit)
{
return true;
}
return false;
}
///
/// Determines whether two unit value objects are equal.
///
/// The left unit value.
/// The right unit value.
/// A value indicating whether two unit value objects are
/// equal.
public static bool operator ==(UnitValue left, UnitValue right)
{
return left.Equals(right);
}
///
/// Determines whether two unit value objects are not equal.
///
/// The left unit value.
/// The right unit value.
/// A value indicating whether two unit value objects are not
/// equal.
public static bool operator !=(UnitValue left, UnitValue right)
{
return !left.Equals(right);
}
///
/// Determines whether the left value is smaller than the right.
///
/// The left unit value.
/// The right unit value.
/// A value indicating whether the left value is smaller than
/// the right.
public static bool operator <(UnitValue left, UnitValue right)
{
return left.CompareTo(right) < 0;
}
///
/// Determines whether the left value is larger than the right.
///
/// The left unit value.
/// The right unit value.
/// A value indicating whether the left value is larger than
/// the right.
public static bool operator >(UnitValue left, UnitValue right)
{
return left.CompareTo(right) > 0;
}
///
/// Returns the hash code of the unit value object.
///
/// The hash code.
public override int GetHashCode()
{
unchecked
{
return this.Value.GetHashCode() + (int)this.Unit;
}
}
}
}