Class PText

java.lang.Object
ai.badmonkey.agentspaces.pdt.values.PText

public final class PText extends Object
A text that merges (PDT 2 phase 6, P2-T-02 and P2-T-03; srad/agentspaces-pdt2.md §3.9): a Fugue sequence of characters with ids, edited by one replica at a time, that stores as a t leaf of runs (TextLeaf). Positions and lengths count Unicode code points, as the runs do.

An insert follows Fugue: the new characters hang as the right child of the character before the position when it has no right children, else as the left child of the next character in Fugue order (deleted ones included), and each further character as the right child of the one before it. A replica numbers its characters from one more than the largest counter it holds, so typing at the cursor extends one run. A delete keeps the characters' ids and drops their text. merge(PText) is the union of two texts, which every replica computes alike, so two replicas converge whatever they did concurrently, and Fugue keeps each insert's characters together.

The value is immutable: every edit returns a new text. A replica is any non-empty text that no other writer of the document uses; replicaOf(AgentId) derives eight base32 characters from an agent's id, so the runs stay small.

As a value, a text is the wire encoding of its leaf's map, {k:"t", r:[run...], v:1} with the map indefinite as every embedded item is (rule S8), and codec(String) writes and reads that form strictly. A map value of text above 1 KiB spills to a t node that holds the canonical node bytes of the same leaf (P2-T-02), two texts that meet in a merge take their union (P2-T-03), and a head whose value holds a text is PRef.v 2 (§6.10.3, rule 3).

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    The codec codec(String) returns; a ref whose values pass through it writes PRef.v 2 heads (§6.10.3, rule 3).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
    The characters this release lets one text hold, live and deleted: the leaf bound.
    static final int
    The length of a replica id replicaOf(AgentId) derives.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Codec<PText>
    codec(String replica)
    The codec of texts edited by a replica: a text encodes as the wire encoding of its leaf's map and decodes from exactly that form.
    delete(long pos, long len)
    The text with characters deleted.
    static PText
    empty(String replica)
    The empty text, edited by a replica.
    boolean
    Equality by the leaf: the same characters, ids, and deletions, whatever the replica.
    int
     
    insert(long pos, String text)
    The text with characters inserted before a position.
    The canonical leaf of the characters, maximal runs in id order.
    long
    The live characters, in code points.
    merge(PText other)
    The union of two texts (P2-T-03): every character of either, deleted when either deleted it, in Fugue order, edited by this text's replica.
    static PText
    of(TextLeaf leaf, String replica)
    The text of a leaf, edited by a replica.
    Every character id in Fugue order, deleted ones included.
    The replica that edits this text.
    static String
    The replica id of an agent (P2-T-02): the first five bytes of the SHA-256 of its encoded id, as eight lower-case base32 characters.
    The live text.
    This text edited by another replica.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • MAX_CHARS

      public static final long MAX_CHARS
      The characters this release lets one text hold, live and deleted: the leaf bound.
      See Also:
    • REPLICA_LENGTH

      public static final int REPLICA_LENGTH
      The length of a replica id replicaOf(AgentId) derives.
      See Also:
  • Method Details

    • empty

      public static PText empty(String replica)
      The empty text, edited by a replica.
    • of

      public static PText of(TextLeaf leaf, String replica)
      The text of a leaf, edited by a replica.
    • replicaOf

      public static String replicaOf(AgentId agent)
      The replica id of an agent (P2-T-02): the first five bytes of the SHA-256 of its encoded id, as eight lower-case base32 characters.
    • codec

      public static Codec<PText> codec(String replica)
      The codec of texts edited by a replica: a text encodes as the wire encoding of its leaf's map and decodes from exactly that form.
    • replica

      public String replica()
      The replica that edits this text.
    • length

      public long length()
      The live characters, in code points.
    • toString

      public String toString()
      The live text.
      Overrides:
      toString in class Object
    • order

      public List<TextRun.CharId> order()
      Every character id in Fugue order, deleted ones included.
    • leaf

      public TextLeaf leaf()
      The canonical leaf of the characters, maximal runs in id order.
    • withReplica

      public PText withReplica(String other)
      This text edited by another replica.
    • insert

      public PText insert(long pos, String text)
      The text with characters inserted before a position.
      Parameters:
      pos - 0 to length()
      text - the characters, without a lone surrogate
      Returns:
      the text, or this one when text is empty
      Throws:
      IllegalArgumentException - when the text would hold more than MAX_CHARS characters
    • delete

      public PText delete(long pos, long len)
      The text with characters deleted.
      Parameters:
      pos - the first deleted position
      len - how many live characters to delete
      Returns:
      the text, or this one when len is 0
    • merge

      public PText merge(PText other)
      The union of two texts (P2-T-03): every character of either, deleted when either deleted it, in Fugue order, edited by this text's replica.
      Throws:
      IllegalArgumentException - when the two texts give one character id two parents, sides, or characters
    • equals

      public boolean equals(Object o)
      Equality by the leaf: the same characters, ids, and deletions, whatever the replica.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object