Class BidiAlgorithm

java.lang.Object
com.mta.tehreer.unicode.BidiAlgorithm
All Implemented Interfaces:
Disposable

public class BidiAlgorithm extends Object implements Disposable
This class implements Unicode Bidirectional Algorithm available at http://www.unicode.org/reports/tr9.

A BidiAlgorithm object provides information related to individual paragraphs in source text by applying rule P1. It can be used to create paragraph objects by explicitly specifying the paragraph level or deriving it from rules P2 and P3. Once a paragraph object is created, embedding levels of characters can be queried from it.

  • Field Details

    • MAX_LEVEL

      public static final byte MAX_LEVEL
      Maximum explicit embedding level.
      See Also:
  • Constructor Details

    • BidiAlgorithm

      public BidiAlgorithm(@NonNull String text)
      Constructs a bidi algorithm object for the given text.
      Parameters:
      text - The text to apply unicode bidirectional algorithm on.
      Throws:
      IllegalArgumentException - if text is empty.
  • Method Details

    • finalizable

      @NonNull public static BidiAlgorithm finalizable(@NonNull BidiAlgorithm bidiAlgorithm)
      Wraps a bidi algorithm object into a finalizable instance which is guaranteed to be disposed automatically by the GC when no longer in use. After calling this method, dispose() should not be called on either original object or returned object. Calling dispose() on returned object will throw an UnsupportedOperationException.

      Note: The behavior is undefined if the passed-in object is already disposed or wrapped into another finalizable instance.

      Parameters:
      bidiAlgorithm - The bidi algorithm object to wrap into a finalizable instance.
      Returns:
      The finalizable instance of the passed-in bidi algorithm object.
    • isFinalizable

      public static boolean isFinalizable(@NonNull BidiAlgorithm bidiAlgorithm)
      Checks whether a bidi algorithm object is finalizable or not.
      Parameters:
      bidiAlgorithm - The bidi algorithm object to check.
      Returns:
      true if the passed-in bidi algorithm object is finalizable, false otherwise.
    • getCharBidiClasses

      @NonNull public IntList getCharBidiClasses()
      Returns a list containing the bidi classes of all characters in source text. The valid bidi class values are available in BidiClass as static constants.
      Returns:
      A list containing the bidi classes of all characters in source text.
    • getParagraphBoundary

      public int getParagraphBoundary(int charStart, int charEnd)
      Returns the boundary of the first paragraph within the given range.

      The boundary of the paragraph occurs after a character whose bidirectional type is Paragraph Separator (B), or the charEnd if no such character exists before it. The exception to this rule is when a Carriage Return (CR) is followed by a Line Feed (LF). Both CR and LF are paragraph separators, but in that case, the boundary of the paragraph is considered after LF character.

      Parameters:
      charStart - The index to the first character of the paragraph in source text.
      charEnd - The suggested index after the last character of the paragraph in source text.
      Returns:
      The boundary of the first paragraph within the given range.
      Throws:
      IllegalArgumentException - if charStart is negative, or charEnd is greater than the length of source text, or charStart is greater than or equal to charEnd.
    • createParagraph

      @NonNull public BidiParagraph createParagraph(int charStart, int charEnd, @NonNull BaseDirection baseDirection)
      Creates a paragraph object processed with Unicode Bidirectional Algorithm.

      This method processes only first paragraph starting at charStart and ending at either charEnd or some character before it, in accordance with Rule P1 of Unicode Bidirectional Algorithm.

      The paragraph level is determined by applying Rules P2-P3 and embedding levels are resolved by applying Rules X1-I2.

      Parameters:
      charStart - The index to the first character of the paragraph in source text.
      charEnd - The suggested index after the last character of the paragraph in source text.
      baseDirection - The base direction of the paragraph.
      Returns:
      A paragraph object processed with Unicode Bidirectional Algorithm.
      Throws:
      IllegalArgumentException - if charStart is negative, or charEnd is greater than the length of source text, or charStart is greater than or equal to charEnd.
    • createParagraph

      @NonNull public BidiParagraph createParagraph(int charStart, int charEnd, byte baseLevel)
      Creates a paragraph object processed with Unicode Bidirectional Algorithm.

      This method processes only first paragraph starting at charStart and ending at either charEnd or some character before it, in accordance with Rule P1 of Unicode Bidirectional Algorithm.

      The paragraph level is overridden by baseLevel parameter and embedding levels are resolved by applying Rules X1-I2.

      Parameters:
      charStart - The index to the first character of the paragraph in source text.
      charEnd - The suggested index after the last character of the paragraph in source text.
      baseLevel - Base level to override.
      Returns:
      A paragraph object processed with Unicode Bidirectional Algorithm.
      Throws:
      IllegalArgumentException - if any of the following is true:
      • charStart is negative
      • charEnd is greater than the length of source text
      • charStart is greater than or equal to charEnd
      • baseLevel is less than zero
      • baseLevel is greater than MAX_LEVEL
    • dispose

      public void dispose()
      Description copied from interface: Disposable
      Releases the native memory of this object. Failing to call this method will cause memory leaks.

      Note: The behavior is undefined if this method is called more than once on the same object.

      Specified by:
      dispose in interface Disposable
    • toString

      @NonNull public String toString()
      Overrides:
      toString in class Object