Ejemplo n.º 1
0
 /// <summary>
 /// Converts an ADAPT NumericRepresentation value with an included UnitOfMeasure to the requested units
 /// </summary>
 /// <param name="value"></param>
 /// <param name="mapper"></param>
 /// <returns></returns>
 public static double?AsConvertedDouble(this NumericRepresentationValue value, string targetUnitCode)
 {
     if (value == null)
     {
         return(null);
     }
     else if (value.Value.UnitOfMeasure == null)
     {
         return(value.Value.Value); //Return the unconverted value
     }
     else
     {
         RepresentationUnitSystem.UnitOfMeasure sourceUOM = RepresentationUnitSystem.InternalUnitSystemManager.Instance.UnitOfMeasures[value.Value.UnitOfMeasure.Code];
         RepresentationUnitSystem.UnitOfMeasure targetUOM = RepresentationUnitSystem.InternalUnitSystemManager.Instance.UnitOfMeasures[targetUnitCode];
         if (sourceUOM == null || targetUOM == null)
         {
             return(value.Value.Value); //Return the unconverted value
         }
         else
         {
             RepresentationUnitSystem.UnitOfMeasureConverter converter = new RepresentationUnitSystem.UnitOfMeasureConverter();
             return(converter.Convert(sourceUOM, targetUOM, value.Value.Value));
         }
     }
 }
Ejemplo n.º 2
0
        public static double ConvertValue(this double n, string srcUnitCode, string dstUnitCode)
        {
            RepresentationUnitSystem.UnitOfMeasure sourceUOM = RepresentationUnitSystem.InternalUnitSystemManager.Instance.UnitOfMeasures[srcUnitCode];
            RepresentationUnitSystem.UnitOfMeasure targetUOM = RepresentationUnitSystem.InternalUnitSystemManager.Instance.UnitOfMeasures[dstUnitCode];
            if (sourceUOM == null || targetUOM == null)
            {
                return(n); //Return the unconverted value
            }
            else
            {
                //The plugin uses "count" instead of "seeds".  Alter the codes so that the conversion will succeed if there are mismatches
                if (srcUnitCode.StartsWith("seeds"))
                {
                    srcUnitCode = srcUnitCode.Replace("seeds", "count");
                    sourceUOM   = RepresentationUnitSystem.InternalUnitSystemManager.Instance.UnitOfMeasures[srcUnitCode];
                }
                if (dstUnitCode.StartsWith("seeds"))
                {
                    dstUnitCode = dstUnitCode.Replace("seeds", "count");
                    targetUOM   = RepresentationUnitSystem.InternalUnitSystemManager.Instance.UnitOfMeasures[dstUnitCode];
                }

                RepresentationUnitSystem.UnitOfMeasureConverter converter = new RepresentationUnitSystem.UnitOfMeasureConverter();
                return(converter.Convert(sourceUOM, targetUOM, n));
            }
        }
Ejemplo n.º 3
0
 public override int GetHashCode()
 {
     unchecked
     {
         var hashCode = Value.GetHashCode();
         hashCode = (hashCode * 397) ^ UnitOfMeasure.GetHashCode();
         hashCode = (hashCode * 397) ^ Representation;
         return(hashCode);
     }
 }
        public double Convert(UnitOfMeasure sourceUom, UnitOfMeasure targetUom, double sourceValue)
        {
            var scalarSource = sourceUom as ScalarUnitOfMeasure;
            var scalarTarget = targetUom as ScalarUnitOfMeasure;

            if (scalarSource != null)
            {
                if(scalarTarget != null)
                    return Convert(scalarSource, scalarTarget, sourceValue);
                return Convert(scalarSource, (CompositeUnitOfMeasure) targetUom, sourceValue);
            }
            if (scalarTarget != null)
                return Convert((CompositeUnitOfMeasure) sourceUom, scalarTarget, sourceValue);
            return Convert((CompositeUnitOfMeasure) sourceUom, (CompositeUnitOfMeasure) targetUom, sourceValue);
        }
Ejemplo n.º 5
0
        public double Convert(UnitOfMeasure sourceUom, UnitOfMeasure targetUom, double sourceValue)
        {
            var scalarSource = sourceUom as ScalarUnitOfMeasure;
            var scalarTarget = targetUom as ScalarUnitOfMeasure;

            if (scalarSource != null)
            {
                if (scalarTarget != null)
                {
                    return(Convert(scalarSource, scalarTarget, sourceValue));
                }
                return(Convert(scalarSource, (CompositeUnitOfMeasure)targetUom, sourceValue));
            }
            if (scalarTarget != null)
            {
                return(Convert((CompositeUnitOfMeasure)sourceUom, scalarTarget, sourceValue));
            }
            return(Convert((CompositeUnitOfMeasure)sourceUom, (CompositeUnitOfMeasure)targetUom, sourceValue));
        }
        public UnitOfMeasure CombineUnitsAsFraction(UnitOfMeasure numerator, UnitOfMeasure denominator)
        {
            var firstComponent  = new UnitOfMeasureComponent(numerator, 1);
            var secondComponent = new UnitOfMeasureComponent(denominator, -1);
            var firstDomainId   = numerator.DomainID;
            var secondDomainId  = denominator.DomainID;

            if (numerator is CompositeUnitOfMeasure)
            {
                firstDomainId = "[" + firstDomainId + "]";
            }
            if (denominator is CompositeUnitOfMeasure)
            {
                secondDomainId = "[" + secondDomainId + "]";
            }
            var newDomainId = firstDomainId + "1" + secondDomainId + "-1";

            return(new CompositeUnitOfMeasure(newDomainId, new [] { firstComponent, secondComponent }));
        }
Ejemplo n.º 7
0
        public UnitOfMeasure CombineUnitsAsFraction(UnitOfMeasure numerator, UnitOfMeasure denominator)
        {
            var firstComponent = new UnitOfMeasureComponent(numerator, 1);
            var secondComponent = new UnitOfMeasureComponent(denominator, -1);
            var firstDomainId = numerator.DomainID;
            var secondDomainId = denominator.DomainID;
            if (numerator is CompositeUnitOfMeasure)
                firstDomainId = "[" + firstDomainId + "]";
            if (denominator is CompositeUnitOfMeasure)
                secondDomainId = "[" + secondDomainId + "]";
            var newDomainId = firstDomainId + "1" + secondDomainId + "-1";

            return new CompositeUnitOfMeasure(newDomainId, new []{firstComponent, secondComponent});
        }