Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Javolution - Java(TM) Solution for Real-Time and Embedded Systems
* Copyright (C) 2012 - Javolution (http://javolution.org/)
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software is
* freely granted, provided that this notice is preserved.
*/
package javolution.text;
import java.io.Serializable;
import javolution.lang.MathLib;
/**
*
An {@link Appendable} text whose capacity expands
* gently without incurring expensive resize/copy operations ever.
*
*
This class is not intended for large documents manipulations which
* should be performed with the {@link Text} class directly
* (O(Log(n)) {@link Text#insert insertion} and
* {@link Text#delete deletion} capabilities).
*
*
The textual format of any appended object is retrieved
* from the current {@link TextContext}.
*
* @author Jean-Marie Dautelle
* @version 5.3, January 20, 2008
*/
public class TextBuilder implements Appendable, CharSequence, Serializable {
private static final long serialVersionUID = 0x600L; // Version.
// We do a full resize (and copy) only when the capacity is less than C1.
// For large collections, multi-dimensional arrays are employed.
private static final int B0 = 5; // Initial capacity in bits.
private static final int C0 = 1 << B0; // Initial capacity (32)
private static final int B1 = 10; // Low array maximum capacity in bits.
private static final int C1 = 1 << B1; // Low array maximum capacity (1024).
private static final int M1 = C1 - 1; // Mask.
// Resizes up to 1024 maximum (32, 64, 128, 256, 512, 1024).
private char[] _low = new char[C0];
// For larger capacity use multi-dimensional array.
private char[][] _high = new char[1][];
/**
* Holds the current length.
*/
private int _length;
/**
* Holds current capacity.
*/
private int _capacity = C0;
/**
* Creates a text builder of small initial capacity.
*/
public TextBuilder() {
_high[0] = _low;
}
/**
* Creates a text builder holding the specified String
* (convenience method).
*
* @param str the initial string content of this text builder.
*/
public TextBuilder(String str) {
this();
append(str);
}
/**
* Creates a text builder of specified initial capacity.
* Unless the text length exceeds the specified capacity, operations
* on this text builder will not allocate memory.
*
* @param capacity the initial capacity.
*/
public TextBuilder(int capacity) {
this();
while (capacity > _capacity) {
increaseCapacity();
}
}
/**
* Returns the length (character count) of this text builder.
*
* @return the number of characters (16-bits Unicode).
*/
public final int length() {
return _length;
}
/**
* Returns the character at the specified index.
*
* @param index the index of the character.
* @return the character at the specified index.
* @throws IndexOutOfBoundsException if
* (index < 0) || (index >= this.length()).
*/
public final char charAt(int index) {
if (index >= _length)
throw new IndexOutOfBoundsException();
return index < C1 ? _low[index] : _high[index >> B1][index & M1];
}
/**
* Copies the character from this text builder into the destination
* character array.
*
* @param srcBegin this text start index.
* @param srcEnd this text end index (not included).
* @param dst the destination array to copy the data into.
* @param dstBegin the offset into the destination array.
* @throws IndexOutOfBoundsException if (srcBegin < 0) ||
* (dstBegin < 0) || (srcBegin > srcEnd) || (srcEnd > this.length())
* || ((dstBegin + srcEnd - srcBegin) > dst.length)
*/
public final void getChars(int srcBegin, int srcEnd, char[] dst,
int dstBegin) {
if ((srcBegin < 0) || (srcBegin > srcEnd) || (srcEnd > this._length))
throw new IndexOutOfBoundsException();
for (int i = srcBegin, j = dstBegin; i < srcEnd;) {
char[] chars0 = _high[i >> B1];
int i0 = i & M1;
int length = MathLib.min(C1 - i0, srcEnd - i);
System.arraycopy(chars0, i0, dst, j, length);
i += length;
j += length;
}
}
/**
* Sets the character at the specified position.
*
* @param index the index of the character to modify.
* @param c the new character.
* @throws IndexOutOfBoundsException if (index < 0) ||
* (index >= this.length())
*/
public final void setCharAt(int index, char c) {
if ((index < 0) || (index >= _length))
throw new IndexOutOfBoundsException();
_high[index >> B1][index & M1] = c;
}
/**
* Convenience method equivalent to {@link #setLength(int, char)
* setLength(newLength, '\u0000')}.
*
* @param newLength the new length of this builder.
* @throws IndexOutOfBoundsException if (newLength < 0)
*/
public final void setLength(int newLength) {
setLength(newLength, '\u0000');
}
/**
* Sets the length of this character builder.
* If the length is greater than the current length; the
* specified character is inserted.
*
* @param newLength the new length of this builder.
* @param fillChar the character to be appended if required.
* @throws IndexOutOfBoundsException if (newLength < 0)
*/
public final void setLength(int newLength, char fillChar) {
if (newLength < 0)
throw new IndexOutOfBoundsException();
if (newLength <= _length)
_length = newLength;
else
for (int i = _length; i++ < newLength;) {
append(fillChar);
}
}
/**
* Returns a {@link java.lang.CharSequence} corresponding
* to the character sequence between the specified indexes.
*
* @param start the index of the first character inclusive.
* @param end the index of the last character exclusive.
* @return a character sequence.
* @throws IndexOutOfBoundsException if (start < 0) || (end < 0) ||
* (start > end) || (end > this.length())
*/
public final java.lang.CharSequence subSequence(int start, int end) {
if ((start < 0) || (end < 0) || (start > end) || (end > _length))
throw new IndexOutOfBoundsException();
return Text.valueOf(this, start, end);
}
/**
* Appends the specified character.
*
* @param c the character to append.
* @return this
*/
public final TextBuilder append(char c) {
if (_length >= _capacity)
increaseCapacity();
_high[_length >> B1][_length & M1] = c;
_length++;
return this;
}
/**
* Appends the textual representation of the specified object.
* This method is equivalent to
* {@code TextContext.getFormat(obj.getClass()).format(obj, this)}
*/
public final TextBuilder append(Object obj) {
if (obj == null) return append("null");
TextFormat