Span (Apache OpenNLP Tools 2.5.9 API)
Description: declaration: package: opennlp.tools.util, class: Span

# Class Span

java.lang.Object
opennlp.tools.util.Span

All Implemented Interfaces:
  `Serializable, Comparable<Span>`

Direct Known Subclasses:
  `LinkedSpan, MascSentence, MascToken, MascWord`

public class Span extends Object implements Comparable<Span>, Serializable
Class for storing start and end integer offsets.

See Also:
  - Serialized Form

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `Span(int s, int e)`
  Initializes a new `Span`.
  `Span(int s, int e, double prob)`
  Initializes a new `Span`.
  `Span(int s, int e, String type)`
  Initializes a new `Span`.
  `Span(int s, int e, String type, double prob)`
  Initializes a new `Span`.
  `Span(Span span, double prob)`
  Creates a new immutable `Span` based on an existing `Span`, where the existing `Span` did not include the probability.
  `Span(Span span, int offset)`
  Initializes a new `Span` with an existing `Span` which is shifted by an offset.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `int`
  `compareTo(Span s)`
  Compares the specified `Span` to the current span.
  `boolean`
  `contains(int index)`
  An index with the value of end is considered outside the `Span`.
  `boolean`
  `contains(Span s)`
  Identical `spans` are considered to contain each other.
  `boolean`
  `crosses(Span s)`
  Checks if the specified `Span` crosses this span.
  `boolean`
  `equals(Object o)`
  `CharSequence`
  `getCoveredText(CharSequence text)`
  `int`
  `getEnd()`
  Note: that the returned index is one past the actual end of the span in the text, or the first element past the end of the span.
  `double`
  `getProb()`
  `int`
  `getStart()`
  `String`
  `getType()`
  `int`
  `hashCode()`
  `boolean`
  `intersects(Span s)`
  Checks if the specified `Span` intersects with this span.
  `int`
  `length()`
  `static String[]`
  `spansToStrings(Span[] spans, CharSequence s)`
  Converts an array of `spans` to an array of `String`.
  `static String[]`
  `spansToStrings(Span[] spans, String[] tokens)`
  `boolean`
  `startsWith(Span s)`
  `String`
  `toString()`
  `Span`
  `trim(CharSequence text)`

  ### Methods inherited from class Object

  `getClass, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### Span

    public Span(int s, int e, String type)
    Initializes a new `Span`. Sets the prob to `0` as default.
    Parameters:
      `s` - The start position of a `Span`. Must be equal to or greater than `0`. Must not be greater than `e`.
      `e` - The end position of a `Span`, which is `+1` more than the last element in the span. Must be equal to or greater than `0`.
      `type` - the type of the span
    Throws:
      `IllegalArgumentException` - Thrown if given parameters are invalid.

  - ### Span

    public Span(int s, int e, String type, double prob)
    Initializes a new `Span`.
    Parameters:
      `s` - The start position of a `Span`. Must be equal to or greater than `0`. Must not be greater than `e`.
      `e` - The end position of a `Span`, which is `+1` more than the last element in the span. Must be equal to or greater than `0`.
      `type` - The type of the `Span`
      `prob` - The probability of the `Span`.
    Throws:
      `IllegalArgumentException` - Thrown if given parameters are invalid.

  - ### Span

    public Span(int s, int e)
    Initializes a new `Span`. Sets the prob to `0` as default.
    Parameters:
      `s` - The start position of a `Span`. Must be equal to or greater than `0`. Must not be greater than `e`.
      `e` - The end position of a `Span`, which is `+1` more than the last element in the span. Must be equal to or greater than `0`.
    Throws:
      `IllegalArgumentException` - Thrown if given parameters are invalid.

  - ### Span

    public Span(int s, int e, double prob)
    Initializes a new `Span`. Sets the prob to `0` as default.
    Parameters:
      `s` - The start position of a `Span`. Must be equal to or greater than `0`. Must not be greater than `e`.
      `e` - The end position of a `Span`, which is `+1` more than the last element in the span. Must be equal to or greater than `0`.
      `prob` - The probability of the `Span`
    Throws:
      `IllegalArgumentException` - Thrown if given parameters are invalid.

  - ### Span

    public Span(Span span, int offset)
    Initializes a new `Span` with an existing `Span` which is shifted by an offset.
    Parameters:
      `span` - The existing `Span`.
      `offset` - The positive or negative shift offset.
    Throws:
      `IllegalArgumentException` - Thrown if given parameters are invalid.

  - ### Span

    public Span(Span span, double prob)
    Creates a new immutable `Span` based on an existing `Span`, where the existing `Span` did not include the probability.
    Parameters:
      `span` - The `Span` that has no prob or the prob is incorrect and a new `Span` must be generated.
      `prob` - The probability of the `Span`.
    Throws:
      `IllegalArgumentException` - Thrown if given parameters are invalid.

- ## Method Details

  - ### getStart

    public int getStart()
    Returns:
      Retrieves the start of a `Span`. Guaranteed to be greater than `0`.

  - ### getEnd

    public int getEnd()
    Note: that the returned index is one past the actual end of the span in the text, or the first element past the end of the span.
    Returns:
      Retrieves the end of a `Span`. Guaranteed to be greater than `0`.

  - ### getType

    public String getType()
    Returns:
      Retrieves the type of a `Span` or `null` if not set.

  - ### length

    public int length()
    Returns:
      Returns the length of a `Span`. Guaranteed to be greater than `0`.

  - ### contains

    public boolean contains(Span s)
    Identical `spans` are considered to contain each other.
    Parameters:
      `s` - The `Span` to compare with this `Span`.
    Returns:
      `true` is the specified {`Span` s} is contained by this span, `false` otherwise.

  - ### contains

    public boolean contains(int index)
    An index with the value of end is considered outside the `Span`.
    Parameters:
      `index` - the index to test with this `Span`.
    Returns:
      `true` if the span contains this specified index, `false` otherwise.

  - ### startsWith

    public boolean startsWith(Span s)
    Parameters:
      `s` - The `Span` to compare with this span.
    Returns:
      `true` if the specified span starts with this span and is contained in this span, `false` otherwise

  - ### intersects

    public boolean intersects(Span s)
    Checks if the specified `Span` intersects with this span.
    Parameters:
      `s` - The `Span` to compare with this span.
    Returns:
      `true` is the spans overlap, `false` otherwise.

  - ### crosses

    public boolean crosses(Span s)
    Checks if the specified `Span` crosses this span.
    Parameters:
      `s` - The `Span` to compare with this span.
    Returns:
      `true` is the given `Span` overlaps this span and contains a non-overlapping section, `false` otherwise.

  - ### getCoveredText

    public CharSequence getCoveredText(CharSequence text)
    Parameters:
      `text` - The `text` to analyze.
    Returns:
      Retrieves the (sub)string covered by the current `Span` of the specified text.
    Throws:
      `IllegalArgumentException` - Thrown if parameters violated a constraint.

  - ### trim

    public Span trim(CharSequence text)
    Parameters:
      `text` - The `text` to analyze.
    Returns:
      A copy of this `Span` with leading and trailing white spaces removed, or the same object if already trimmed.

  - ### compareTo

    public int compareTo(Span s)
    Compares the specified `Span` to the current span.
    Specified by:
      `compareTo` in interface `Comparable<Span>`
    Parameters:
      `s` - The `Span` instance to compare against.
    See Also:
      - `Comparable.compareTo(Object)`

  - ### hashCode

    public int hashCode()
    Overrides:
      `hashCode` in class `Object`

  - ### equals

    public boolean equals(Object o)
    Overrides:
      `equals` in class `Object`

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`
    Returns:
      A human-readable representation of this `Span`.

  - ### spansToStrings

    public static String[] spansToStrings(Span[] spans, CharSequence s)
    Converts an array of `spans` to an array of `String`.
    Parameters:
      `spans` - The array used as input.
      `s` - The `CharSequence` used to compute covered text.
    Returns:
      The converted array of strings.

  - ### spansToStrings

    public static String[] spansToStrings(Span[] spans, String[] tokens)

  - ### getProb

    public double getProb()
    Returns:
      Retrieves the probability represented by a `Span`.