Class MappedText

java.lang.Object
opennlp.tools.tokenize.MappedText

final class MappedText extends Object
The normalized text with the original-text range for each character. Characters inserted by the pipeline (isolation spaces) use an empty range at the insertion point.

The buffers chars, starts and ends are exposed for fast, allocation-free reads; only the first length entries of each are valid. They grow on demand, so the capacity given to the constructor is a sizing hint, not a limit.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) char[]
     
    (package private) int[]
     
    (package private) int
     
    private static final int
    The largest capacity the buffers grow to.
    private static final int
    The smallest capacity the buffers grow to from empty.
    (package private) int[]
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    MappedText(int capacity)
    Creates an empty mapping.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) void
    add(char c, int originalStart, int originalEnd)
    Appends one UTF-16 code unit with its source range.
    (package private) void
    add(String s, int originalStart, int originalEnd)
    Appends every UTF-16 code unit of a string, each with the same source range.
    (package private) void
    addCodePoint(int codePoint, int originalStart, int originalEnd)
    Appends one code point, as one or two UTF-16 code units that share the same source range.
    (package private) int
    Returns the number of code units the buffers hold before they have to grow.
    (package private) int
    codePointAt(int index)
    Returns the code point at an index of the mapped UTF-16 buffer.
    private void
    Doubles the buffers, starting at 16 and capped at MAX_CAPACITY.
    (package private) String
    Returns the mapped character content, the first length code units.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_CAPACITY

      private static final int MAX_CAPACITY
      The largest capacity the buffers grow to. Some JVMs reserve header words in arrays, so allocating close to Integer.MAX_VALUE elements can fail even with enough memory.
      See Also:
    • MIN_GROWTH

      private static final int MIN_GROWTH
      The smallest capacity the buffers grow to from empty.
      See Also:
    • chars

      char[] chars
    • starts

      int[] starts
    • ends

      int[] ends
    • length

      int length
  • Constructor Details

    • MappedText

      MappedText(int capacity)
      Creates an empty mapping.
      Parameters:
      capacity - The expected number of UTF-16 code units. Must not be negative. Zero is allowed and allocates nothing up front; the buffers grow on the first add. Since it is only a sizing hint, a value above MAX_CAPACITY is clamped to it.
      Throws:
      IllegalArgumentException - Thrown if capacity is negative.
  • Method Details

    • capacity

      int capacity()
      Returns the number of code units the buffers hold before they have to grow.
      Returns:
      the number of code units the buffers hold before they have to grow
    • add

      void add(char c, int originalStart, int originalEnd)
      Appends one UTF-16 code unit with its source range. The range is not validated: the pipeline passes ranges of the original text, and an empty range (originalStart == originalEnd) marks an inserted character.
      Parameters:
      c - The code unit to append.
      originalStart - The start of the source range in the original text.
      originalEnd - The exclusive end of the source range in the original text.
      Throws:
      IllegalStateException - Thrown if the mapping already holds the maximum number of code units an array can hold.
    • add

      void add(String s, int originalStart, int originalEnd)
      Appends every UTF-16 code unit of a string, each with the same source range.
      Parameters:
      s - The string to append. Must not be null.
      originalStart - The start of the source range in the original text.
      originalEnd - The exclusive end of the source range in the original text.
      See Also:
    • addCodePoint

      void addCodePoint(int codePoint, int originalStart, int originalEnd)
      Appends one code point, as one or two UTF-16 code units that share the same source range.
      Parameters:
      codePoint - A valid Unicode code point.
      originalStart - The start of the source range in the original text.
      originalEnd - The exclusive end of the source range in the original text.
      See Also:
    • codePointAt

      int codePointAt(int index)
      Returns the code point at an index of the mapped UTF-16 buffer. A high surrogate followed by a low surrogate within length forms one supplementary code point; an unpaired surrogate is returned as is.
      Parameters:
      index - The index of a code unit, in [0, length).
      Returns:
      The code point that starts at index.
      Throws:
      IndexOutOfBoundsException - Thrown if index is outside [0, length).
    • text

      String text()
      Returns the mapped character content, the first length code units.
      Returns:
      the mapped character content, the first length code units
    • grow

      private void grow()
      Doubles the buffers, starting at 16 and capped at MAX_CAPACITY.
      Throws:
      IllegalStateException - Thrown if the buffers are already at MAX_CAPACITY.