Java 源码 - AbstractStringBuilder

2022/2/17 20:42:19

本文主要是介绍Java 源码 - AbstractStringBuilder,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

介绍

A mutable sequence of characters.

示例

xxxxxx

源码

abstract class AbstractStringBuilder implements Appendable, CharSequence {
  
  char[] value;

  int count;

  AbstractStringBuilder(int capacity) {
    value = new char[capacity];
  }

  public int capacity() {
    return value.length;
  }

  public AbstractStringBuilder append(String str) {
    if (str == null)
      return appendNull();
    int len = str.length();
    ensureCapacityInternal(count + len);
    str.getChars(0, len, value, count);
    count += len;
    return this;
  }
  
  private AbstractStringBuilder appendNull() {
    int c = count;
    ensureCapacityInternal(c + 4);
    final char[] value = this.value;
    value[c++] = 'n';
    value[c++] = 'u';
    value[c++] = 'l';
    value[c++] = 'l';
    count = c;
    return this;
  }  
  
  public void ensureCapacity(int minimumCapacity) {
    if (minimumCapacity > 0)
      ensureCapacityInternal(minimumCapacity);
  }

  private void ensureCapacityInternal(int minimumCapacity) {
    if (minimumCapacity - value.length > 0) {
      value = Arrays.copyOf(value,
          newCapacity(minimumCapacity));
    }
  }

  private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

  /**
   * Returns a capacity at least as large as the given minimum capacity.
   */
  private int newCapacity(int minCapacity) {
    int newCapacity = (value.length << 1) + 2;
    if (newCapacity - minCapacity < 0) {
      newCapacity = minCapacity;
    }
    return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)
      ? hugeCapacity(minCapacity)
      : newCapacity;
  }

  private int hugeCapacity(int minCapacity) {
    if (Integer.MAX_VALUE - minCapacity < 0) { // overflow
      throw new OutOfMemoryError();
    }
    return (minCapacity > MAX_ARRAY_SIZE)
      ? minCapacity : MAX_ARRAY_SIZE;
  }

  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 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;
  }

  @Override
  public abstract String toString();

  final char[] getValue() {
    return value;
  }
} 


这篇关于Java 源码 - AbstractStringBuilder的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程