[MDSAL-736] Improve Decimal64 validity checks Created: 14/Mar/22  Updated: 22/Jan/24

Status: In Review
Project: mdsal
Component/s: Binding codegen
Affects Version/s: None
Fix Version/s: 14.0.0, 13.0.1

Type: New Feature Priority: Medium
Reporter: Robert Varga Assignee: Ivan Hrasko
Resolution: Unresolved Votes: 0
Labels: pt
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Blocks
is blocked by MDSAL-85 Binding Model API: restrictions do no... Confirmed

 Description   

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

Generated at Wed Feb 07 20:10:47 UTC 2024 using Jira 8.20.10#820010-sha1:ace47f9899e9ee25d7157d59aa17ab06aee30d3d.