📄 appendingstringbuffer.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.wicket.util.string;import java.io.IOException;/** * This is a copy or combination of <code>java.lang.StringBuffer</code> and * <code>java.lang.String</code> It has a special method getValue() which returns the internal * char array. * * Hashcode and equals methods are also implemented. * * This AppendingStringBuffer is not synchronized. * * @author Johan Compagner * @see java.lang.StringBuffer */public final class AppendingStringBuffer implements java.io.Serializable, CharSequence{ /** use serialVersionUID from JDK 1.0.2 for interoperability */ static final long serialVersionUID = 1L; private static final AppendingStringBuffer NULL = new AppendingStringBuffer("null"); private static final StringBuffer SB_NULL = new StringBuffer("null"); /** * The value is used for character storage. * * @serial */ private char value[]; /** * The count is the number of characters in the buffer. * * @serial */ private int count; /** * Constructs a string buffer with no characters in it and an initial capacity of 16 characters. */ public AppendingStringBuffer() { this(16); } /** * Constructs a string buffer with no characters in it and an initial capacity specified by the * <code>length</code> argument. * * @param length * the initial capacity. * @exception NegativeArraySizeException * if the <code>length</code> argument is less than <code>0</code>. */ public AppendingStringBuffer(int length) { value = new char[length]; } /** * Constructs a string buffer so that it represents the same sequence of characters as the * string argument; in other words, the initial contents of the string buffer is a copy of the * argument string. The initial capacity of the string buffer is <code>16</code> plus the * length of the string argument. * * @param str * the initial contents of the buffer. * @exception NullPointerException * if <code>str</code> is <code>null</code> */ public AppendingStringBuffer(CharSequence str) { this(str.length() + 16); append(str); } /** * Returns the length (character count) of this string buffer. * * @return the length of the sequence of characters currently represented by this string buffer. */ public int length() { return count; } /** * Returns the current capacity of the String buffer. The capacity is the amount of storage * available for newly inserted characters; beyond which an allocation will occur. * * @return the current capacity of this string buffer. */ public int capacity() { return value.length; } /** * Ensures that the capacity of the buffer is at least equal to the specified minimum. If the * current capacity of this string buffer is less than the argument, then a new internal buffer * is allocated with greater capacity. The new capacity is the larger of: * <ul> * <li>The <code>minimumCapacity</code> argument. * <li>Twice the old capacity, plus <code>2</code>. * </ul> * If the <code>minimumCapacity</code> argument is nonpositive, this method takes no action * and simply returns. * * @param minimumCapacity * the minimum desired capacity. */ public void ensureCapacity(int minimumCapacity) { if (minimumCapacity > value.length) { expandCapacity(minimumCapacity); } } /** * This implements the expansion semantics of ensureCapacity but is unsynchronized for use * internally by methods which are already synchronized. * * @param minimumCapacity * * @see java.lang.StringBuffer#ensureCapacity(int) */ private void expandCapacity(int minimumCapacity) { int newCapacity = (value.length + 1) * 2; if (newCapacity < 0) { newCapacity = Integer.MAX_VALUE; } else if (minimumCapacity > newCapacity) { newCapacity = minimumCapacity; } char newValue[] = new char[newCapacity]; System.arraycopy(value, 0, newValue, 0, count); value = newValue; } /** * Sets the length of this String buffer. This string buffer is altered to represent a new * character sequence whose length is specified by the argument. For every nonnegative index * <i>k</i> less than <code>newLength</code>, the character at index <i>k</i> in the new * character sequence is the same as the character at index <i>k</i> in the old sequence if * <i>k</i> is less than the length of the old character sequence; otherwise, it is the null * character <code>'\u0000'</code>. * * In other words, if the <code>newLength</code> argument is less than the current length of * the string buffer, the string buffer is truncated to contain exactly the number of characters * given by the <code>newLength</code> argument. * <p> * If the <code>newLength</code> argument is greater than or equal to the current length, * sufficient null characters (<code>'\u0000'</code>) are appended to the string buffer * so that length becomes the <code>newLength</code> argument. * <p> * The <code>newLength</code> argument must be greater than or equal to <code>0</code>. * * @param newLength * the new length of the buffer. * @exception IndexOutOfBoundsException * if the <code>newLength</code> argument is negative. * @see java.lang.StringBuffer#length() */ public void setLength(int newLength) { if (newLength < 0) { throw new StringIndexOutOfBoundsException(newLength); } if (newLength > value.length) { expandCapacity(newLength); } if (count < newLength) { for (; count < newLength; count++) { value[count] = '\0'; } } else { count = newLength; } } /** * The specified character of the sequence currently represented by the string buffer, as * indicated by the <code>index</code> argument, is returned. The first character of a string * buffer is at index <code>0</code>, the next at index <code>1</code>, and so on, for * array indexing. * <p> * The index argument must be greater than or equal to <code>0</code>, and less than the * length of this string buffer. * * @param index * the index of the desired character. * @return the character at the specified index of this string buffer. * @exception IndexOutOfBoundsException * if <code>index</code> is negative or greater than or equal to * <code>length()</code>. * @see java.lang.StringBuffer#length() */ public char charAt(int index) { if ((index < 0) || (index >= count)) { throw new StringIndexOutOfBoundsException(index); } return value[index]; } /** * Characters are copied from this string buffer into the destination character array * <code>dst</code>. The first character to be copied is at index <code>srcBegin</code>; * the last character to be copied is at index <code>srcEnd-1</code>. The total number of * characters to be copied is <code>srcEnd-srcBegin</code>. The characters are copied into * the subarray of <code>dst</code> starting at index <code>dstBegin</code> and ending at * index: * <p> * <blockquote> * * <pre> * dstbegin + (srcEnd - srcBegin) - 1 * </pre> * * </blockquote> * * @param srcBegin * start copying at this offset in the string buffer. * @param srcEnd * stop copying at this offset in the string buffer. * @param dst * the array to copy the data into. * @param dstBegin * offset into <code>dst</code>. * @exception NullPointerException * if <code>dst</code> is <code>null</code>. * @exception IndexOutOfBoundsException * if any of the following is true: * <ul> * <li><code>srcBegin</code> is negative * <li><code>dstBegin</code> is negative * <li>the <code>srcBegin</code> argument is greater than the * <code>srcEnd</code> argument. * <li><code>srcEnd</code> is greater than <code>this.length()</code>, the * current length of this string buffer. * <li><code>dstBegin+srcEnd-srcBegin</code> is greater than * <code>dst.length</code> * </ul> */ 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); } /** * The character at the specified index of this string buffer is set to <code>ch</code>. The * string buffer is altered to represent a new character sequence that is identical to the old * character sequence, except that it contains the character <code>ch</code> at position * <code>index</code>. * <p> * The index argument must be greater than or equal to <code>0</code>, and less than the * length of this string buffer. * * @param index * the index of the character to modify. * @param ch * the new character. * @exception IndexOutOfBoundsException * if <code>index</code> is negative or greater than or equal to * <code>length()</code>. * @see java.lang.StringBuffer#length() */ public void setCharAt(int index, char ch) { if ((index < 0) || (index >= count)) { throw new StringIndexOutOfBoundsException(index); } value[index] = ch; } /** * Appends the string representation of the <code>Object</code> argument to this string * buffer. * <p> * The argument is converted to a string as if by the method <code>String.valueOf</code>, and * the characters of that string are then appended to this string buffer. * * @param obj * an <code>Object</code>. * @return a reference to this <code>AppendingStringBuffer</code> object. * @see java.lang.String#valueOf(java.lang.Object) * @see java.lang.StringBuffer#append(java.lang.String) */ public AppendingStringBuffer append(Object obj) { if (obj instanceof AppendingStringBuffer) { return append((AppendingStringBuffer)obj); } else if (obj instanceof StringBuffer) { return append((StringBuffer)obj); } return append(String.valueOf(obj)); } /** * Appends the string to this string buffer. * <p> * The characters of the <code>String</code> argument are appended, in order, to the contents * of this string buffer, increasing the length of this string buffer by the length of the * argument. If <code>str</code> is <code>null</code>, then the four characters * <code>"null"</code> are appended to this string buffer. * <p> * Let <i>n</i> be the length of the old character sequence, the one contained in the string * buffer just prior to execution of the <code>append</code> method. Then the character at * index <i>k</i> in the new character sequence is equal to the character at index <i>k</i> in * the old character sequence, if <i>k</i> is less than <i>n</i>; otherwise, it is equal to * the character at index <i>k-n</i> in the argument <code>str</code>. * * @param str * a string. * @return a reference to this <code>AppendingStringBuffer</code>. */ public AppendingStringBuffer append(String str) { if (str == null) { str = String.valueOf(str); } int len = str.length(); int newcount = count + len; if (newcount > value.length) { expandCapacity(newcount); } str.getChars(0, len, value, count); count = newcount; return this; } /** * Appends the specified <tt>AppendingStringBuffer</tt> to this <tt>AppendingStringBuffer</tt>. * <p> * The characters of the <tt>AppendingStringBuffer</tt> argument are appended, in order, to * the contents of this <tt>AppendingStringBuffer</tt>, increasing the length of this * <tt>AppendingStringBuffer</tt> by the length of the argument. If <tt>sb</tt> is * <tt>null</tt>, then the four characters <tt>"null"</tt> are appended to this * <tt>AppendingStringBuffer</tt>. * <p> * Let <i>n</i> be the length of the old character sequence, the one contained in the * <tt>AppendingStringBuffer</tt> just prior to execution of the <tt>append</tt> method. * Then the character at index <i>k</i> in the new character sequence is equal to the character * at index <i>k</i> in the old character sequence, if <i>k</i> is less than <i>n</i>; * otherwise, it is equal to the character at index <i>k-n</i> in the argument <code>sb</code>. * <p> * The method <tt>ensureCapacity</tt> is first called on this <tt>AppendingStringBuffer</tt> * with the new buffer length as its argument. (This ensures that the storage of this * <tt>AppendingStringBuffer</tt> is adequate to contain the additional characters being * appended.) * * @param sb * the <tt>AppendingStringBuffer</tt> to append. * @return a reference to this <tt>AppendingStringBuffer</tt>. * @since 1.4 */ public AppendingStringBuffer append(AppendingStringBuffer sb) { if (sb == null) { sb = NULL; } int len = sb.length(); int newcount = count + len; if (newcount > value.length) { expandCapacity(newcount); } sb.getChars(0, len, value, count); count = newcount; return this; } /** * Appends the specified <tt>AppendingStringBuffer</tt> to this <tt>AppendingStringBuffer</tt>. * <p> * The characters of the <tt>AppendingStringBuffer</tt> argument are appended, in order, to * the contents of this <tt>AppendingStringBuffer</tt>, increasing the length of this * <tt>AppendingStringBuffer</tt> by the length of the argument. If <tt>sb</tt> is * <tt>null</tt>, then the four characters <tt>"null"</tt> are appended to this * <tt>AppendingStringBuffer</tt>. * <p> * Let <i>n</i> be the length of the old character sequence, the one contained in the * <tt>AppendingStringBuffer</tt> just prior to execution of the <tt>append</tt> method.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -