Package org.apache.groovy.contracts

# Class ViolationTracker

java.lang.Object
org.apache.groovy.contracts.ViolationTracker

public class ViolationTracker extends Object

A violation tracker is used to keep a list of pre-, post-condition or class-invariant violations in chronological order. This is necessary to evaluate all parts of a pre- or postcondition, and still being able to rethrow assertion errors.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final ThreadLocal<ViolationTracker>`
  `INSTANCE`
  Stores the tracker bound to the current thread while a contract is being evaluated.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ViolationTracker()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static void`
  `deinit()`
  Removes the tracker associated with the current thread.
  `AssertionViolation`
  `first()`
  Returns the earliest recorded violation.
  `boolean`
  `hasViolations()`
  Indicates whether this tracker currently contains any violations.
  `static void`
  `init()`
  Installs a fresh tracker for the current thread.
  `AssertionViolation`
  `last()`
  Returns the most recently recorded violation.
  `static void`
  `rethrowFirst()`
  Rethrows the earliest recorded violation for the current thread.
  `static void`
  `rethrowLast()`
  Rethrows the most recently recorded violation for the current thread.
  `void`
  `track(AssertionViolation assertionViolation)`
  Records a newly observed assertion violation in encounter order.
  `static boolean`
  `violationsOccurred()`
  Indicates whether the current thread has recorded at least one violation.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### INSTANCE

    public static final ThreadLocal<ViolationTracker> INSTANCE
    Stores the tracker bound to the current thread while a contract is being evaluated.

- ## Constructor Details

  - ### ViolationTracker

    public ViolationTracker()

- ## Method Details

  - ### init

    @GroovyABI(since="4.0.0") public static void init()
    Installs a fresh tracker for the current thread.

  - ### deinit

    @GroovyABI(since="4.0.0") public static void deinit()
    Removes the tracker associated with the current thread.

  - ### violationsOccurred

    @GroovyABI(since="4.0.0") public static boolean violationsOccurred()
    Indicates whether the current thread has recorded at least one violation.
    Returns:
      `true` if a violation has been tracked for the current thread

  - ### rethrowFirst

    @GroovyABI(since="4.0.0") public static void rethrowFirst()
    Rethrows the earliest recorded violation for the current thread.

  - ### rethrowLast

    @GroovyABI(since="4.0.0") public static void rethrowLast()
    Rethrows the most recently recorded violation for the current thread.

  - ### track

    @GroovyABI(since="4.0.0") public void track(AssertionViolation assertionViolation)
    Records a newly observed assertion violation in encounter order.
    Parameters:
      `assertionViolation` - the violation to store

  - ### hasViolations

    @GroovyABI(since="4.0.0") public boolean hasViolations()
    Indicates whether this tracker currently contains any violations.
    Returns:
      `true` if at least one violation has been recorded

  - ### first

    @GroovyABI(since="4.0.0") public AssertionViolation first()
    Returns the earliest recorded violation.
    Returns:
      the first violation in encounter order

  - ### last

    @GroovyABI(since="4.0.0") public AssertionViolation last()
    Returns the most recently recorded violation.
    Returns:
      the last violation in encounter order