Package org.apache.groovy.contracts
Class VariantSupport
java.lang.Object
org.apache.groovy.contracts.VariantSupport
Shared runtime support for
Decreases termination
measures, used by both the loop variant (@Decreases on a loop) and the
recursion variant (@Decreases on a method, via MethodVariantSupport).
It is the single source of truth for what "strictly decreases and stays
well-founded" means — a scalar Comparable (with a non-negative floor for
Numbers) or a List compared lexicographically — and for whether
the check runs at all under the -ea/-da configuration.
The comparison itself is violation-agnostic (describeFailure(java.lang.Object, java.lang.Object) returns a
description rather than throwing) so each caller can raise the appropriate
violation type.
- Since:
- 6.0.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckLoopVariant(Object prev, Object curr, String className) Loop-variant entry point invoked from generated loop-body code: if enabled, verifycurrstrictly decreased fromprevand stayed well-founded, throwingLoopVariantViolationotherwise.static StringdescribeFailure(Object prev, Object curr) Describe howcurrfails to be a valid strict, well-founded decrease fromprev, ornullif it is valid.static booleanWhether termination-measure checks are enabled forclassNameunder the current-ea/-daconfiguration (anullclass name uses the global default).
-
Method Details
-
enabled
Whether termination-measure checks are enabled forclassNameunder the current-ea/-daconfiguration (anullclass name uses the global default). Mirrors how@Requires/@Ensuresgate.- Parameters:
className- the enclosing class name, ornullfor the global default- Returns:
trueif the check should run
-
describeFailure
Describe howcurrfails to be a valid strict, well-founded decrease fromprev, ornullif it is valid.Listmeasures are compared lexicographically.- Parameters:
prev- the measure beforecurr- the measure after- Returns:
- a failure description suffix, or
nullif the decrease is valid
-
checkLoopVariant
Loop-variant entry point invoked from generated loop-body code: if enabled, verifycurrstrictly decreased fromprevand stayed well-founded, throwingLoopVariantViolationotherwise.- Parameters:
prev- the variant before the iterationcurr- the variant after the iterationclassName- the enclosing class name (for-ea/-dagating)
-