热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

JDK源码之——AbstractStringBuilder

部分方法没有解释,有疑问或错误的地方,欢迎评论指出importsun.misc.FloatingDecimal;importjava.util.Arr

部分方法没有解释,有疑问或错误的地方,欢迎评论指出

import sun.misc.FloatingDecimal;import java.util.Arrays;ort javtil.Arrays;/*** StringBuilder 抽象类*/
abstract class AbstractStringBuilder implements Appendable, CharSequence {/*** 储存值的数组*/char[] value;/*** 容量大小*/int count;/*** 无参构造*/AbstractStringBuilder() {}/*** 根据传入长度构造 AbstractStringBuilder*/AbstractStringBuilder(int capacity) {value = new char[capacity];}/*** 返回字符串长度 (字符数)*/@Overridepublic int length() {return count;}/*** 返回当前容量*/public int capacity() {return value.length;}/*** 确保容量足够*/public void ensureCapacity(int minimumCapacity) {if (minimumCapacity > 0)ensureCapacityInternal(minimumCapacity);}// 判断传入容量是否超过容量 超过则扩容private void ensureCapacityInternal(int minimumCapacity) {if (minimumCapacity - value.length > 0)expandCapacity(minimumCapacity);}/*** 扩容*/void expandCapacity(int minimumCapacity) {//一次长度为length + 2int newCapacity = value.length * 2 + 2;//如果扩容一次还不足minimumCapacity,则取minimumCapacityif (newCapacity - minimumCapacity <0)newCapacity = minimumCapacity;//如果newCapacity还小于0 , 则取Integer所支持的最大值if (newCapacity <0) {if (minimumCapacity <0) // overflowthrow new OutOfMemoryError();newCapacity = Integer.MAX_VALUE;}//复制数组的值 到新的数组中,新的数组长度为newCapacityvalue = Arrays.copyOf(value, newCapacity);}/*** 尝试减少value所占用的存储空间。* 如果缓冲区 length 大于保持其当前序列所需的缓冲区字符 count ,* 则调整value的大小以提高空间效率*/public void trimToSize() {if (count = 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);}//在指定位置插入charpublic void setCharAt(int index, char ch) {if ((index <0) || (index >= count))throw new StringIndexOutOfBoundsException(index);value[index] = ch;}//appendpublic AbstractStringBuilder append(Object obj) {return append(String.valueOf(obj));}//append字符串,添加字符串public AbstractStringBuilder append(String str) {if (str == null)//添加空 则添加"null"return appendNull();int len = str.length();//确保容量ensureCapacityInternal(count + len);//复制数组str.getChars(0, len, value, count);//容量同步count += len;return this;}// Documentation in subclasses because of synchro differencepublic 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;}/*** 从1.8新增* @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@Overridepublic 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());}//添加 "null" countprivate AbstractStringBuilder appendNull() {int c = count;ensureCapacityInternal(c + 4);final char[] value = this.value;value[c++] = &#39;n&#39;;value[c++] = &#39;u&#39;;value[c++] = &#39;l&#39;;value[c++] = &#39;l&#39;;count = c;return this;}//添加 固定长度字符@Overridepublic AbstractStringBuilder append(CharSequence s, int start, int end) {if (s == null)s = "null";if ((start <0) || (start > end) || (end > s.length()))throw new IndexOutOfBoundsException("start " + start + ", end " + end + ", s.length() "+ s.length());int len = end - start;ensureCapacityInternal(count + len);for (int i = start, j = count; i 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;}/*** 删除指定位置的字符* @param index* @return*/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;}//字符替换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;}//字符串截取 开始位置到最后位置public String substring(int start) {return substring(start, count);}//字符串截取 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);}@Overridepublic abstract String toString();//返回char[]final char[] getValue() {return value;}}

 


推荐阅读
author-avatar
尚伦旺
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有