Uploaded image for project: 'mdsal'
  1. mdsal
  2. MDSAL-736

Improve Decimal64 validity checks

XMLWordPrintable

    • Icon: New Feature New Feature
    • Resolution: Unresolved
    • Icon: Medium Medium
    • 14.0.0, 13.0.2
    • None
    • Binding codegen

      Our current checks currently look like this:

          private static final Range<Decimal64>[] CHECK_VALUERANGE_RANGES;
      
          static {
              @SuppressWarnings("unchecked")
              final Range<Decimal64>[] a = (Range<Decimal64>[]) Array.newInstance(Range.class, 1);
              a[0] = Range.closed(Decimal64.of(2, 7777L), Decimal64.of(2, 11111L));
              CHECK_VALUERANGE_RANGES = a;
          }
      
          private static void check_valueRange(final org.opendaylight.yangtools.yang.common.Decimal64 value) {
              for (Range<Decimal64> r : CHECK_VALUERANGE_RANGES) {
                  if (r.contains(value)) {
                      return;
                  }
              }
      
              CodeHelpers.throwInvalidRange(CHECK_VALUERANGE_RANGES, value);
          }
      
          @ConstructorParameters("value")
          @ConstructorProperties("value")
          public MyDerivedDecimal2(Decimal64 _value) {
              super(_value);
      
              if (_value != null) {
                  check_valueRange(_value);
              }
      
              CodeHelpers.requireValue(_value);
          }
      

      This has one major omission: we do not check Decimal64.scale(), which should always be matching fraction-digits.

      Once we check that, we can also improve performance and memory footprint: if the scale matches, then the range checks can be performed on the unscaled value – i.e. for a similar cost as our uint32/uint64 checks.

      Once we are in the area, we should also clean up the layout of the above:

      • the requireValue return should be used for field initialization, or
      • if we have superclass (as above) it should be omitted
      • since we have non-nullness established, there is no need for the if (_value != null) guard

            ivanhrasko Ivan Hrasko
            rovarga Robert Varga
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: