Class ImmutableDocument

java.lang.Object
opennlp.tools.document.ImmutableDocument
All Implemented Interfaces:
Document

final class ImmutableDocument extends Object implements Document
The default Document implementation: an unmodifiable map from layer key to an unmodifiable annotation list.

Instances are immutable: with(LayerKey, List) returns a new document that shares the unchanged layers with its ancestor, copying the map but not the layers, so documents grown from a common ancestor share their layer lists. The text is captured as a String at construction, so a mutable CharSequence handed to empty(CharSequence) cannot change the document afterwards. That immutability makes instances safe to share between threads.

  • Field Details

  • Constructor Details

  • Method Details

    • empty

      static ImmutableDocument empty(CharSequence text)
      Creates a document without any layers.
      Parameters:
      text - The original document text, captured as its content at this moment. Must not be null.
      Returns:
      An empty ImmutableDocument. Never null.
      Throws:
      IllegalArgumentException - Thrown if text is null.
    • text

      public CharSequence text()
      Specified by:
      text in interface Document
      Returns:
      The original text of the document. Never null.
    • get

      public <T> List<Annotation<T>> get(LayerKey<T> layer)
      Retrieves the annotations of one layer.
      Specified by:
      get in interface Document
      Type Parameters:
      T - The type of the layer's annotation values.
      Parameters:
      layer - The layer to read. Must not be null.
      Returns:
      The layer's annotations in their layer order, or an empty list when the layer is absent. Never null; the list is unmodifiable.
    • layers

      public Set<LayerKey<?>> layers()
      Specified by:
      layers in interface Document
      Returns:
      The keys of all layers present on the document. Never null; the set is unmodifiable.
    • with

      public <T> Document with(LayerKey<T> layer, List<Annotation<T>> annotations)
      Returns a new document with one layer added.
      Specified by:
      with in interface Document
      Type Parameters:
      T - The type of the layer's annotation values.
      Parameters:
      layer - The key of the layer to add. Must not be null and must not already be present.
      annotations - The annotations of the layer. Must not be null, must not contain null, and every value must be assignable to the layer's type. Under a positional key every annotation must carry a span within the text bounds; under a document-scoped key no annotation may carry a span.
      Returns:
      A new Document sharing this document's text and existing layers. Never null.
    • merge

      public Document merge(Document other, Document.DuplicateLayerPolicy duplicateLayers)
      Returns a document combining this document's layers with another document's layers over the same text, resolving duplicate layer keys with duplicateLayers. This implementation copies the layer map once, not once per added layer.
      Specified by:
      merge in interface Document
      Parameters:
      other - The document whose layers are added on top of this document's layers. Must not be null and must carry the same text content.
      duplicateLayers - How to treat a layer key present on both documents. Must not be null.
      Returns:
      A Document carrying the layers of both documents. This document itself may be returned when other adds no layer. Never null; both source documents are left untouched.
    • copyValidated

      private <T> List<Annotation<?>> copyValidated(LayerKey<T> layer, Document from)
      Returns a validated immutable copy of one of from's layers, capturing the key's value type.
      Returns:
      a validated immutable copy of one of from's layers, capturing the key's value type
    • validate

      private <T> void validate(LayerKey<T> layer, List<Annotation<T>> annotations)
      Checks one layer's annotations against the key's contract: no null elements, values assignable to the key's type, spans present and within the text bounds under a positional key, absent under a document-scoped key.
      Throws:
      IllegalArgumentException - Thrown if any check fails; the message names the layer.