Exemplo n.º 1
0
 private static void VerifySameDimension(QuantityBase x, QuantityBase y)
 {
     if (!Equals(x.Unit, y.Unit))
     {
         throw new InvalidOperationException("Quantitites must have same units");
     }
 }
Exemplo n.º 2
0
 private static void VerifySameUnitsIfSameDimension(QuantityBase x, QuantityBase y)
 {
     if (Equals(x.Dimension, y.Dimension) &&
         !Equals(x.Unit, y.Unit))
     {
         throw new NotSupportedException("Performing arithmetic on two quantities that has the same dimension but different units are not supported - yet.");
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Subtracts the right quantity from the left one. The exact type of the two quantities must be the same.
 /// </summary>
 /// <exception cref="InvalidOperationException">
 /// When there is no registered operation for the type, or the type of the terms differ.
 /// </exception>
 /// <exception cref="ArgumentException">
 /// When the units of the provided quantities differ.
 /// </exception>
 /// <param name="left"></param>
 /// <param name="right"></param>
 /// <returns>left - right</returns>
 public QuantityBase Subtract(QuantityBase left, QuantityBase right)
 {
     // Units must be equal
     if (!Equals(left.Unit, right.Unit))
     {
         throw new ArgumentException(string.Format("Units of quantities must be the same (was '{0}' and '{1}'", left.Unit, right.Unit));
     }
     return(_substractionStore.PerformOperation(left, right));
 }
Exemplo n.º 4
0
        /// <summary>
        /// Performns a pre-registered operation on the left and right quantities. The operation
        /// must be registreded using AddOperation.
        /// </summary>
        /// <exception cref="NullReferenceException">If any of the arguments are null.</exception>
        /// <exception cref="InvalidOperationException">If the operation are not supported.</exception>
        /// <param name="left"></param>
        /// <param name="right"></param>
        public QuantityBase PerformOperation(QuantityBase left, QuantityBase right)
        {
            if (left == null || right == null)
            {
                throw new NullReferenceException("Arguments cannot be null.");
            }

            Func <QuantityBase, QuantityBase, QuantityBase> operation;
            OperationKey key = new OperationKey(left.GetType(), right.GetType());

            if (_operations.TryGetValue(key, out operation))
            {
                return(operation(left, right));
            }

            throw new InvalidOperationException(string.Format("Operation op('{0}', '{1}') is not supported.", left.GetType(), right.GetType()));
        }
Exemplo n.º 5
0
 /// <summary>
 /// Divides the dividend quantity with the divisor quantity. There must be a registered division
 /// operation for the given types.
 /// </summary>
 /// <exception cref="InvalidOperationException">When there is no registered operation for the types.</exception>
 /// <param name="dividend"></param>
 /// <param name="divisor"></param>
 /// <returns>dividend / divisor</returns>
 public QuantityBase Divide(QuantityBase dividend, QuantityBase divisor)
 {
     return(_divisionStore.PerformOperation(dividend, divisor));
 }
Exemplo n.º 6
0
 /// <summary>
 /// Multiplies the left quantity with the right one. There must be a registered multiplication
 /// operation for the given types.
 /// </summary>
 /// <exception cref="InvalidOperationException">When there is no registered operation for the types.</exception>
 /// <param name="left"></param>
 /// <param name="right"></param>
 /// <returns>left*right</returns>
 public QuantityBase Multiply(QuantityBase left, QuantityBase right)
 {
     return(_multiplicationStore.PerformOperation(left, right));
 }