/** * Concatenates the specified text to the end of this text. This method is very fast (faster even * than <code>StringBuffer.append(String)</code>) and still returns a text instance with an * internal binary tree of minimal depth! * * @param that the text that is concatenated. * @return <code>this + that</code> */ public ImmutableText concat(ImmutableText that) { if (that.length() == 0) { return this; } // All Text instances are maintained balanced: // (head < tail * 2) & (tail < head * 2) final int length = this._count + that._count; if (length <= BLOCK_SIZE) { // Merges to primitive. char[] chars = new char[length]; this.getChars(0, this._count, chars, 0); that.getChars(0, that._count, chars, this._count); return new ImmutableText(chars); } else { // Returns a composite. ImmutableText head = this; ImmutableText tail = that; if (((head._count << 1) < tail._count) && (tail._data == null)) { // tail is composite // head too small, returns (head + tail/2) + (tail/2) if (tail._head._count > tail._tail._count) { // Rotates to concatenate with smaller part. tail = tail.rightRotation(); } head = head.concat(tail._head); tail = tail._tail; } else if (((tail._count << 1) < head._count) && (head._data == null)) { // head is composite. // tail too small, returns (head/2) + (head/2 concat tail) if (head._tail._count > head._head._count) { // Rotates to concatenate with smaller part. head = head.leftRotation(); } tail = head._tail.concat(tail); head = head._head; } return new ImmutableText(head, tail); } }