Class ProviderSpec

java.lang.Object
opennlp.tools.util.ext.ProviderSpec

public final class ProviderSpec extends Object
Describes what a Provider should create: an optional location, such as a model file, and string options. Instances are immutable.
Since:
3.0.0
See Also:
  • Field Details

  • Constructor Details

    • ProviderSpec

      private ProviderSpec(URI location, Map<String,String> options)
      Parameters:
      location - The location, or null if the spec has none.
      options - The options. Must not be null, an option name must not be null or blank and an option value must not be null.
      Throws:
      IllegalArgumentException - Thrown if options is invalid.
  • Method Details

    • empty

      public static ProviderSpec empty()
      Returns:
      A spec without location and options. Never null.
    • of

      public static ProviderSpec of(Map<String,String> options)
      Creates a spec with options and without location.
      Parameters:
      options - The options. Must not be null, an option name must not be null or blank and an option value must not be null.
      Returns:
      The spec. Never null.
      Throws:
      IllegalArgumentException - Thrown if options is invalid.
    • of

      public static ProviderSpec of(URI location)
      Creates a spec with a location and without options.
      Parameters:
      location - The absolute location. Must not be null.
      Returns:
      The spec. Never null.
      Throws:
      IllegalArgumentException - Thrown if location is null or not absolute.
    • of

      public static ProviderSpec of(URI location, Map<String,String> options)
      Creates a spec with a location and options.
      Parameters:
      location - The absolute location, which is taken as it is, unlike the location of of(Path, Map). Must not be null.
      options - The options. Must not be null, an option name must not be null or blank and an option value must not be null.
      Returns:
      The spec. Never null.
      Throws:
      IllegalArgumentException - Thrown if location is null or not absolute, or if options is invalid.
    • of

      public static ProviderSpec of(Path location)
      Creates a spec for a file or directory without options, see of(Path, Map).
      Parameters:
      location - The file or directory. Must not be null.
      Returns:
      The spec. Never null.
      Throws:
      IllegalArgumentException - Thrown if location is null.
    • of

      public static ProviderSpec of(Path location, Map<String,String> options)
      Creates a spec for a file or directory with options. A relative path is resolved against the working directory of the running process when the spec is created, so a spec that is kept in a static field must not be created from a relative path. The file system is not read, so the location of a directory carries no trailing separator and a path holding .. is kept as it is.
      Parameters:
      location - The file or directory. Must not be null.
      options - The options. Must not be null, an option name must not be null or blank and an option value must not be null.
      Returns:
      The spec. Never null.
      Throws:
      IllegalArgumentException - Thrown if location is null, or if options is invalid.
    • uriOf

      private static URI uriOf(Path location)
      Converts a path of the default file system without reading it, unlike Path.toUri(), which appends a separator to an existing directory and would make the location depend on what is on disk when the spec is created. The path is made absolute but neither normalized nor resolved against symbolic links.
      Parameters:
      location - The file or directory. Must not be null.
      Returns:
      The location of location. Never null.
      Throws:
      IllegalArgumentException - Thrown if location has no URI.
    • location

      public Optional<URI> location()
      Returns:
      The location, or Optional.empty() if this spec has none.
    • path

      public Optional<Path> path()
      Returns:
      The location as a path, or Optional.empty() if this spec has no location or the location is not a hierarchical file URI of the default file system.
    • options

      public Map<String,String> options()
      Returns:
      The options in insertion order. Unmodifiable and never null.
    • option

      public String option(String key, String defaultValue)
      Reads an option.
      Parameters:
      key - The option name. Must not be null.
      defaultValue - The value to return if the option is absent. May be null.
      Returns:
      The option value, or defaultValue.
      Throws:
      IllegalArgumentException - Thrown if key is null.
    • hasOnlyOptions

      public boolean hasOnlyOptions(String... names)
      Checks whether every option of this spec has one of the given names.
      Parameters:
      names - The accepted option names. Must not be null or contain a null element.
      Returns:
      true if this spec has no option with another name.
      Throws:
      IllegalArgumentException - Thrown if names is null or contains a null element.
    • locationEndsWith

      public boolean locationEndsWith(String suffix)
      Checks whether the last path segment of the location ends with a suffix, ignoring case. A query and a fragment are not part of the comparison. For an opaque URI, such as jar:file:/models.jar!/model.onnx, the scheme-specific part is used as path. This is a convenience for routing a request to a provider, not a validation of the location.
      Parameters:
      suffix - The suffix, for example .onnx. Must not be null or empty.
      Returns:
      true if this spec has a location and its last path segment ends with suffix.
      Throws:
      IllegalArgumentException - Thrown if suffix is null or empty.
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      The option values and the user info, query and fragment of the location are omitted, as they may hold credentials.
      Overrides:
      toString in class Object
    • contains

      private boolean contains(String[] names, String key)
    • printableLocation

      private String printableLocation()
    • printable

      static String printable(URI location)
      Renders a location without the parts that may hold credentials, for a message or a log entry.
      Parameters:
      location - The location. Must not be null.
      Returns:
      The location without its user info, query and fragment. Never null.
    • redact

      private static String redact(String part)
      Returns:
      The scheme-specific part of an opaque location, such as https://user@host/models.jar!/model.onnx, without the user info of a nested authority.
    • withoutUserInfo

      private static String withoutUserInfo(String authority)
    • hasControlCharacter

      private boolean hasControlCharacter(String path)
    • decode

      private String decode(String part)
      Decodes the percent escapes of a raw location, unlike URLDecoder, which would also read + as a space. A run of escapes is decoded as UTF-8, and the characters between them, which a raw location holds unescaped if they are not ASCII, are kept as they are.
      Returns:
      The decoded part.
    • appendEscapes

      private void appendEscapes(StringBuilder decoded, ByteArrayOutputStream escapes)
    • escapedByte

      private int escapedByte(String part, int character)
    • cutQuery

      private static String cutQuery(String part)