Exemplo n.º 1
0
        public void QuantityEquals()
        {
            var newq = new P.Quantity(3.14m, "kg");

            Assert.AreEqual(newq, new P.Quantity(3.14, "kg"));
            Assert.AreNotEqual(newq, new P.Quantity(3.15, "kg"));
        }
Exemplo n.º 2
0
        public void QuantityConstructor()
        {
            var newq = new P.Quantity(3.14m, "kg");

            Assert.AreEqual("kg", newq.Unit);
            Assert.AreEqual(3.14m, newq.Value);
        }
Exemplo n.º 3
0
 private static string convertToXmlString(object value)
 {
     return(value switch
     {
         bool bl => XmlConvert.ToString(bl),
         Byte by => XmlConvert.ToString(by),               // Not used in FHIR serialization
         Char cr => XmlConvert.ToString(cr),               // Not used in FHIR serialization
         DateTime dt => XmlConvert.ToString(dt, FMT_FULL), // Obsolete: use DateTimeOffset instead!!
         decimal dec => XmlConvert.ToString(dec),
         Double dbl => XmlConvert.ToString(dbl),
         Int16 i16 => XmlConvert.ToString(i16),
         Int32 i32 => XmlConvert.ToString(i32),
         Int64 i64 => XmlConvert.ToString(i64),        // Not used in FHIR serialization
         SByte sb => XmlConvert.ToString(sb),          // Not used in FHIR serialization
         Single sing => XmlConvert.ToString(sing),     // Not used in FHIR serialization
         UInt16 uint16 => XmlConvert.ToString(uint16), // Not used in FHIR serialization
         UInt32 uint32 => XmlConvert.ToString(uint32), // Not used in FHIR serialization
         UInt64 uint64 => XmlConvert.ToString(uint64), // Not used in FHIR serialization
         byte[] barr => System.Convert.ToBase64String(barr),
         DateTimeOffset dto => XmlConvert.ToString(dto, FMT_FULL),
         Uri uri => uri.ToString(),
         P.DateTime pdt => pdt.ToString(),
         P.Time pt => pt.ToString(),
         P.Date pd => pd.ToString(),
         Enum en => en.GetLiteral(),
         BigInteger bi => bi.ToString(),
         P.Quantity q => q.ToString(),
         _ => throw Error.NotSupported($"Cannot convert '{value.GetType().Name}' value '{value}' to string"),
     });
Exemplo n.º 4
0
 /// <summary>
 /// Creates an implementation of ITypedElement that represents a primitive value
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 // HACK: For now, allow a Quantity (which is NOT a primitive) in the .Value property
 // of ITypedElement. This is a temporary situation to make a quick & dirty upgrade of
 // FP to Normative (with Quantity support) possible.
 public static ITypedElement ForPrimitive(object value)
 {
     return(value switch
     {
         P.Quantity q => PrimitiveElement.ForQuantity(q),
         _ => new PrimitiveElement(value, useFullTypeName: true)
     });
Exemplo n.º 5
0
        internal static PrimitiveElement ForQuantity(P.Quantity value)
        {
            if (value is null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            return(new PrimitiveElement(value, TypeSpecifier.Quantity.FullName, "@QuantityAsPrimitiveValue@"));
        }
Exemplo n.º 6
0
        public void DifferentUnitsNotSupported()
        {
            var a = new P.Quantity(3.14m, "kg");
            var b = new P.Quantity(30.5, "g");

            ExceptionAssert.Throws <NotSupportedException>(() => a < b);
            ExceptionAssert.Throws <NotSupportedException>(() => a == b);
            ExceptionAssert.Throws <NotSupportedException>(() => a >= b);
            ExceptionAssert.Throws <NotSupportedException>(() => a.Equals(b));
        }
Exemplo n.º 7
0
        public void Comparison()
        {
            var smaller = new P.Quantity(3.14m, "kg");
            var bigger  = new P.Quantity(4.0, "kg");

            Assert.IsTrue(smaller < bigger);
#pragma warning disable CS1718 // Comparison made to same variable
            Assert.IsTrue(smaller <= smaller);
#pragma warning restore CS1718 // Comparison made to same variable
            Assert.IsTrue(bigger >= smaller);

            Assert.AreEqual(-1, smaller.CompareTo(bigger));
            Assert.AreEqual(1, bigger.CompareTo(smaller));
            Assert.AreEqual(0, smaller.CompareTo(smaller));
        }
Exemplo n.º 8
0
        public static bool TryParse(string value, Type primitiveType, out object?parsed)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (!typeof(Any).IsAssignableFrom(primitiveType))
            {
                throw new ArgumentException($"Must be a subclass of {nameof(Any)}.", nameof(primitiveType));
            }

            bool success;

            (success, parsed) = parse();
            return(success);

            (bool success, object?p) parse()
            {
                if (primitiveType == typeof(Boolean))
                {
                    return(Boolean.TryParse(value, out var p), p?.Value);
                }
                else if (primitiveType == typeof(Code))
                {
                    return(Code.TryParse(value, out var p), p);
                }
                else if (primitiveType == typeof(Concept))
                {
                    return(Concept.TryParse(value, out var p), p);
                }
                else if (primitiveType == typeof(Decimal))
                {
                    return(Decimal.TryParse(value, out var p), p?.Value);
                }
                else if (primitiveType == typeof(Integer))
                {
                    return(Integer.TryParse(value, out var p), p?.Value);
                }
                else if (primitiveType == typeof(Long))
                {
                    return(Long.TryParse(value, out var p), p?.Value);
                }
                else if (primitiveType == typeof(Date))
                {
                    return(Date.TryParse(value, out var p), p);
                }
                else if (primitiveType == typeof(DateTime))
                {
                    return(DateTime.TryParse(value, out var p), p);
                }
                else if (primitiveType == typeof(Time))
                {
                    return(Time.TryParse(value, out var p), p);
                }
                else if (primitiveType == typeof(Ratio))
                {
                    return(Ratio.TryParse(value, out var p), p);
                }
                else if (primitiveType == typeof(Quantity))
                {
                    return(Quantity.TryParse(value, out var p), p);
                }
                else if (primitiveType == typeof(String))
                {
                    return(String.TryParse(value, out var p), p?.Value);
                }
                else
                {
                    return(false, null);
                }
            }
        }
Exemplo n.º 9
0
 public Ratio(Quantity numerator, Quantity denominator)
 {
     Numerator   = numerator ?? throw new ArgumentNullException(nameof(numerator));
     Denominator = denominator ?? throw new ArgumentNullException(nameof(denominator));
 }