|
|
@@ -24,6 +24,14 @@ import net.ranides.assira.generic.ValueUtils;
|
|
|
import net.ranides.assira.math.MathUtils;
|
|
|
|
|
|
/**
|
|
|
+ * This class is alternative to java StringBuilder:
|
|
|
+ * - it offers a bit more methods for appending
|
|
|
+ * - it supports "printf" append
|
|
|
+ * - it supports appending list of items (using some join separator)
|
|
|
+ * - it can be converted to Reader/Writer views
|
|
|
+ *
|
|
|
+ * Configuration options (for example list separators) could be configured independently in every opened scope
|
|
|
+ *
|
|
|
*
|
|
|
* @author Ranides Atterwim {@literal <ranides@gmail.com>}
|
|
|
*/
|
|
|
@@ -35,7 +43,7 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
- private static final StrBuilderOptions OPTIONS = new StrBuilderOptions();
|
|
|
+ private static final StrBuilderOptions OPTIONS = new StrBuilderOptions() { };
|
|
|
|
|
|
protected static final char[] EMPTY = new char[0];
|
|
|
|
|
|
@@ -48,29 +56,68 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
private final Supplier<Formatter> formatter = LazyReference.unique(() -> new Formatter(this));
|
|
|
|
|
|
final Deque<BuilderState> options = new ArrayDeque<>();
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Creates new builder with default capacity
|
|
|
+ */
|
|
|
public StrBuilder() {
|
|
|
this(CAPACITY);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates new builder with specified capacity
|
|
|
+ *
|
|
|
+ * @param capacity capacity
|
|
|
+ */
|
|
|
public StrBuilder(int capacity) {
|
|
|
buffer = new char[capacity];
|
|
|
options.push(new BuilderState(OPTIONS));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns options for current block.
|
|
|
+ *
|
|
|
+ * StrBuilder supports nested blocks marked by "open" and "close" methods.
|
|
|
+ * Nested blocks inherit options from parent block, but can be modified without affecting other blocks.
|
|
|
+ *
|
|
|
+ * For example, you can open block, override separator, append some items and forget after close.
|
|
|
+ * Your change won't brake later usages of separator in former blocks.
|
|
|
+ *
|
|
|
+ * @return options
|
|
|
+ */
|
|
|
public StrBuilderOptions options() {
|
|
|
return options.peek();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns current size of buffer
|
|
|
+ *
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
@Override
|
|
|
public int length() {
|
|
|
return size;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns current size of internal buffer.
|
|
|
+ * Capacity is automatically increased on demand.
|
|
|
+ *
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
public int capacity() {
|
|
|
return buffer.length;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Changes size of content stored the buffer.
|
|
|
+ * If content is longer than "length", some data will be discarded.
|
|
|
+ * If content is shorter than "length", it will append "c" characters.
|
|
|
+ *
|
|
|
+ * @param length length
|
|
|
+ * @param c c
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder resize(int length, char c) {
|
|
|
if (length < 0) {
|
|
|
throw new IllegalArgumentException("negative size: " + length);
|
|
|
@@ -83,22 +130,47 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Allocates space for internal buffer.
|
|
|
+ * If buffer is larger than specified "capacity", it does nothing.
|
|
|
+ *
|
|
|
+ * @param capacity capacity
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder reserve(int capacity) {
|
|
|
buffer = ArrayAllocator.grow(buffer, capacity, size);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Reallocates internal buffer to current "size"
|
|
|
+ *
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder trim() {
|
|
|
return trim(size);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Reallocates internal buffer to specified size "n".
|
|
|
+ *
|
|
|
+ * Please note that it preserves current content, so trimmed buffer can be larger than "n"
|
|
|
+ *
|
|
|
+ * @param n n
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder trim(int n) {
|
|
|
if (buffer.length > n && buffer.length!=Math.max(n,size)) {
|
|
|
buffer = ArrayAllocator.trim(buffer, Math.max(n,size));
|
|
|
}
|
|
|
return this;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Clears data appended into this buffer
|
|
|
+ *
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder clear() {
|
|
|
size = 0;
|
|
|
BuilderState state = options.peek().copy();
|
|
|
@@ -112,23 +184,50 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
StringUtils.checkIndex(size, index);
|
|
|
return buffer[index];
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Changes character inside buffer at specified position.
|
|
|
+ *
|
|
|
+ * @param index index
|
|
|
+ * @param value value
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder setCharAt(int index, char value) {
|
|
|
StringUtils.checkIndex(size, index);
|
|
|
buffer[index] = value;
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns subsequence which is view of this buffer.
|
|
|
+ * Every change inside buffer will be visible in returned CharSequence
|
|
|
+ *
|
|
|
+ * @param begin begin
|
|
|
+ * @param end end
|
|
|
+ * @return CharSequence
|
|
|
+ */
|
|
|
@Override
|
|
|
public CharSequence subSequence(int begin, int end) {
|
|
|
NativeArrayAllocator.ensureFromTo(size, begin, end);
|
|
|
return StringUtils.wrap(buffer, begin, end);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns new array with current data copied into it
|
|
|
+ *
|
|
|
+ * @return copy
|
|
|
+ */
|
|
|
public char[] getChars() {
|
|
|
return getChars(0, size);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns new array with fragment of current data copied into it
|
|
|
+ *
|
|
|
+ * @param begin begin
|
|
|
+ * @param end end
|
|
|
+ * @return copy
|
|
|
+ */
|
|
|
public char[] getChars(int begin, int end) {
|
|
|
StringUtils.checkRange(size, begin, end);
|
|
|
if (end == begin) {
|
|
|
@@ -136,58 +235,148 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
}
|
|
|
return ArrayUtils.slice(buffer, begin, end);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Copies current data into provided array.
|
|
|
+ *
|
|
|
+ * @param target target
|
|
|
+ * @return target
|
|
|
+ */
|
|
|
public char[] getChars(char[] target) {
|
|
|
return getChars(0, size, target, 0);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Copies fragment of current data into provided array
|
|
|
+ * Copy operation will start writing to array from "offset"
|
|
|
+ *
|
|
|
+ * @param begin begin
|
|
|
+ * @param end end
|
|
|
+ * @param target target
|
|
|
+ * @param offset offset
|
|
|
+ * @return target
|
|
|
+ */
|
|
|
public char[] getChars(int begin, int end, char[] target, int offset) {
|
|
|
StringUtils.checkRange(size, begin, end);
|
|
|
System.arraycopy(buffer, begin, target, offset, end - begin);
|
|
|
return target;
|
|
|
}
|
|
|
-
|
|
|
- public String getRightFragment(int length) {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns string with "length" leading characters.
|
|
|
+ *
|
|
|
+ * @param length length
|
|
|
+ * @return String
|
|
|
+ */
|
|
|
+ public String getHeadFragment(int length) {
|
|
|
+ return new String(buffer, 0, MathUtils.clip(length, 0, size));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns string with "length" trailing characters.
|
|
|
+ *
|
|
|
+ * @param length length
|
|
|
+ * @return String
|
|
|
+ */
|
|
|
+ public String getTailFragment(int length) {
|
|
|
int n = MathUtils.clip(length, 0, size);
|
|
|
return new String(buffer, size - n, n);
|
|
|
}
|
|
|
-
|
|
|
- public String getLeftFragment(int length) {
|
|
|
- return new String(buffer, 0, MathUtils.clip(length, 0, size));
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Reverses characters inside buffer.
|
|
|
+ *
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder reverse() {
|
|
|
NativeArrayUtils.reverse(NativeArray.wrap(buffer), size);
|
|
|
return this;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Starts new block in list mode.
|
|
|
+ * Current opened block is stacked and will be restored on close.
|
|
|
+ *
|
|
|
+ * Appends to the buffer "options.open" string
|
|
|
+ *
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder open() {
|
|
|
options.push(options.peek().derive());
|
|
|
return append(options.peek().open());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Starts new block in list mode.
|
|
|
+ * Current opened block is stacked and will be restored on close.
|
|
|
+ *
|
|
|
+ * Appends to the buffer "open" string
|
|
|
+ *
|
|
|
+ * @param open open
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder open(String open) {
|
|
|
options.push(options.peek().derive().open(open));
|
|
|
return append(open);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Starts new block in list mode.
|
|
|
+ * Current opened block is stacked and will be restored on close.
|
|
|
+ *
|
|
|
+ * Appends to the buffer "open" string
|
|
|
+ * Closing block will append "close" string
|
|
|
+ *
|
|
|
+ * @param open open
|
|
|
+ * @param close close
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder open(String open, String close) {
|
|
|
options.push(options.peek().derive().open(open).close(close));
|
|
|
return append(open);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Starts new block in list mode.
|
|
|
+ * Current opened block is stacked and will be restored on close.
|
|
|
+ *
|
|
|
+ * Appends to the buffer "open" string
|
|
|
+ * Closing block will append "close" string
|
|
|
+ * Inserted items will be separated by "separator"
|
|
|
+ *
|
|
|
+ * @param open open
|
|
|
+ * @param close close
|
|
|
+ * @param separator separator
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder open(String open, String close, String separator) {
|
|
|
options.push(options.peek().derive().open(open).close(close).separator(separator));
|
|
|
return append(open);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Closes current block.
|
|
|
+ * Restores state of previously opened block, if any.
|
|
|
+ *
|
|
|
+ * Appends to the buffer "close" string
|
|
|
+ *
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder close() {
|
|
|
if(options.size() <= 1) {
|
|
|
throw new IllegalStateException("There is not block to close.");
|
|
|
}
|
|
|
return append(options.pop().close());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * If you want to insert some values into block, separated by "separator", you can use this method.
|
|
|
+ * It should be called BEFORE each appended item.
|
|
|
+ *
|
|
|
+ * Appends to the buffer "separator" string if it is not first call inside current block.
|
|
|
+ *
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder item() {
|
|
|
BuilderState state = options.peek();
|
|
|
if(0 != state.counter++) {
|
|
|
@@ -195,11 +384,21 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
}
|
|
|
return this;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends new line separator as configured in options for current block.
|
|
|
+ *
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder endl() {
|
|
|
return append(options.peek().endl());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends blank value as configured in options for current block.
|
|
|
+ *
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder append() {
|
|
|
String value = options.peek().blank();
|
|
|
return null == value ? this : append(value);
|
|
|
@@ -221,11 +420,25 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
}
|
|
|
return this;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends string into buffer
|
|
|
+ *
|
|
|
+ * @param value value
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder append(String value) {
|
|
|
return (value == null) ? append() : append(value, 0, value.length());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends fragment of string into buffer
|
|
|
+ *
|
|
|
+ * @param value value
|
|
|
+ * @param begin begin
|
|
|
+ * @param end end
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder append(String value, int begin, int end) {
|
|
|
if (value == null) {
|
|
|
return append();
|
|
|
@@ -239,11 +452,25 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
}
|
|
|
return this;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends StringBuffer into buffer
|
|
|
+ *
|
|
|
+ * @param value value
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder append(StringBuffer value) {
|
|
|
return (value == null) ? append() : append(value, 0, value.length());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends fragment of StringBuffer into buffer
|
|
|
+ *
|
|
|
+ * @param value value
|
|
|
+ * @param begin begin
|
|
|
+ * @param end end
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder append(StringBuffer value, int begin, int end) {
|
|
|
if (value == null) {
|
|
|
return append();
|
|
|
@@ -257,11 +484,25 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
}
|
|
|
return this;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends StringBuilder into buffer
|
|
|
+ *
|
|
|
+ * @param value value
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder append(StringBuilder value) {
|
|
|
return (value == null) ? append() : append(value, 0, value.length());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends fragment of StringBuilder into buffer
|
|
|
+ *
|
|
|
+ * @param value value
|
|
|
+ * @param begin begin
|
|
|
+ * @param end end
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder append(StringBuilder value, int begin, int end) {
|
|
|
if (value == null) {
|
|
|
return append();
|
|
|
@@ -275,11 +516,25 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
}
|
|
|
return this;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends chars into buffer
|
|
|
+ *
|
|
|
+ * @param value value
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder append(char[] value) {
|
|
|
return (value == null) ? append() : append(value, 0, value.length);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends fragment of chars into buffer
|
|
|
+ *
|
|
|
+ * @param value value
|
|
|
+ * @param begin begin
|
|
|
+ * @param end end
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder append(char[] value, int begin, int end) {
|
|
|
if (value == null) {
|
|
|
return append();
|
|
|
@@ -301,42 +556,116 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Appends value into buffer.
|
|
|
+ * Converts object using "toString" method.
|
|
|
+ *
|
|
|
+ * @param value value
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder append(Object value) {
|
|
|
return (value == null) ? append() : append(value.toString());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends value into buffer.
|
|
|
+ * Converts boolean to "true" or "false" string
|
|
|
+ *
|
|
|
+ * @param value value
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder append(boolean value) {
|
|
|
return append(value ? "true" : "false");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends value into buffer.
|
|
|
+ * Converts value using "String.valueOf"
|
|
|
+ *
|
|
|
+ * @param value value
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder append(int value) {
|
|
|
return append(String.valueOf(value));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Appends value into buffer.
|
|
|
+ * Converts value using "String.valueOf"
|
|
|
+ *
|
|
|
+ * @param value value
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder append(long value) {
|
|
|
return append(String.valueOf(value));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Appends value into buffer.
|
|
|
+ * Converts value using "String.valueOf"
|
|
|
+ *
|
|
|
+ * @param value value
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder append(float value) {
|
|
|
return append(String.valueOf(value));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Appends value into buffer.
|
|
|
+ * Converts value using "String.valueOf"
|
|
|
+ *
|
|
|
+ * @param value value
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder append(double value) {
|
|
|
return append(String.valueOf(value));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends values into buffer, separating them using "options.separator".
|
|
|
+ * Converts values using "String.valueOf"
|
|
|
+ *
|
|
|
+ * @param values values
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder append(Iterable<?> values) {
|
|
|
return append(values.iterator(), this::asString);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends values into buffer, separating them using "options.separator".
|
|
|
+ * Converts values using "function"
|
|
|
+ *
|
|
|
+ * @param values values
|
|
|
+ * @param function function
|
|
|
+ * @param <T> T
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public <T> StrBuilder append(Iterable<? extends T> values, Function<? super T,String> function) {
|
|
|
return append(values.iterator(), function);
|
|
|
}
|
|
|
-
|
|
|
- public <T> StrBuilder append(Iterator<? extends T> values) {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends values into buffer, separating them using "options.separator".
|
|
|
+ * Converts values using "String.valueOf"
|
|
|
+ *
|
|
|
+ * @param values values
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
+ public StrBuilder append(Iterator<?> values) {
|
|
|
return append(values, this::asString);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends values into buffer, separating them using "options.separator".
|
|
|
+ * Converts values using "function"
|
|
|
+ *
|
|
|
+ * @param values values
|
|
|
+ * @param function function
|
|
|
+ * @param <T> T
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public <T> StrBuilder append(Iterator<? extends T> values, Function<? super T,String> function) {
|
|
|
if ( !values.hasNext() ) {
|
|
|
return this;
|
|
|
@@ -351,7 +680,15 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
append(separator);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends values into buffer, separating them using "options.separator".
|
|
|
+ * Converts values using "String.valueOf"
|
|
|
+ *
|
|
|
+ * @param values values
|
|
|
+ * @param <T> T
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public <T> StrBuilder append(T[] values) {
|
|
|
return append(values, this::asString);
|
|
|
}
|
|
|
@@ -359,7 +696,16 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
private String asString(Object value) {
|
|
|
return null == value ? null : value.toString();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends values into buffer, separating them using "options.separator".
|
|
|
+ * Converts values using "function"
|
|
|
+ *
|
|
|
+ * @param values values
|
|
|
+ * @param function function
|
|
|
+ * @param <T> T
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public <T> StrBuilder append(T[] values, Function<? super T,String> function) {
|
|
|
if ( 0==values.length ) {
|
|
|
return this;
|
|
|
@@ -371,12 +717,26 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
}
|
|
|
return this;
|
|
|
}
|
|
|
-
|
|
|
- public <T> StrBuilder append(NativeArray values) {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends values into buffer, separating them using "options.separator".
|
|
|
+ * Converts values using "String.valueOf"
|
|
|
+ *
|
|
|
+ * @param values values
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
+ public StrBuilder append(NativeArray values) {
|
|
|
return append(values.size(), i -> String.valueOf(values.get(i)));
|
|
|
}
|
|
|
-
|
|
|
- public <T> StrBuilder append(int size, IntFunction<String> function) {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Appends "size" values generated by "function", separating them using "options.separator".
|
|
|
+ *
|
|
|
+ * @param size size
|
|
|
+ * @param function function
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
+ public StrBuilder append(int size, IntFunction<String> function) {
|
|
|
if ( 0==size ) {
|
|
|
return this;
|
|
|
}
|
|
|
@@ -388,6 +748,15 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Replaces characters from specified range by new value.
|
|
|
+ * Shifts all data, if necessary, i.e. value had different length than range.
|
|
|
+ *
|
|
|
+ * @param begin begin
|
|
|
+ * @param end end
|
|
|
+ * @param value value
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder replace(int begin, int end, CharSequence value) {
|
|
|
if(value == null) {
|
|
|
value = options.peek().blank();
|
|
|
@@ -405,6 +774,15 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Replaces characters from specified range by new value
|
|
|
+ * Shifts all data, if necessary, i.e. value had different length than range.
|
|
|
+ *
|
|
|
+ * @param begin begin
|
|
|
+ * @param end end
|
|
|
+ * @param value value
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder replace(int begin, int end, char[] value) {
|
|
|
if(value == null) {
|
|
|
return replace(begin, end, options.peek().blank());
|
|
|
@@ -419,12 +797,28 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Inserts value at specified index.
|
|
|
+ * Shifts content already stored in buffer if necessary.
|
|
|
+ *
|
|
|
+ * @param offset offset
|
|
|
+ * @param value value
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder insert(int offset, CharSequence value) {
|
|
|
insertBytes(offset, value.length());
|
|
|
copy(offset, value, 0, value.length());
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Inserts value at specified index.
|
|
|
+ * Shifts content already stored in buffer if necessary.
|
|
|
+ *
|
|
|
+ * @param offset offset
|
|
|
+ * @param value value
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder insert(int offset, char[] value) {
|
|
|
insertBytes(offset, value.length);
|
|
|
copy(offset, value, 0, value.length);
|
|
|
@@ -461,6 +855,14 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Appends string generated by {@link Formatter}.
|
|
|
+ * It uses default locale.
|
|
|
+ *
|
|
|
+ * @param format format
|
|
|
+ * @param arguments arguments
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
public StrBuilder printf(String format, Object... arguments) {
|
|
|
formatter.get().format(Locale.getDefault(), format, arguments);
|
|
|
return this;
|
|
|
@@ -513,15 +915,33 @@ public final class StrBuilder implements CharSequence, Appendable, Serializable
|
|
|
public String toString() {
|
|
|
return new String(buffer, 0, size);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Creates new StringBuilder with buffer content.
|
|
|
+ * StringBuilder capacity is exactly current size.
|
|
|
+ *
|
|
|
+ * @return StringBuilder
|
|
|
+ */
|
|
|
public StringBuilder toBuilder() {
|
|
|
return new StringBuilder(size).append(buffer, 0, size);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns new instance of Writer view.
|
|
|
+ * Writer can be used to change buffer and vice versa.
|
|
|
+ *
|
|
|
+ * @return Writer
|
|
|
+ */
|
|
|
public Writer asWriter() {
|
|
|
return new SWriter();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns new instance of Reader view.
|
|
|
+ * Changes inside buffer will be visible in Reader.
|
|
|
+ *
|
|
|
+ * @return Reader
|
|
|
+ */
|
|
|
public Reader asReader() {
|
|
|
return new SReader();
|
|
|
}
|