/// <summary> /// Sets the conversion function for a particular conversion function lookup. /// </summary> /// <typeparam name="TQuantity">The quantity type, must implement <see cref="IQuantity"/>.</typeparam> /// <param name="conversionLookup">The quantity conversion function lookup key.</param> /// <param name="conversionFunction">The quantity conversion function.</param> internal void SetConversionFunction <TQuantity>(ConversionFunctionLookupKey conversionLookup, ConversionFunction <TQuantity> conversionFunction) where TQuantity : IQuantity { IQuantity TypelessConversionFunction(IQuantity quantity) => conversionFunction((TQuantity)quantity); _conversionFunctions[conversionLookup] = TypelessConversionFunction; }
/// <summary> /// Sets the conversion function from two units of the same quantity type. /// </summary> /// <typeparam name="TQuantity">The type of quantity, must implement <see cref="IQuantity"/>.</typeparam> /// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param> /// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param> /// <param name="conversionFunction">The quantity conversion function.</param> public void SetConversionFunction <TQuantity>(Enum from, Enum to, ConversionFunction <TQuantity> conversionFunction) where TQuantity : IQuantity { var quantityType = typeof(TQuantity); var conversionLookup = new ConversionFunctionLookupKey(quantityType, from, quantityType, to); SetConversionFunction(conversionLookup, conversionFunction); }
/// <summary> /// Gets the conversion function by its lookup key. /// </summary> /// <param name="lookupKey"></param> internal ConversionFunction GetConversionFunction(ConversionFunctionLookupKey lookupKey) { IQuantity EchoFunction(IQuantity fromQuantity) => fromQuantity; // If from/to units and to quantity types are equal, then return a function that echoes the input quantity // in order to not have to map conversion functions to "self". if (lookupKey.Item1 == lookupKey.Item3 && Equals(lookupKey.Item2, lookupKey.Item4)) { return(EchoFunction); } return(_conversionFunctions[lookupKey]); }
/// <summary> /// Sets the conversion function for a particular conversion function lookup. /// </summary> /// <param name="lookupKey">The lookup key.</param> /// <param name="conversionFunction">The quantity conversion function.</param> internal void SetConversionFunction(ConversionFunctionLookupKey lookupKey, ConversionFunction conversionFunction) { _conversionFunctions[lookupKey] = conversionFunction; }
/// <summary> /// Sets the conversion function from two units of different quantity types. /// </summary> /// <param name="fromType">From quantity type, must implement <see cref="IQuantity"/>.</param> /// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param> /// <param name="toType">To quantity type, must implement <see cref="IQuantity"/>.</param> /// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param> /// <param name="conversionFunction">The quantity conversion function.</param> public void SetConversionFunction(Type fromType, Enum from, Type toType, Enum to, ConversionFunction conversionFunction) { var conversionLookup = new ConversionFunctionLookupKey(fromType, from, toType, to); SetConversionFunction(conversionLookup, conversionFunction); }
/// <summary> /// /// </summary> /// <param name="lookupKey"></param> /// <param name="conversionFunction"></param> /// <returns>true if set; otherwise, false.</returns> public bool TryGetConversionFunction(ConversionFunctionLookupKey lookupKey, out ConversionFunction conversionFunction) { return(_conversionFunctions.TryGetValue(lookupKey, out conversionFunction)); }
/// <summary> /// Try to get the conversion function for two units of the same quantity type. /// </summary> /// <param name="fromType">From quantity type, must implement <see cref="IQuantity"/>.</param> /// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param> /// <param name="toType">To quantity type, must implement <see cref="IQuantity"/>.</param> /// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param> /// <param name="conversionFunction">The quantity conversion function.</param> /// <returns>true if set; otherwise, false.</returns> public bool TryGetConversionFunction(Type fromType, Enum from, Type toType, Enum to, out ConversionFunction conversionFunction) { var conversionLookup = new ConversionFunctionLookupKey(fromType, from, toType, to); return(TryGetConversionFunction(conversionLookup, out conversionFunction)); }
/// <summary> /// Gets the conversion function from two units of different quantity types. /// </summary> /// <param name="fromType">From quantity type, must implement <see cref="IQuantity"/>.</param> /// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param> /// <param name="toType">To quantity type, must implement <see cref="IQuantity"/>.</param> /// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param> public ConversionFunction GetConversionFunction(Type fromType, Enum from, Type toType, Enum to) { var conversionLookup = new ConversionFunctionLookupKey(fromType, from, toType, to); return(GetConversionFunction(conversionLookup)); }