您的位置:首页 > 产品设计 > UI/UE

java源码分析(4)-AbstractStringBuilder

2016-05-31 17:13 645 查看

AbstractStringBuilder

1.AbstractStringBuilder

为抽象类,主要的属性有两个,一个为value,一个为count,value用于存放值,count用于管理该类的容量

char value[];
int count;
public int length() {//length方法返回的是count的值,而不是value.length
return count;
}
public int capacity() {//capacity方法返回的是value.length,用以返回容量
return value.length;
}
public void ensureCapacity(int minimumCapacity) {
if (minimumCapacity > value.length) {
expandCapacity(minimumCapacity);
}
}
void expandCapacity(int minimumCapacity) {
int newCapacity = (value.length + 1) * 2;//自动扩容机制,每次扩容(value.length+1)*2
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE;//若传入的参数小于0,则直接把容量设置到Integer的最

大值
} else if (minimumCapacity > newCapacity) {
newCapacity = minimumCapacity;//若扩容后的容量还是小于传入的参数,则将传入的参数设

为容量
}
value = Arrays.copyOf(value, newCapacity);
}
public void trimToSize() {
if (count < value.length) {
value = Arrays.copyOf(value, count);//当count小于value.length时,将value多余长度的

值删除,时value.length的长度等于count
}
}

2.trimToSize()

  用于保留value的值,保留的长度为count的值,只有当count的值小于value.length时才起作用,

<strong> public void trimToSize() {
if (count < value.length) {
value = Arrays.copyOf(value, count);
}
}</strong>

3.setLength(int newLength)

public void setLength(int newLength) {
if (newLength < 0)
throw new StringIndexOutOfBoundsException(newLength);
if (newLength > value.length)
expandCapacity(newLength);//当传入的值大于value.length时,需要扩容

if (count < newLength) {
for (; count < newLength; count++)
value[count] = '\0';//为新扩容的元素赋值'\0',为结束符
} else {
count = newLength;//若count小于输入参数,则将count的值设置为输入的值
}
}

4.getChars(int srcBegin, int srcEnd, char dst[],int dstBegin)

append依赖的一个方法,用以添加一个字符串数组

public void getChars(int srcBegin, int srcEnd, char dst[],
int dstBegin)
{
if (srcBegin < 0)
throw new StringIndexOutOfBoundsException(srcBegin);
if ((srcEnd < 0) || (srcEnd > count))
throw new StringIndexOutOfBoundsException(srcEnd);
if (srcBegin > srcEnd)
throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);//用于添加字符串,

将value的值添加到dst[]中
}

5.append()

在对象后拼接字符串或其他对象,效率较高,可以观察到,在拼接时并没有创建新的对象,也没有舍弃旧的对象,相对于String的机制,性能提升相当明显。

public AbstractStringBuilder append(String str) {
if (str == null) str = "null";//若传入的字符串长度为0,则默认添加null
int len = str.length();
if (len == 0) return this;//若添加的字符串长度为0,则直接返回对象本身(不进行添加)
int newCount = count + len;
if (newCount > value.length)
expandCapacity(newCount);//检验容量,若容量不够则扩容
str.getChars(0, len, value, count);//将str的值添加到value后
count = newCount;//容量count更新
return this;
}
public AbstractStringBuilder append(StringBuffer sb) {
if (sb == null)
return append("null");//传入的StringBuffer长度为零,默认添加null
int len = sb.length();
int newCount = count + len;
if (newCount > value.length)
expandCapacity(newCount);//检验容量,有需要则执行扩容
sb.getChars(0, len, value, count);
count = newCount;//更新容量count的值
return this;
}
public AbstractStringBuilder append(CharSequence s) {
if (s == null)
s = "null";
//根据序列类型的不同,执行不同的添加方法
if (s instanceof String)
return this.append((String)s);
if (s instanceof StringBuffer)
return this.append((StringBuffer)s);
return this.append(s, 0, s.length());
}
public AbstractStringBuilder append(int i) {
if (i == Integer.MIN_VALUE) {
append("-2147483648");//Integer最小值为特例,特殊处理
return this;
}
int appendedLength = (i < 0) ? stringSizeOfInt(-i) + 1
: stringSizeOfInt(i);//判断Integer的位数,负数有负号,要

多加一位
int spaceNeeded = count + appendedLength;
if (spaceNeeded > value.length)
expandCapacity(spaceNeeded);
Integer.getChars(i, spaceNeeded, value);
count = spaceNeeded;
return this;
}
final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
99999999, 999999999, Integer.MAX_VALUE };
static int stringSizeOfInt(int x) {
for (int i=0; ; i++)
if (x <= sizeTable[i])
return i+1;
}

6.delete(int start, int end)

用以删除一部分的字符

public AbstractStringBuilder delete(int start, int end) {
//验证参数的有效性
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (end > count)
end = count;//结束下标大于count时,将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;//重置count大小
}
return this;
}

7.replace(int start, int end, String str)

替换本身对象中的一部分字符

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);//计算替换后字符串的长度以此判断是否需要进

行扩容
if (newCount > value.length)
expandCapacity(newCount);
//替换的步骤为,先删除要替换的部分,再拼接要添加的部分
System.arraycopy(value, end, value, start + len, count - end);
str.getChars(value, start);
count = newCount;//更新count值
return this;
}

8.insert(int index, char str[], int offse,int len)

在对象中间插入字符串数组

public AbstractStringBuilder insert(int index, char str[], int offset,
int len)
{
//验证参数有效性
if ((index < 0) || (index > length()))
throw new StringIndexOutOfBoundsException(index);
if ((offset < 0) || (len < 0) || (offset > str.length - len))
throw new StringIndexOutOfBoundsException(
"offset " + offset + ", len " + len + ", str.length "
+ str.length);
int newCount = count + len;
if (newCount > value.length)//验证是否要扩容
expandCapacity(newCount);
System.arraycopy(value, index, value, index + len, count - index);//先在value中建立起

用于存放插入值的空位
System.arraycopy(str, offset, value, index, len);//向空位中插入str
count = newCount;//更新count值
return this;
}

9. reverse()

将对象本身的字符顺序调转后返回给原对象

public AbstractStringBuilder reverse() {//反转字符串
boolean hasSurrogate = false;
int n = count - 1;
//采用从中间向两端遍历,对换对称位置上的字符
for (int j = (n-1) >> 1; j >= 0; --j) {
char temp = value[j];
char temp2 = value[n - j];
if (!hasSurrogate) {//验证每个字符的编码是否在范围内
hasSurrogate = (temp >= Character.MIN_SURROGATE && temp <=

Character.MAX_SURROGATE)
|| (temp2 >= Character.MIN_SURROGATE && temp2 <= Character.MAX_SURROGATE);
}
value[j] = temp2;
value[n - j] = temp;
}
if (hasSurrogate) {
//例如憨子由两个字符组成,直接反转后,用以表示一个汉字的两个字符顺序错了,需要重新

调整
for (int i = 0; i < count - 1; i++) {
char c2 = value[i];
if (Character.isLowSurrogate(c2)) {
char c1 = value[i + 1];
if (Character.isHighSurrogate(c1)) {
value[i++] = c1;//调整两个字符的顺序
value[i] = c2;
}
}
}
}
return this;
}

可以看到,这里需要考虑到字符编码的问题,例如汉字由两个字符组成,将对称位置的字符互换后,两个用以表示一个汉字的字符顺序会颠倒,需要互换这两个字符的位置

   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: