一、
此类是抽象类,继承了 Appendable, CharSequence接口。
abstract class AbstractStringBuilder implements Appendable, CharSequence
实现了CharSequence字符序列的接口
- 需要实现length()方法来获取字符序列的长度
- 需要实现charAt(int index)方法来获取指定位置的字符
- 需要实现subSequence(int start, int end)获取子序列
- 含有toString()
实现了Appendable字符可添加的接口
- 需要实现append(CharSequence csq),添加一个字符序列
- 需要实现 append(CharSequence csq, int start, int end) throws IOException,添加字符序列的一部分
- 需要实现append(char c) throws IOException添加字符
二、 常量:
char[] value; //存储字符
int count; //实际存储的数量
相较于String里面的不可变的value和count,这里的这两个变量是可以变得的
三、构造器:
//空参构造器
AbstractStringBuilder() { }
//创建指定容量的AbstractStringBuilder
AbstractStringBuilder(int capacity) {
value = new char[capacity];
}
四、方法:
-- CharSequence实现
//返回当前容量。容量是可用于新插入字符的存储量,超过该容量将进行分配
public int capacity() {
return value.length;
}
//返回指定位置的字符
public char charAt(int index) {
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
return value[index];
}
//返回从start到end的子字符序列
public CharSequence subSequence(int start, int end) {
return substring(start, end);
}
//重载
public CharSequence subSequence(int start, int end) {
return substring(start, end);
}
-- Appendable实现
//添加指定字符串到字符序列尾,如果参数字符串是null,会向后添加`null`四个字符
//并
public AbstractStringBuilder append(String str) {
if (str == null)
return appendNull();
int len = str.length();
//扩容,如果当前容量不够的话
//count+len为最小容量,扩容机制是将原有的容量扩大2倍再加1,
//乘2加1后,如果大于Integer.MAX_VALUE会报错 OutOfMemoryError()。
//则将容量置为Integer.MAX_VALUE - 8和最小容量的最小值。
//乘2加1后小于最小容量,那么将最小容量置为扩容后的容量。
ensureCapacityInternal(count + len);
str.getChars(0, len, value, count);
count += len;
return this;
}
public AbstractStringBuilder append(StringBuffer sb) {
if (sb == null)
return appendNull();
int len = sb.length();
ensureCapacityInternal(count + len);
sb.getChars(0, len, value, count);
count += len;
return this;
}
/**
* @since 1.8
*/
AbstractStringBuilder append(AbstractStringBuilder asb) {
if (asb == null)
return appendNull();
int len = asb.length();
ensureCapacityInternal(count + len);
asb.getChars(0, len, value, count);
count += len;
return this;
}
// Documentation in subclasses because of synchro difference
@Override
public AbstractStringBuilder append(CharSequence s) {
if (s == null)
return appendNull();
if (s instanceof String)
return this.append((String)s);
if (s instanceof AbstractStringBuilder)
return this.append((AbstractStringBuilder)s);
return this.append(s, 0, s.length());
}
//删除指定位置的字符串序列
public AbstractStringBuilder delete(int start, int end) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (end > count)
end = count;
if (start > end)
throw new StringIndexOutOfBoundsException();
int len = end - start;
if (len > 0) {
System.arraycopy(value, start+len, value, start, count-end);
count -= len;
}
return this;
}
public AbstractStringBuilder deleteCharAt(int index) {
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
System.arraycopy(value, index+1, value, index, count-index-1);
count--;
return this;
}
//将start到end的子字符串置为指定指定字符串
public AbstractStringBuilder replace(int start, int end, String str) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (start > count)
throw new StringIndexOutOfBoundsException("start > length()");
if (start > end)
throw new StringIndexOutOfBoundsException("start > end");
if (end > count)
end = count;
int len = str.length();
int newCount = count + len - (end - start);
ensureCapacityInternal(newCount);
System.arraycopy(value, end, value, start + len, count - end);
str.getChars(value, start);
count = newCount;
return this;
}
//返回从下标start开始的子字符串
public String substring(int start) {
return substring(start, count);
}
//返回下标从start到end的子序列
public CharSequence subSequence(int start, int end) {
return substring(start, end);
}
//返回下标从start到end的子字符串,实际上面几个方法都调用的这个方法
public String substring(int start, int end) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (end > count)
throw new StringIndexOutOfBoundsException(end);
if (start > end)
throw new StringIndexOutOfBoundsException(end - start);
return new String(value, start, end - start);
}
indexOf();
//返回指定子字符串首次出现时在此字符串内的索引。
public int indexOf(String str) {
return indexOf(str, 0);
}
//从指定的索引开始,返回指定子字符串首次出现时在此字符串内的索引。
public int indexOf(String str, int fromIndex) {
return String.indexOf(value, 0, count, str, fromIndex);
}
//返回该字符串内指定子字符串最右边出现的索引
public int lastIndexOf(String str) {
return lastIndexOf(str, count);
}
//返回最后一次出现的指定子字符串在此字符串内的索引
public int lastIndexOf(String str, int fromIndex) {
return String.lastIndexOf(value, 0, count, str, fromIndex);
}
//反转字符
public AbstractStringBuilder reverse() {
boolean hasSurrogates = false;
int n = count - 1;
for (int j = (n-1) >> 1; j >= 0; j--) {
int k = n - j;
char cj = value[j];
char ck = value[k];
value[j] = ck;
value[k] = cj;
if (Character.isSurrogate(cj) ||
Character.isSurrogate(ck)) {
hasSurrogates = true;
}
}
if (hasSurrogates) {
reverseAllValidSurrogatePairs();
}
return this;
}