Java源碼閱讀之String(2)

Java源碼閱讀之String(2)

這一篇博客用於記錄String類的部分方法。

//獲取字符串長度,返回的是value數組的長度
public int length() {
    return value.length;
}

//根據value數組長度判斷字符串是否爲空
public boolean isEmpty() {
    return value.length == 0;
}

//根據index返回所在位置的字符
public char charAt(int index) {
    if ((index < 0) || (index >= value.length)) {
        throw new StringIndexOutOfBoundsException(index);
    }
    return value[index];
}

//返回指定位置的Unicode碼點,如果(index-1)處的char值處於低位範
//圍內,(index-2)不是負數,並且(index-2)處的char值處於高位
//範圍內,則補充代碼點的值被返回。 如果index-1中的char值是
//不成對的低位,則返回碼值。
public int codePointBefore(int index) {
    int i = index - 1;
    if ((i < 0) || (i >= value.length)) {
        throw new StringIndexOutOfBoundsException(index);
    }
    return Character.codePointBeforeImpl(value, index, 0);
}

//返回此String對象的指定範圍內的Unicode碼點。 範圍從指定的
//beginIndex開始,並擴展到索引endIndex-1處的char。並且計算是否爲高
//低位合併的Unicode碼點
public int codePointCount(int beginIndex, int endIndex) {
    if (beginIndex < 0 || endIndex > value.length || 
        beginIndex > endIndex) {
        throw new IndexOutOfBoundsException();
    }
    return Character.codePointCountImpl(value, beginIndex, 
        endIndex - beginIndex);
}

//返回此 String 中從給定的 index 處偏移 codePointOffset 個代碼點
//的索引。文本範圍內由 index 和 codePointOffset 給定的未配對代理項
//各計爲一個代碼點。
public int offsetByCodePoints(int index, int 
    codePointOffset) {
    if (index < 0 || index > value.length) {
        throw new IndexOutOfBoundsException();
    }
    return Character.offsetByCodePointsImpl(value, 0, 
        value.length,index, codePointOffset);
}

//從dstBegin開始,將這個字符串中的字符複製到dst中。 
//此方法不執行任何範圍檢查。
void getChars(char dst[], int dstBegin) {
    System.arraycopy(value, 0, dst, dstBegin, 
        value.length);
}

//將此字符串中的字符複製到目標字符數組中。
//複製範圍爲srcBegin到srcEnd-1,複製到從索引dstBegin開始到索引處結
//束的dst子數組中
public void getChars(int srcBegin, int srcEnd, char dst[], 
    int dstBegin) {
    if (srcBegin < 0) {
        throw new 
            StringIndexOutOfBoundsException(srcBegin);
    }
    if (srcEnd > value.length) {
        throw new StringIndexOutOfBoundsException(srcEnd);
    }
    if (srcBegin > srcEnd) {
        throw new StringIndexOutOfBoundsException(srcEnd - 
            srcBegin);
    }
    System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd 
        - srcBegin);
}

//根據所給的編碼方式返回byte字節序列,給定編碼方式爲編碼名稱
public byte[] getBytes(String charsetName)
    throws UnsupportedEncodingException {
    if (charsetName == null) 
        throw new NullPointerException();
    return StringCoding.encode(charsetName, value, 0, 
        value.length);
}

//根據所給的編碼方式返回byte字節序列,給定編碼方式爲編碼對象
public byte[] getBytes(Charset charset) {
    if (charset == null) 
        throw new NullPointerException();
    return StringCoding.encode(charset, value, 0, 
        value.length);
}

//根據默認的編碼方式返回byte字節序列,默認編碼方式爲"ISO-8859-1"
public byte[] getBytes() {
    return StringCoding.encode(value, 0, value.length);
}

//判斷當前String對象與anObject對象是否相等
public boolean equals(Object anObject) {
    //判斷兩者引用地址是否相等,即是否指向同一個對象
    if (this == anObject) {
        return true;
    }
    //如果不指向同一個對象,需要判斷兩者內容是否相等,在判斷兩者內容
    //相等之前需要確定anObject也是一個String對象
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            //對當前對象的value和anObject的value逐一對比,有一個
            //不相同則返回false
            while (n-- != 0) {
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

//判斷當前對象和StringBuffer 的對象sb內容是否相同
public boolean contentEquals(StringBuffer sb) {
    return contentEquals((CharSequence)sb);
}

//判斷當前對象的內容和實現了CharSequence 接口的cs對象的
//內容是否相同
public boolean contentEquals(CharSequence cs) {
    //判斷cs是否爲StringBuffer或StringBuilder
    if (cs instanceof AbstractStringBuilder) {
        //如果是StringBuffer(線程安全)需要先鎖住cs再判斷
        if (cs instanceof StringBuffer) {
            synchronized(cs) {
                return nonSyncContentEquals((AbstractStringBuilder)cs);
            }
        } else {
            //如果是StringBuilder(非線程安全)直接對cs進行判斷
            return nonSyncContentEquals((AbstractStringBuilder)cs);
        }
    }
    //如果是String對象 直接調用equals方法判斷
    if (cs instanceof String) {
        return equals(cs);
    }
    //如果是是一個通用的CharSequence對象,直接進行判斷
    char v1[] = value;
    int n = v1.length;
    if (n != cs.length()) {
        return false;
    }
    for (int i = 0; i < n; i++) {
        if (v1[i] != cs.charAt(i)) {
            return false;
        }
    }
    return true;
}

//非線程安全方式判斷sb對象和當前對象的內容是否相等
private boolean nonSyncContentEquals(AbstractStringBuilder sb) {
    char v1[] = value;
    char v2[] = sb.getValue();
    int n = v1.length;
    if (n != sb.length()) {
        return false;
    }
    for (int i = 0; i < n; i++) {
        if (v1[i] != v2[i]) {
            return false;
        }
    }
    return true;
}

//忽略大小寫判斷當前對象和anotherString的值是否相等
public boolean equalsIgnoreCase(String anotherString) {
    return (this == anotherString) ? true: (anotherString != null)
          && (anotherString.value.length == value.length)
          && regionMatches(true, 0, anotherString, 0, value.length);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章