Interface Provider<T>

Type Parameters:
T - The service interface created by this provider.
All Known Subinterfaces:
DocumentAnnotatorProvider, TextEmbedderProvider

public interface Provider<T>
Creates instances of one service interface from a ProviderSpec.

Every service interface has an SPI of its own that extends this interface, for example DocumentAnnotatorProvider extends Provider<DocumentAnnotator>. Implementations are listed in META-INF/services/<SPI fully qualified name>, need a public no-argument constructor and are resolved through Providers.

Implementations must be stateless and thread-safe. The constructor, the static initializer of the implementation class, isAvailable() and supports(ProviderSpec) must not load models or initialize native libraries: they run while providers are looked up, before a caller has chosen one.

Since:
3.0.0
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates a new instance.
    default boolean
    Checks whether this provider can be used in the running process, for example whether an optional dependency is on the class path.
     
    default int
     
    boolean
    Checks whether this provider can create an instance for a spec, based on the spec alone.
  • Method Details

    • name

      String name()
      Returns:
      The case-sensitive name of this provider: at most 64 characters of ASCII letters, digits, ., _ or -, since it is used as a configuration value. A provider with another name is skipped when providers are looked up. Providers of one SPI may share a name; priority() ranks them.
    • priority

      default int priority()
      Returns:
      The rank of this provider among providers that support the same spec, larger wins. The default is 0.
    • isAvailable

      default boolean isAvailable()
      Checks whether this provider can be used in the running process, for example whether an optional dependency is on the class path. Check a class by class literal and catch LinkageError instead of loading it by name, which also keeps a native image free of reflection metadata. The check runs on every resolution, so it must not block.
      Returns:
      true if this provider can be used. The default is true.
    • supports

      boolean supports(ProviderSpec spec)
      Checks whether this provider can create an instance for a spec, based on the spec alone. The check runs on every resolution, so it must not block.
      Parameters:
      spec - The spec to check. Must not be null.
      Returns:
      true if create(ProviderSpec) accepts spec.
      Throws:
      IllegalArgumentException - Thrown if spec is null.
    • create

      T create(ProviderSpec spec) throws IOException
      Creates a new instance. The caller owns it and closes it if it is closeable. The location of the spec is unvalidated input of the caller: a provider that opens it decides itself which schemes and which directories it accepts.
      Parameters:
      spec - The spec to create an instance for. Must not be null.
      Returns:
      The new instance. Never null.
      Throws:
      IllegalArgumentException - Thrown if spec is null or not supported.
      IOException - Thrown if a resource named by spec could not be read.