JDK1.8源码阅读记录lang包AbstractStringBuilder类

时光总嘲笑我的痴心妄想 提交于 2020-02-29 02:57:56

JDK1.8源码阅读记录

JAVA.LANG包

AbstractStringBuilder类

abstract class AbstractStringBuilder implements Appendable, CharSequence

Appendable顾名思义,是append()的象征,CharSequence字符序列,有length()、charAt(int index)、subSequence(int start, int end)等常用方法。

变量

/**
     * 存储字符
     */
    char[] value;

    /**
     * 表示已用字符的长度
     */
    int count;

构造方法

/**
     * 无参构造
     */
    AbstractStringBuilder() {
    }

    /**
     * 初始化容量
     */
    AbstractStringBuilder(int capacity) {
        value = new char[capacity];
    }

常用方法

	/*
	* 返回已用字符的长度,count
	*/
	public int length() {
        return count;
    }
    
    /*
	* 返回value数据可容纳的字符数目,value.length,即容量
	*/
    public int capacity() {
        return value.length;
    }
	/*
	* 确保容量(capacity)至少等于minimumCapacity,
	* 调用ensureCapacityInternal(minimumCapacity)方法
	*/
	public void ensureCapacity(int minimumCapacity) {
        if (minimumCapacity > 0)
            ensureCapacityInternal(minimumCapacity);
    }
	
	/*
	* 判断minimumCapacity与当前容量大小,
	* 若minimumCapacity大于当前容量,
	* 调用value=Arrays.copyOf(value,newCapacity(minimumCapacity))方法
	*/
	private void ensureCapacityInternal(int minimumCapacity) {
        // overflow-conscious code
        if (minimumCapacity - value.length > 0) {
            value = Arrays.copyOf(value,
                    newCapacity(minimumCapacity));
        }
    }
	
	/*
	* MAX_ARRAY_SIZE 表示理论上允许value的最大长度,
	* Integer.MAX_VALUE-8是因为有些虚拟机会自动给value添加一些标题字,
	* 为防止异常才-8
	*/
	private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
	
	/*
	* 返回至少等于给定最小容量的容量。 如果足够,则返回增加了相同数量+ 2的当前容量。 
	* 不会返回大于MAX_ARRAY_SIZE的容量,除非给定的最小容量大于该容量。
	*/
	private int newCapacity(int minCapacity) {
        // overflow-conscious code
        int newCapacity = (value.length << 1) + 2;
        if (newCapacity - minCapacity < 0) {
            newCapacity = minCapacity;
        }
        return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)
            ? hugeCapacity(minCapacity)
            : newCapacity;
    }
	
	/*
	* 扩容,若给定最小容量大于MAX_ARRAY_SIZE,若扩容到给定最小容量
	* 否则只扩容到MAX_ARRAY_SIZE
	*/
    private int hugeCapacity(int minCapacity) {
        if (Integer.MAX_VALUE - minCapacity < 0) { // overflow
            throw new OutOfMemoryError();
        }
        return (minCapacity > MAX_ARRAY_SIZE)
            ? minCapacity : MAX_ARRAY_SIZE;
    }
	/*
	* 减少容量到count,即删去未被使用的位置
	*/
    public void trimToSize() {
        if (count < value.length) {
            value = Arrays.copyOf(value, count);
        }
    }
	
	/*
	* newLength大于count ,设置value长度为newLength,
	* 除了已用的位置,其余位置用'\0'代替
	*/
    public void setLength(int newLength) {
        if (newLength < 0)
            throw new StringIndexOutOfBoundsException(newLength);
        ensureCapacityInternal(newLength);

        if (count < newLength) {
            Arrays.fill(value, count, newLength, '\0');
        }

        count = newLength;
    }
	
	/*
	* 取得给定索引的字符
	*/
	public char charAt(int index) {
        if ((index < 0) || (index >= count))
            throw new StringIndexOutOfBoundsException(index);
        return value[index];
    }
    
	/*
	* 将指定位置的字符内容复制到目标字符数组
	*/
	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);
    }
	
	/*
	* 设置给定索引值处的字符内容
	*/
	public void setCharAt(int index, char ch) {
        if ((index < 0) || (index >= count))
            throw new StringIndexOutOfBoundsException(index);
        value[index] = ch;
    }
	public AbstractStringBuilder append(Object obj) {return append(String.valueOf(obj));
    }
    public AbstractStringBuilder append(String str)
    public AbstractStringBuilder append(StringBuffer sb)
    AbstractStringBuilder append(AbstractStringBuilder asb)
    public AbstractStringBuilder append(CharSequence s)
    private AbstractStringBuilder appendNull()
    public AbstractStringBuilder append(CharSequence s, int start, int end)
    public AbstractStringBuilder append(char[] str)
    public AbstractStringBuilder append(char str[], int offset, int len)
    public AbstractStringBuilder append(boolean b)
    public AbstractStringBuilder append(char c)
    public AbstractStringBuilder append(int i)
    public AbstractStringBuilder append(long l)
    public AbstractStringBuilder append(float f)
    public AbstractStringBuilder append(double d)
    public AbstractStringBuilder delete(int start, int end)

在尾部增加指定内容,方法虽多,但大同小异。

	public AbstractStringBuilder delete(int start, int end)
	public AbstractStringBuilder deleteCharAt(int index)

删除给定位置包括字符,即不仅内容删除,容量也随之减少。

	public AbstractStringBuilder replace(int start, int end, String str)

用str字符串代替本对象从start到end处的子字符串,若容量不足,则扩容。

    public String substring(int start) {
        return substring(start, count);
    }
	public CharSequence subSequence(int start, int end) {
        return substring(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);
    }

取得给定位置的子字符串或子字符序列。

    public AbstractStringBuilder insert(int index, char[] str, int offset,
                                        int len)
  	public AbstractStringBuilder insert(int offset, Object obj)
  	public AbstractStringBuilder insert(int offset, String str)
  	public AbstractStringBuilder insert(int offset, char[] str)
  	public AbstractStringBuilder insert(int dstOffset, CharSequence s)
  	public AbstractStringBuilder insert(int dstOffset, CharSequence s,
                                         int start, int end)
	public AbstractStringBuilder insert(int offset, boolean b)
	public AbstractStringBuilder insert(int offset, char c)
	public AbstractStringBuilder insert(int offset, int i)
	public AbstractStringBuilder insert(int offset, long l)
	public AbstractStringBuilder insert(int offset, float f)
	public AbstractStringBuilder insert(int offset, double d)
	

在指定位置插入指定内容,并返回新的字符串或字符序列。

	public int indexOf(String str)
	public int indexOf(String str, int fromIndex)

从指定位置开始向后搜寻首次出现str字符串的索引值。

	public int lastIndexOf(String str)
	public int lastIndexOf(String str, int fromIndex)

从fromIndex位置开始向前搜寻最后一次出现str的索引值。

    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;
    }
    private void reverseAllValidSurrogatePairs() {
        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;
                }
            }
        }
    }

导致此字符序列被序列的反序替换。

	/*
	*返回表示此序列中数据的字符串。
	*/
	public abstract String toString();
	
	/*
	*子类无法继承,返回字符串的内容。
	*/
	final char[] getValue() {
        return value;
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!