Class ValidationConfig

java.lang.Object
dev.metaschema.core.model.constraint.ValidationConfig
All Implemented Interfaces:
AutoCloseable

public final class ValidationConfig extends Object implements AutoCloseable
Configuration for constraint validation.

This class supports parallel execution and optional event-based instrumentation. Two execution modes are available:

An optional ValidationEventListener can be configured via withListener(ValidationEventListener) to receive callbacks during validation. By default, a NoOpValidationEventListener is used, ensuring zero overhead when instrumentation is not needed.

Instances should be used with try-with-resources or explicitly closed after validation.

  • Field Details

    • SEQUENTIAL

      @NonNull public static final ValidationConfig SEQUENTIAL
      Single-threaded sequential execution (default, current behavior).

      This instance does not need to be closed.

  • Method Details

    • withExecutor

      @NonNull @Owning public static @Owning ValidationConfig withExecutor(@NonNull ExecutorService executor)
      Create configuration using an application-provided executor.

      The executor is NOT shut down by close(); the caller retains ownership.

      The caller owns the returned configuration and is responsible for closing it.

      Parameters:
      executor - the executor service to use for parallel tasks
      Returns:
      configuration using the provided executor
      Throws:
      NullPointerException - if executor is null
    • withThreads

      @NonNull @Owning public static @Owning ValidationConfig withThreads(int threadCount)
      Create configuration that creates an internal thread pool.

      The internal pool is shut down when close() is called.

      The caller owns the returned configuration and is responsible for closing it.

      Parameters:
      threadCount - number of threads (must be >= 1)
      Returns:
      configuration with internal thread pool
      Throws:
      IllegalArgumentException - if threadCount < 1
    • withListener

      @NonNull @Owning public @Owning ValidationConfig withListener(@NonNull ValidationEventListener listener)
      Create a new configuration with the specified event listener, preserving the current parallel execution settings.

      If this config owns its executor, the derived config will lazily create its own independent pool to avoid unsafe sharing of owned executors.

      Parameters:
      listener - the event listener to use for validation instrumentation
      Returns:
      a new configuration with the given listener
      Throws:
      NullPointerException - if listener is null
    • addListener

      @NonNull @Owning public @Owning ValidationConfig addListener(@NonNull ValidationEventListener additionalListener)
      Create a new configuration that adds an additional event listener, preserving the current parallel execution settings and any existing listener.

      If the current listener is a NoOpValidationEventListener, the new listener replaces it directly. Otherwise, a CompositeValidationEventListener is created to deliver events to both the existing and new listeners.

      Parameters:
      additionalListener - the additional event listener to add
      Returns:
      a new configuration with the additional listener
      Throws:
      NullPointerException - if additionalListener is null
    • isParallel

      public boolean isParallel()
      Check if parallel execution is enabled.
      Returns:
      true if using more than one thread
    • getListener

      @NonNull public ValidationEventListener getListener()
      Get the configured validation event listener.
      Returns:
      the event listener, never null
    • getExecutor

      @NonNull public ExecutorService getExecutor()
      Get the executor service, creating an internal pool if needed.

      For internal pools, the executor is created lazily on first call.

      Returns:
      the executor service
      Throws:
      IllegalStateException - if called on SEQUENTIAL config
    • close

      public void close()
      Shut down internal executor if one was created.

      Does nothing if using an external executor or if no executor was created.

      Specified by:
      close in interface AutoCloseable