-
Notifications
You must be signed in to change notification settings - Fork 6
/
demo_code.java
120 lines (115 loc) · 4.19 KB
/
demo_code.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Deletes the character at the specified index.
*
* @see #charAt(int)
* @see #setCharAt(int, char)
* @param index the index to delete
* @return this, to enable chaining
* @throws IndexOutOfBoundsException if the index is invalid
*/
public StrBuilder deleteCharAt(int index) {
if (index < 0 || index >= size) {
throw new StringIndexOutOfBoundsException(index);
}
deleteImpl(index, index + 1, 1);
return this;
}
//-----------------------------------------------------------------------
/**
* Copies the builder's character array into a new character array.
*
* @return a new array that represents the contents of the builder
*/
public char[] toCharArray() {
if (size == 0) {
return ArrayUtils.EMPTY_CHAR_ARRAY;
}
char chars[] = new char[size];
System.arraycopy(buffer, 0, chars, 0, size);
return chars;
}
/**
* Appends a separator to the builder if the loop index is greater than zero.
* The separator is appended using {@link #append(char)}.
* <p>
* This method is useful for adding a separator each time around the
* loop except the first.
* <pre>
* for (int i = 0; i < list.size(); i++) {
* appendSeparator(",", i);
* append(list.get(i));
* }
* </pre>
* Note that for this simple example, you should use
* {@link #appendWithSeparators(Iterable, String)}.
*
* @param separator the separator to use
* @param loopIndex the loop index
* @return this, to enable chaining
* @since 2.3
*/
public StrBuilder appendSeparator(char separator, int loopIndex) {
if (loopIndex > 0) {
append(separator);
}
return this;
}
//-----------------------------------------------------------------------
/**
* Appends the pad character to the builder the specified number of times.
*
* @param length the length to append, negative means no append
* @param padChar the character to append
* @return this, to enable chaining
*/
public StrBuilder appendPadding(int length, char padChar) {
if (length >= 0) {
ensureCapacity(size + length);
for (int i = 0; i < length; i++) {
buffer[size++] = padChar;
}
}
return this;
}
/**
* Appends an object to the builder padding on the right to a fixed length.
* The <code>toString</code> of the object is used.
* If the object is larger than the length, the right hand side is lost.
* If the object is null, null text value is used.
*
* @param obj the object to append, null uses null text
* @param width the fixed field width, zero or negative has no effect
* @param padChar the pad character to use
* @return this, to enable chaining
*/
public StrBuilder appendFixedWidthPadRight(Object obj, int width, char padChar) {
if (width > 0) {
ensureCapacity(size + width);
String str = (obj == null ? getNullText() : obj.toString());
int strLen = str.length();
if (strLen >= width) {
str.getChars(0, width, buffer, size);
} else {
int padLen = width - strLen;
str.getChars(0, strLen, buffer, size);
for (int i = 0; i < padLen; i++) {
buffer[size + strLen + i] = padChar;
}
}
size += width;
}
return this;
}
/**
* Appends an object to the builder padding on the right to a fixed length.
* The <code>String.valueOf</code> of the <code>int</code> value is used.
* If the object is larger than the length, the right hand side is lost.
*
* @param value the value to append
* @param width the fixed field width, zero or negative has no effect
* @param padChar the pad character to use
* @return this, to enable chaining
*/
public StrBuilder appendFixedWidthPadRight(int value, int width, char padChar) {
return appendFixedWidthPadRight(String.valueOf(value), width, padChar);
}