Class TextStore

java.lang.Object
monq.jfa.textparts.TextStore

public class TextStore extends Object

stores a text and parts of it in a (hopefully) efficient manner. A TextStore maintains a string as well as possibly overlapping substrings thereof which are called parts. The string can be enlarged with append() and shortened by calling setLength(). The parts, defined by start and end index, are created with addPart() and setPart().

Furthermore, a small nonnegative integer may be associated with each part. This is a hack which was introduced so that a TextStore is exactly what is needed to represent a match, its submatches as well as the small identifying integers which can be associated with them.

The parts can be accessed with two getPart methods (getPart(), getPart()), which append the requested part, or a piece thereof to a given StringBuilder.

Note: Many applications define part 0 to refer to the whole stored string, but this is not mandatory.

Part Indexing

All methods which have a part index as parameter accept negative values to address parts relative to the end of the part list. In particular -1 refers to the last part.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addPart(int start, int end)
     
    void
    addPart(int start, int end, byte id)
    like setPart() except that the given part is appended to the list of parts.
    void
    append(char ch)
     
    void
    appends the given text to the this.
    void
    append(StringBuilder s, int start, int end)
    appends the given text to the this.
    void
    appendPart(StringBuilder s, int start, int end)
     
    void
    appendPart(StringBuilder s, int start, int end, byte id)
    combines operations append(java.lang.String) and addPart() such that the whole appended text will also define a new part.
    void
    resets this to contain neither text nor any parts.
    void
    deleteParts(int first, int last)
    deletes all parts in the inclusive range referenced by first and last.
    int
    getEnd(int part)
     
    byte
    getId(int part)
    retrieves the small integer value which can be associated with a part.
    int
    returns the number of currently stored parts.
    getPart(int part)
    returns the requested part.
    getPart(int part, int from, int to)
    returns a substring of the requested part.
    void
    getPart(StringBuilder sb, int part)
    appends the requested part to the given StringBuilder.
    void
    getPart(StringBuilder sb, int part, int from, int to)
    appends a substring of the requested part to the given StringBuilder.
    int
    getPartLen(int part)
    returns the length of the requested part.
    int
    getStart(int part)
     
    int
    returns the size of the currently stored text.
    void
    setLength(int l)
     
    void
    setPart(int part, int start, int end)
    calls the 4 parameter setPart() with id==-1.
    void
    setPart(int part, int start, int end, byte id)
    defines the given part as the stretch of characters in the stored text starting at position start and reaching to the character just before end.
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • TextStore

      public TextStore()
  • Method Details

    • getNumParts

      public int getNumParts()
      returns the number of currently stored parts.
    • length

      public int length()
      returns the size of the currently stored text.
    • clear

      public void clear()

      resets this to contain neither text nor any parts.

    • setLength

      public void setLength(int l)
    • append

      public void append(String s)

      appends the given text to the this. If the added text shall become a part, call setPart() or addPart() afterwards.

    • append

      public void append(char ch)
    • append

      public void append(StringBuilder s, int start, int end)

      appends the given text to the this. If the added text shall become a part, call setPart() or addPart() afterwards.

    • setPart

      public void setPart(int part, int start, int end, byte id)

      defines the given part as the stretch of characters in the stored text starting at position start and reaching to the character just before end.

      Parameters:
      part - see part indexing
      id - is a small id to be assigned to new part
      Throws:
      ArrayIndexOutOfBoundsException - if part is out of range
      IllegalArgumentException - if start and end do not satisfy the relation 0<=start<=end<=textSize().
    • setPart

      public void setPart(int part, int start, int end)

      calls the 4 parameter setPart() with id==-1.

    • deleteParts

      public void deleteParts(int first, int last)

      deletes all parts in the inclusive range referenced by first and last. This does not change the underlying string. Negative part indexes are allowed, but the parts referenced by first and last must be in order.

    • addPart

      public void addPart(int start, int end, byte id)

      like setPart() except that the given part is appended to the list of parts.

    • addPart

      public void addPart(int start, int end)
    • appendPart

      public void appendPart(StringBuilder s, int start, int end, byte id)

      combines operations append(java.lang.String) and addPart() such that the whole appended text will also define a new part.

    • appendPart

      public void appendPart(StringBuilder s, int start, int end)
    • getPart

      public void getPart(StringBuilder sb, int part)

      appends the requested part to the given StringBuilder. If part is out of range, this is silently ignored and sb is not changed.

      Parameters:
      part - see part indexing.
    • getPart

      public String getPart(int part)

      returns the requested part.

      See Also:
    • getId

      public byte getId(int part)

      retrieves the small integer value which can be associated with a part.

      Throws:
      ArrayIndexOutOfBoundsException - if part is out of range
    • getPart

      public void getPart(StringBuilder sb, int part, int from, int to)

      appends a substring of the requested part to the given StringBuilder. The substring is indicated by from and to, where to refers to the character just after the last one requested. Both indices are relative to the part, not the whole internally stored text. Both indices can be negative in order to be taken relative to getPartLen(part). In addition, to==0 is also taken relative to the end of the part.

      If part is out of range, this is silently ignored and sb is not changed.

      If start or end are out of range, they are truncated to the nearest fitting value.

      Examples

      • from=0, to=0 refers to the whole part
      • from=-2, to=-1 returns the 2nd to last character
      Parameters:
      part - see part indexing
    • getPart

      public String getPart(int part, int from, int to)
      returns a substring of the requested part.
      See Also:
    • getPartLen

      public int getPartLen(int part)

      returns the length of the requested part. If part is out of range, 0 is returned.

      Parameters:
      part - see part indexing
    • getStart

      public int getStart(int part)
    • getEnd

      public int getEnd(int part)
    • toString

      public String toString()
      Overrides:
      toString in class Object