PageRenderTime 71ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/java-1.7.0-openjdk/openjdk/jdk/src/share/classes/java/lang/AbstractStringBuilder.java

#
Java | 1407 lines | 413 code | 64 blank | 930 comment | 97 complexity | 89544a81813cf26c45c3b6e5ef37c94c MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package java.lang;
  26. import sun.misc.FloatingDecimal;
  27. import java.util.Arrays;
  28. /**
  29. * A mutable sequence of characters.
  30. * <p>
  31. * Implements a modifiable string. At any point in time it contains some
  32. * particular sequence of characters, but the length and content of the
  33. * sequence can be changed through certain method calls.
  34. *
  35. * @author Michael McCloskey
  36. * @author Martin Buchholz
  37. * @author Ulf Zibis
  38. * @since 1.5
  39. */
  40. abstract class AbstractStringBuilder implements Appendable, CharSequence {
  41. /**
  42. * The value is used for character storage.
  43. */
  44. char[] value;
  45. /**
  46. * The count is the number of characters used.
  47. */
  48. int count;
  49. /**
  50. * This no-arg constructor is necessary for serialization of subclasses.
  51. */
  52. AbstractStringBuilder() {
  53. }
  54. /**
  55. * Creates an AbstractStringBuilder of the specified capacity.
  56. */
  57. AbstractStringBuilder(int capacity) {
  58. value = new char[capacity];
  59. }
  60. /**
  61. * Returns the length (character count).
  62. *
  63. * @return the length of the sequence of characters currently
  64. * represented by this object
  65. */
  66. public int length() {
  67. return count;
  68. }
  69. /**
  70. * Returns the current capacity. The capacity is the amount of storage
  71. * available for newly inserted characters, beyond which an allocation
  72. * will occur.
  73. *
  74. * @return the current capacity
  75. */
  76. public int capacity() {
  77. return value.length;
  78. }
  79. /**
  80. * Ensures that the capacity is at least equal to the specified minimum.
  81. * If the current capacity is less than the argument, then a new internal
  82. * array is allocated with greater capacity. The new capacity is the
  83. * larger of:
  84. * <ul>
  85. * <li>The <code>minimumCapacity</code> argument.
  86. * <li>Twice the old capacity, plus <code>2</code>.
  87. * </ul>
  88. * If the <code>minimumCapacity</code> argument is nonpositive, this
  89. * method takes no action and simply returns.
  90. *
  91. * @param minimumCapacity the minimum desired capacity.
  92. */
  93. public void ensureCapacity(int minimumCapacity) {
  94. if (minimumCapacity > 0)
  95. ensureCapacityInternal(minimumCapacity);
  96. }
  97. /**
  98. * This method has the same contract as ensureCapacity, but is
  99. * never synchronized.
  100. */
  101. private void ensureCapacityInternal(int minimumCapacity) {
  102. // overflow-conscious code
  103. if (minimumCapacity - value.length > 0)
  104. expandCapacity(minimumCapacity);
  105. }
  106. /**
  107. * This implements the expansion semantics of ensureCapacity with no
  108. * size check or synchronization.
  109. */
  110. void expandCapacity(int minimumCapacity) {
  111. int newCapacity = value.length * 2 + 2;
  112. if (newCapacity - minimumCapacity < 0)
  113. newCapacity = minimumCapacity;
  114. if (newCapacity < 0) {
  115. if (minimumCapacity < 0) // overflow
  116. throw new OutOfMemoryError();
  117. newCapacity = Integer.MAX_VALUE;
  118. }
  119. value = Arrays.copyOf(value, newCapacity);
  120. }
  121. /**
  122. * Attempts to reduce storage used for the character sequence.
  123. * If the buffer is larger than necessary to hold its current sequence of
  124. * characters, then it may be resized to become more space efficient.
  125. * Calling this method may, but is not required to, affect the value
  126. * returned by a subsequent call to the {@link #capacity()} method.
  127. */
  128. public void trimToSize() {
  129. if (count < value.length) {
  130. value = Arrays.copyOf(value, count);
  131. }
  132. }
  133. /**
  134. * Sets the length of the character sequence.
  135. * The sequence is changed to a new character sequence
  136. * whose length is specified by the argument. For every nonnegative
  137. * index <i>k</i> less than <code>newLength</code>, the character at
  138. * index <i>k</i> in the new character sequence is the same as the
  139. * character at index <i>k</i> in the old sequence if <i>k</i> is less
  140. * than the length of the old character sequence; otherwise, it is the
  141. * null character <code>'&#92;u0000'</code>.
  142. *
  143. * In other words, if the <code>newLength</code> argument is less than
  144. * the current length, the length is changed to the specified length.
  145. * <p>
  146. * If the <code>newLength</code> argument is greater than or equal
  147. * to the current length, sufficient null characters
  148. * (<code>'&#92;u0000'</code>) are appended so that
  149. * length becomes the <code>newLength</code> argument.
  150. * <p>
  151. * The <code>newLength</code> argument must be greater than or equal
  152. * to <code>0</code>.
  153. *
  154. * @param newLength the new length
  155. * @throws IndexOutOfBoundsException if the
  156. * <code>newLength</code> argument is negative.
  157. */
  158. public void setLength(int newLength) {
  159. if (newLength < 0)
  160. throw new StringIndexOutOfBoundsException(newLength);
  161. ensureCapacityInternal(newLength);
  162. if (count < newLength) {
  163. for (; count < newLength; count++)
  164. value[count] = '\0';
  165. } else {
  166. count = newLength;
  167. }
  168. }
  169. /**
  170. * Returns the <code>char</code> value in this sequence at the specified index.
  171. * The first <code>char</code> value is at index <code>0</code>, the next at index
  172. * <code>1</code>, and so on, as in array indexing.
  173. * <p>
  174. * The index argument must be greater than or equal to
  175. * <code>0</code>, and less than the length of this sequence.
  176. *
  177. * <p>If the <code>char</code> value specified by the index is a
  178. * <a href="Character.html#unicode">surrogate</a>, the surrogate
  179. * value is returned.
  180. *
  181. * @param index the index of the desired <code>char</code> value.
  182. * @return the <code>char</code> value at the specified index.
  183. * @throws IndexOutOfBoundsException if <code>index</code> is
  184. * negative or greater than or equal to <code>length()</code>.
  185. */
  186. public char charAt(int index) {
  187. if ((index < 0) || (index >= count))
  188. throw new StringIndexOutOfBoundsException(index);
  189. return value[index];
  190. }
  191. /**
  192. * Returns the character (Unicode code point) at the specified
  193. * index. The index refers to <code>char</code> values
  194. * (Unicode code units) and ranges from <code>0</code> to
  195. * {@link #length()}<code> - 1</code>.
  196. *
  197. * <p> If the <code>char</code> value specified at the given index
  198. * is in the high-surrogate range, the following index is less
  199. * than the length of this sequence, and the
  200. * <code>char</code> value at the following index is in the
  201. * low-surrogate range, then the supplementary code point
  202. * corresponding to this surrogate pair is returned. Otherwise,
  203. * the <code>char</code> value at the given index is returned.
  204. *
  205. * @param index the index to the <code>char</code> values
  206. * @return the code point value of the character at the
  207. * <code>index</code>
  208. * @exception IndexOutOfBoundsException if the <code>index</code>
  209. * argument is negative or not less than the length of this
  210. * sequence.
  211. */
  212. public int codePointAt(int index) {
  213. if ((index < 0) || (index >= count)) {
  214. throw new StringIndexOutOfBoundsException(index);
  215. }
  216. return Character.codePointAt(value, index);
  217. }
  218. /**
  219. * Returns the character (Unicode code point) before the specified
  220. * index. The index refers to <code>char</code> values
  221. * (Unicode code units) and ranges from <code>1</code> to {@link
  222. * #length()}.
  223. *
  224. * <p> If the <code>char</code> value at <code>(index - 1)</code>
  225. * is in the low-surrogate range, <code>(index - 2)</code> is not
  226. * negative, and the <code>char</code> value at <code>(index -
  227. * 2)</code> is in the high-surrogate range, then the
  228. * supplementary code point value of the surrogate pair is
  229. * returned. If the <code>char</code> value at <code>index -
  230. * 1</code> is an unpaired low-surrogate or a high-surrogate, the
  231. * surrogate value is returned.
  232. *
  233. * @param index the index following the code point that should be returned
  234. * @return the Unicode code point value before the given index.
  235. * @exception IndexOutOfBoundsException if the <code>index</code>
  236. * argument is less than 1 or greater than the length
  237. * of this sequence.
  238. */
  239. public int codePointBefore(int index) {
  240. int i = index - 1;
  241. if ((i < 0) || (i >= count)) {
  242. throw new StringIndexOutOfBoundsException(index);
  243. }
  244. return Character.codePointBefore(value, index);
  245. }
  246. /**
  247. * Returns the number of Unicode code points in the specified text
  248. * range of this sequence. The text range begins at the specified
  249. * <code>beginIndex</code> and extends to the <code>char</code> at
  250. * index <code>endIndex - 1</code>. Thus the length (in
  251. * <code>char</code>s) of the text range is
  252. * <code>endIndex-beginIndex</code>. Unpaired surrogates within
  253. * this sequence count as one code point each.
  254. *
  255. * @param beginIndex the index to the first <code>char</code> of
  256. * the text range.
  257. * @param endIndex the index after the last <code>char</code> of
  258. * the text range.
  259. * @return the number of Unicode code points in the specified text
  260. * range
  261. * @exception IndexOutOfBoundsException if the
  262. * <code>beginIndex</code> is negative, or <code>endIndex</code>
  263. * is larger than the length of this sequence, or
  264. * <code>beginIndex</code> is larger than <code>endIndex</code>.
  265. */
  266. public int codePointCount(int beginIndex, int endIndex) {
  267. if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
  268. throw new IndexOutOfBoundsException();
  269. }
  270. return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex);
  271. }
  272. /**
  273. * Returns the index within this sequence that is offset from the
  274. * given <code>index</code> by <code>codePointOffset</code> code
  275. * points. Unpaired surrogates within the text range given by
  276. * <code>index</code> and <code>codePointOffset</code> count as
  277. * one code point each.
  278. *
  279. * @param index the index to be offset
  280. * @param codePointOffset the offset in code points
  281. * @return the index within this sequence
  282. * @exception IndexOutOfBoundsException if <code>index</code>
  283. * is negative or larger then the length of this sequence,
  284. * or if <code>codePointOffset</code> is positive and the subsequence
  285. * starting with <code>index</code> has fewer than
  286. * <code>codePointOffset</code> code points,
  287. * or if <code>codePointOffset</code> is negative and the subsequence
  288. * before <code>index</code> has fewer than the absolute value of
  289. * <code>codePointOffset</code> code points.
  290. */
  291. public int offsetByCodePoints(int index, int codePointOffset) {
  292. if (index < 0 || index > count) {
  293. throw new IndexOutOfBoundsException();
  294. }
  295. return Character.offsetByCodePointsImpl(value, 0, count,
  296. index, codePointOffset);
  297. }
  298. /**
  299. * Characters are copied from this sequence into the
  300. * destination character array <code>dst</code>. The first character to
  301. * be copied is at index <code>srcBegin</code>; the last character to
  302. * be copied is at index <code>srcEnd-1</code>. The total number of
  303. * characters to be copied is <code>srcEnd-srcBegin</code>. The
  304. * characters are copied into the subarray of <code>dst</code> starting
  305. * at index <code>dstBegin</code> and ending at index:
  306. * <p><blockquote><pre>
  307. * dstbegin + (srcEnd-srcBegin) - 1
  308. * </pre></blockquote>
  309. *
  310. * @param srcBegin start copying at this offset.
  311. * @param srcEnd stop copying at this offset.
  312. * @param dst the array to copy the data into.
  313. * @param dstBegin offset into <code>dst</code>.
  314. * @throws NullPointerException if <code>dst</code> is
  315. * <code>null</code>.
  316. * @throws IndexOutOfBoundsException if any of the following is true:
  317. * <ul>
  318. * <li><code>srcBegin</code> is negative
  319. * <li><code>dstBegin</code> is negative
  320. * <li>the <code>srcBegin</code> argument is greater than
  321. * the <code>srcEnd</code> argument.
  322. * <li><code>srcEnd</code> is greater than
  323. * <code>this.length()</code>.
  324. * <li><code>dstBegin+srcEnd-srcBegin</code> is greater than
  325. * <code>dst.length</code>
  326. * </ul>
  327. */
  328. public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
  329. {
  330. if (srcBegin < 0)
  331. throw new StringIndexOutOfBoundsException(srcBegin);
  332. if ((srcEnd < 0) || (srcEnd > count))
  333. throw new StringIndexOutOfBoundsException(srcEnd);
  334. if (srcBegin > srcEnd)
  335. throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
  336. System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
  337. }
  338. /**
  339. * The character at the specified index is set to <code>ch</code>. This
  340. * sequence is altered to represent a new character sequence that is
  341. * identical to the old character sequence, except that it contains the
  342. * character <code>ch</code> at position <code>index</code>.
  343. * <p>
  344. * The index argument must be greater than or equal to
  345. * <code>0</code>, and less than the length of this sequence.
  346. *
  347. * @param index the index of the character to modify.
  348. * @param ch the new character.
  349. * @throws IndexOutOfBoundsException if <code>index</code> is
  350. * negative or greater than or equal to <code>length()</code>.
  351. */
  352. public void setCharAt(int index, char ch) {
  353. if ((index < 0) || (index >= count))
  354. throw new StringIndexOutOfBoundsException(index);
  355. value[index] = ch;
  356. }
  357. /**
  358. * Appends the string representation of the {@code Object} argument.
  359. * <p>
  360. * The overall effect is exactly as if the argument were converted
  361. * to a string by the method {@link String#valueOf(Object)},
  362. * and the characters of that string were then
  363. * {@link #append(String) appended} to this character sequence.
  364. *
  365. * @param obj an {@code Object}.
  366. * @return a reference to this object.
  367. */
  368. public AbstractStringBuilder append(Object obj) {
  369. return append(String.valueOf(obj));
  370. }
  371. /**
  372. * Appends the specified string to this character sequence.
  373. * <p>
  374. * The characters of the {@code String} argument are appended, in
  375. * order, increasing the length of this sequence by the length of the
  376. * argument. If {@code str} is {@code null}, then the four
  377. * characters {@code "null"} are appended.
  378. * <p>
  379. * Let <i>n</i> be the length of this character sequence just prior to
  380. * execution of the {@code append} method. Then the character at
  381. * index <i>k</i> in the new character sequence is equal to the character
  382. * at index <i>k</i> in the old character sequence, if <i>k</i> is less
  383. * than <i>n</i>; otherwise, it is equal to the character at index
  384. * <i>k-n</i> in the argument {@code str}.
  385. *
  386. * @param str a string.
  387. * @return a reference to this object.
  388. */
  389. public AbstractStringBuilder append(String str) {
  390. if (str == null) str = "null";
  391. int len = str.length();
  392. ensureCapacityInternal(count + len);
  393. str.getChars(0, len, value, count);
  394. count += len;
  395. return this;
  396. }
  397. // Documentation in subclasses because of synchro difference
  398. public AbstractStringBuilder append(StringBuffer sb) {
  399. if (sb == null)
  400. return append("null");
  401. int len = sb.length();
  402. ensureCapacityInternal(count + len);
  403. sb.getChars(0, len, value, count);
  404. count += len;
  405. return this;
  406. }
  407. // Documentation in subclasses because of synchro difference
  408. public AbstractStringBuilder append(CharSequence s) {
  409. if (s == null)
  410. s = "null";
  411. if (s instanceof String)
  412. return this.append((String)s);
  413. if (s instanceof StringBuffer)
  414. return this.append((StringBuffer)s);
  415. return this.append(s, 0, s.length());
  416. }
  417. /**
  418. * Appends a subsequence of the specified {@code CharSequence} to this
  419. * sequence.
  420. * <p>
  421. * Characters of the argument {@code s}, starting at
  422. * index {@code start}, are appended, in order, to the contents of
  423. * this sequence up to the (exclusive) index {@code end}. The length
  424. * of this sequence is increased by the value of {@code end - start}.
  425. * <p>
  426. * Let <i>n</i> be the length of this character sequence just prior to
  427. * execution of the {@code append} method. Then the character at
  428. * index <i>k</i> in this character sequence becomes equal to the
  429. * character at index <i>k</i> in this sequence, if <i>k</i> is less than
  430. * <i>n</i>; otherwise, it is equal to the character at index
  431. * <i>k+start-n</i> in the argument {@code s}.
  432. * <p>
  433. * If {@code s} is {@code null}, then this method appends
  434. * characters as if the s parameter was a sequence containing the four
  435. * characters {@code "null"}.
  436. *
  437. * @param s the sequence to append.
  438. * @param start the starting index of the subsequence to be appended.
  439. * @param end the end index of the subsequence to be appended.
  440. * @return a reference to this object.
  441. * @throws IndexOutOfBoundsException if
  442. * {@code start} is negative, or
  443. * {@code start} is greater than {@code end} or
  444. * {@code end} is greater than {@code s.length()}
  445. */
  446. public AbstractStringBuilder append(CharSequence s, int start, int end) {
  447. if (s == null)
  448. s = "null";
  449. if ((start < 0) || (start > end) || (end > s.length()))
  450. throw new IndexOutOfBoundsException(
  451. "start " + start + ", end " + end + ", s.length() "
  452. + s.length());
  453. int len = end - start;
  454. ensureCapacityInternal(count + len);
  455. for (int i = start, j = count; i < end; i++, j++)
  456. value[j] = s.charAt(i);
  457. count += len;
  458. return this;
  459. }
  460. /**
  461. * Appends the string representation of the {@code char} array
  462. * argument to this sequence.
  463. * <p>
  464. * The characters of the array argument are appended, in order, to
  465. * the contents of this sequence. The length of this sequence
  466. * increases by the length of the argument.
  467. * <p>
  468. * The overall effect is exactly as if the argument were converted
  469. * to a string by the method {@link String#valueOf(char[])},
  470. * and the characters of that string were then
  471. * {@link #append(String) appended} to this character sequence.
  472. *
  473. * @param str the characters to be appended.
  474. * @return a reference to this object.
  475. */
  476. public AbstractStringBuilder append(char[] str) {
  477. int len = str.length;
  478. ensureCapacityInternal(count + len);
  479. System.arraycopy(str, 0, value, count, len);
  480. count += len;
  481. return this;
  482. }
  483. /**
  484. * Appends the string representation of a subarray of the
  485. * {@code char} array argument to this sequence.
  486. * <p>
  487. * Characters of the {@code char} array {@code str}, starting at
  488. * index {@code offset}, are appended, in order, to the contents
  489. * of this sequence. The length of this sequence increases
  490. * by the value of {@code len}.
  491. * <p>
  492. * The overall effect is exactly as if the arguments were converted
  493. * to a string by the method {@link String#valueOf(char[],int,int)},
  494. * and the characters of that string were then
  495. * {@link #append(String) appended} to this character sequence.
  496. *
  497. * @param str the characters to be appended.
  498. * @param offset the index of the first {@code char} to append.
  499. * @param len the number of {@code char}s to append.
  500. * @return a reference to this object.
  501. * @throws IndexOutOfBoundsException
  502. * if {@code offset < 0} or {@code len < 0}
  503. * or {@code offset+len > str.length}
  504. */
  505. public AbstractStringBuilder append(char str[], int offset, int len) {
  506. if (len > 0) // let arraycopy report AIOOBE for len < 0
  507. ensureCapacityInternal(count + len);
  508. System.arraycopy(str, offset, value, count, len);
  509. count += len;
  510. return this;
  511. }
  512. /**
  513. * Appends the string representation of the {@code boolean}
  514. * argument to the sequence.
  515. * <p>
  516. * The overall effect is exactly as if the argument were converted
  517. * to a string by the method {@link String#valueOf(boolean)},
  518. * and the characters of that string were then
  519. * {@link #append(String) appended} to this character sequence.
  520. *
  521. * @param b a {@code boolean}.
  522. * @return a reference to this object.
  523. */
  524. public AbstractStringBuilder append(boolean b) {
  525. if (b) {
  526. ensureCapacityInternal(count + 4);
  527. value[count++] = 't';
  528. value[count++] = 'r';
  529. value[count++] = 'u';
  530. value[count++] = 'e';
  531. } else {
  532. ensureCapacityInternal(count + 5);
  533. value[count++] = 'f';
  534. value[count++] = 'a';
  535. value[count++] = 'l';
  536. value[count++] = 's';
  537. value[count++] = 'e';
  538. }
  539. return this;
  540. }
  541. /**
  542. * Appends the string representation of the {@code char}
  543. * argument to this sequence.
  544. * <p>
  545. * The argument is appended to the contents of this sequence.
  546. * The length of this sequence increases by {@code 1}.
  547. * <p>
  548. * The overall effect is exactly as if the argument were converted
  549. * to a string by the method {@link String#valueOf(char)},
  550. * and the character in that string were then
  551. * {@link #append(String) appended} to this character sequence.
  552. *
  553. * @param c a {@code char}.
  554. * @return a reference to this object.
  555. */
  556. public AbstractStringBuilder append(char c) {
  557. ensureCapacityInternal(count + 1);
  558. value[count++] = c;
  559. return this;
  560. }
  561. /**
  562. * Appends the string representation of the {@code int}
  563. * argument to this sequence.
  564. * <p>
  565. * The overall effect is exactly as if the argument were converted
  566. * to a string by the method {@link String#valueOf(int)},
  567. * and the characters of that string were then
  568. * {@link #append(String) appended} to this character sequence.
  569. *
  570. * @param i an {@code int}.
  571. * @return a reference to this object.
  572. */
  573. public AbstractStringBuilder append(int i) {
  574. if (i == Integer.MIN_VALUE) {
  575. append("-2147483648");
  576. return this;
  577. }
  578. int appendedLength = (i < 0) ? Integer.stringSize(-i) + 1
  579. : Integer.stringSize(i);
  580. int spaceNeeded = count + appendedLength;
  581. ensureCapacityInternal(spaceNeeded);
  582. Integer.getChars(i, spaceNeeded, value);
  583. count = spaceNeeded;
  584. return this;
  585. }
  586. /**
  587. * Appends the string representation of the {@code long}
  588. * argument to this sequence.
  589. * <p>
  590. * The overall effect is exactly as if the argument were converted
  591. * to a string by the method {@link String#valueOf(long)},
  592. * and the characters of that string were then
  593. * {@link #append(String) appended} to this character sequence.
  594. *
  595. * @param l a {@code long}.
  596. * @return a reference to this object.
  597. */
  598. public AbstractStringBuilder append(long l) {
  599. if (l == Long.MIN_VALUE) {
  600. append("-9223372036854775808");
  601. return this;
  602. }
  603. int appendedLength = (l < 0) ? Long.stringSize(-l) + 1
  604. : Long.stringSize(l);
  605. int spaceNeeded = count + appendedLength;
  606. ensureCapacityInternal(spaceNeeded);
  607. Long.getChars(l, spaceNeeded, value);
  608. count = spaceNeeded;
  609. return this;
  610. }
  611. /**
  612. * Appends the string representation of the {@code float}
  613. * argument to this sequence.
  614. * <p>
  615. * The overall effect is exactly as if the argument were converted
  616. * to a string by the method {@link String#valueOf(float)},
  617. * and the characters of that string were then
  618. * {@link #append(String) appended} to this character sequence.
  619. *
  620. * @param f a {@code float}.
  621. * @return a reference to this object.
  622. */
  623. public AbstractStringBuilder append(float f) {
  624. new FloatingDecimal(f).appendTo(this);
  625. return this;
  626. }
  627. /**
  628. * Appends the string representation of the {@code double}
  629. * argument to this sequence.
  630. * <p>
  631. * The overall effect is exactly as if the argument were converted
  632. * to a string by the method {@link String#valueOf(double)},
  633. * and the characters of that string were then
  634. * {@link #append(String) appended} to this character sequence.
  635. *
  636. * @param d a {@code double}.
  637. * @return a reference to this object.
  638. */
  639. public AbstractStringBuilder append(double d) {
  640. new FloatingDecimal(d).appendTo(this);
  641. return this;
  642. }
  643. /**
  644. * Removes the characters in a substring of this sequence.
  645. * The substring begins at the specified {@code start} and extends to
  646. * the character at index {@code end - 1} or to the end of the
  647. * sequence if no such character exists. If
  648. * {@code start} is equal to {@code end}, no changes are made.
  649. *
  650. * @param start The beginning index, inclusive.
  651. * @param end The ending index, exclusive.
  652. * @return This object.
  653. * @throws StringIndexOutOfBoundsException if {@code start}
  654. * is negative, greater than {@code length()}, or
  655. * greater than {@code end}.
  656. */
  657. public AbstractStringBuilder delete(int start, int end) {
  658. if (start < 0)
  659. throw new StringIndexOutOfBoundsException(start);
  660. if (end > count)
  661. end = count;
  662. if (start > end)
  663. throw new StringIndexOutOfBoundsException();
  664. int len = end - start;
  665. if (len > 0) {
  666. System.arraycopy(value, start+len, value, start, count-end);
  667. count -= len;
  668. }
  669. return this;
  670. }
  671. /**
  672. * Appends the string representation of the {@code codePoint}
  673. * argument to this sequence.
  674. *
  675. * <p> The argument is appended to the contents of this sequence.
  676. * The length of this sequence increases by
  677. * {@link Character#charCount(int) Character.charCount(codePoint)}.
  678. *
  679. * <p> The overall effect is exactly as if the argument were
  680. * converted to a {@code char} array by the method
  681. * {@link Character#toChars(int)} and the character in that array
  682. * were then {@link #append(char[]) appended} to this character
  683. * sequence.
  684. *
  685. * @param codePoint a Unicode code point
  686. * @return a reference to this object.
  687. * @exception IllegalArgumentException if the specified
  688. * {@code codePoint} isn't a valid Unicode code point
  689. */
  690. public AbstractStringBuilder appendCodePoint(int codePoint) {
  691. final int count = this.count;
  692. if (Character.isBmpCodePoint(codePoint)) {
  693. ensureCapacityInternal(count + 1);
  694. value[count] = (char) codePoint;
  695. this.count = count + 1;
  696. } else if (Character.isValidCodePoint(codePoint)) {
  697. ensureCapacityInternal(count + 2);
  698. Character.toSurrogates(codePoint, value, count);
  699. this.count = count + 2;
  700. } else {
  701. throw new IllegalArgumentException();
  702. }
  703. return this;
  704. }
  705. /**
  706. * Removes the <code>char</code> at the specified position in this
  707. * sequence. This sequence is shortened by one <code>char</code>.
  708. *
  709. * <p>Note: If the character at the given index is a supplementary
  710. * character, this method does not remove the entire character. If
  711. * correct handling of supplementary characters is required,
  712. * determine the number of <code>char</code>s to remove by calling
  713. * <code>Character.charCount(thisSequence.codePointAt(index))</code>,
  714. * where <code>thisSequence</code> is this sequence.
  715. *
  716. * @param index Index of <code>char</code> to remove
  717. * @return This object.
  718. * @throws StringIndexOutOfBoundsException if the <code>index</code>
  719. * is negative or greater than or equal to
  720. * <code>length()</code>.
  721. */
  722. public AbstractStringBuilder deleteCharAt(int index) {
  723. if ((index < 0) || (index >= count))
  724. throw new StringIndexOutOfBoundsException(index);
  725. System.arraycopy(value, index+1, value, index, count-index-1);
  726. count--;
  727. return this;
  728. }
  729. /**
  730. * Replaces the characters in a substring of this sequence
  731. * with characters in the specified <code>String</code>. The substring
  732. * begins at the specified <code>start</code> and extends to the character
  733. * at index <code>end - 1</code> or to the end of the
  734. * sequence if no such character exists. First the
  735. * characters in the substring are removed and then the specified
  736. * <code>String</code> is inserted at <code>start</code>. (This
  737. * sequence will be lengthened to accommodate the
  738. * specified String if necessary.)
  739. *
  740. * @param start The beginning index, inclusive.
  741. * @param end The ending index, exclusive.
  742. * @param str String that will replace previous contents.
  743. * @return This object.
  744. * @throws StringIndexOutOfBoundsException if <code>start</code>
  745. * is negative, greater than <code>length()</code>, or
  746. * greater than <code>end</code>.
  747. */
  748. public AbstractStringBuilder replace(int start, int end, String str) {
  749. if (start < 0)
  750. throw new StringIndexOutOfBoundsException(start);
  751. if (start > count)
  752. throw new StringIndexOutOfBoundsException("start > length()");
  753. if (start > end)
  754. throw new StringIndexOutOfBoundsException("start > end");
  755. if (end > count)
  756. end = count;
  757. int len = str.length();
  758. int newCount = count + len - (end - start);
  759. ensureCapacityInternal(newCount);
  760. System.arraycopy(value, end, value, start + len, count - end);
  761. str.getChars(value, start);
  762. count = newCount;
  763. return this;
  764. }
  765. /**
  766. * Returns a new <code>String</code> that contains a subsequence of
  767. * characters currently contained in this character sequence. The
  768. * substring begins at the specified index and extends to the end of
  769. * this sequence.
  770. *
  771. * @param start The beginning index, inclusive.
  772. * @return The new string.
  773. * @throws StringIndexOutOfBoundsException if <code>start</code> is
  774. * less than zero, or greater than the length of this object.
  775. */
  776. public String substring(int start) {
  777. return substring(start, count);
  778. }
  779. /**
  780. * Returns a new character sequence that is a subsequence of this sequence.
  781. *
  782. * <p> An invocation of this method of the form
  783. *
  784. * <blockquote><pre>
  785. * sb.subSequence(begin,&nbsp;end)</pre></blockquote>
  786. *
  787. * behaves in exactly the same way as the invocation
  788. *
  789. * <blockquote><pre>
  790. * sb.substring(begin,&nbsp;end)</pre></blockquote>
  791. *
  792. * This method is provided so that this class can
  793. * implement the {@link CharSequence} interface. </p>
  794. *
  795. * @param start the start index, inclusive.
  796. * @param end the end index, exclusive.
  797. * @return the specified subsequence.
  798. *
  799. * @throws IndexOutOfBoundsException
  800. * if <tt>start</tt> or <tt>end</tt> are negative,
  801. * if <tt>end</tt> is greater than <tt>length()</tt>,
  802. * or if <tt>start</tt> is greater than <tt>end</tt>
  803. * @spec JSR-51
  804. */
  805. public CharSequence subSequence(int start, int end) {
  806. return substring(start, end);
  807. }
  808. /**
  809. * Returns a new <code>String</code> that contains a subsequence of
  810. * characters currently contained in this sequence. The
  811. * substring begins at the specified <code>start</code> and
  812. * extends to the character at index <code>end - 1</code>.
  813. *
  814. * @param start The beginning index, inclusive.
  815. * @param end The ending index, exclusive.
  816. * @return The new string.
  817. * @throws StringIndexOutOfBoundsException if <code>start</code>
  818. * or <code>end</code> are negative or greater than
  819. * <code>length()</code>, or <code>start</code> is
  820. * greater than <code>end</code>.
  821. */
  822. public String substring(int start, int end) {
  823. if (start < 0)
  824. throw new StringIndexOutOfBoundsException(start);
  825. if (end > count)
  826. throw new StringIndexOutOfBoundsException(end);
  827. if (start > end)
  828. throw new StringIndexOutOfBoundsException(end - start);
  829. return new String(value, start, end - start);
  830. }
  831. /**
  832. * Inserts the string representation of a subarray of the {@code str}
  833. * array argument into this sequence. The subarray begins at the
  834. * specified {@code offset} and extends {@code len} {@code char}s.
  835. * The characters of the subarray are inserted into this sequence at
  836. * the position indicated by {@code index}. The length of this
  837. * sequence increases by {@code len} {@code char}s.
  838. *
  839. * @param index position at which to insert subarray.
  840. * @param str A {@code char} array.
  841. * @param offset the index of the first {@code char} in subarray to
  842. * be inserted.
  843. * @param len the number of {@code char}s in the subarray to
  844. * be inserted.
  845. * @return This object
  846. * @throws StringIndexOutOfBoundsException if {@code index}
  847. * is negative or greater than {@code length()}, or
  848. * {@code offset} or {@code len} are negative, or
  849. * {@code (offset+len)} is greater than
  850. * {@code str.length}.
  851. */
  852. public AbstractStringBuilder insert(int index, char[] str, int offset,
  853. int len)
  854. {
  855. if ((index < 0) || (index > length()))
  856. throw new StringIndexOutOfBoundsException(index);
  857. if ((offset < 0) || (len < 0) || (offset > str.length - len))
  858. throw new StringIndexOutOfBoundsException(
  859. "offset " + offset + ", len " + len + ", str.length "
  860. + str.length);
  861. ensureCapacityInternal(count + len);
  862. System.arraycopy(value, index, value, index + len, count - index);
  863. System.arraycopy(str, offset, value, index, len);
  864. count += len;
  865. return this;
  866. }
  867. /**
  868. * Inserts the string representation of the {@code Object}
  869. * argument into this character sequence.
  870. * <p>
  871. * The overall effect is exactly as if the second argument were
  872. * converted to a string by the method {@link String#valueOf(Object)},
  873. * and the characters of that string were then
  874. * {@link #insert(int,String) inserted} into this character
  875. * sequence at the indicated offset.
  876. * <p>
  877. * The {@code offset} argument must be greater than or equal to
  878. * {@code 0}, and less than or equal to the {@linkplain #length() length}
  879. * of this sequence.
  880. *
  881. * @param offset the offset.
  882. * @param obj an {@code Object}.
  883. * @return a reference to this object.
  884. * @throws StringIndexOutOfBoundsException if the offset is invalid.
  885. */
  886. public AbstractStringBuilder insert(int offset, Object obj) {
  887. return insert(offset, String.valueOf(obj));
  888. }
  889. /**
  890. * Inserts the string into this character sequence.
  891. * <p>
  892. * The characters of the {@code String} argument are inserted, in
  893. * order, into this sequence at the indicated offset, moving up any
  894. * characters originally above that position and increasing the length
  895. * of this sequence by the length of the argument. If
  896. * {@code str} is {@code null}, then the four characters
  897. * {@code "null"} are inserted into this sequence.
  898. * <p>
  899. * The character at index <i>k</i> in the new character sequence is
  900. * equal to:
  901. * <ul>
  902. * <li>the character at index <i>k</i> in the old character sequence, if
  903. * <i>k</i> is less than {@code offset}
  904. * <li>the character at index <i>k</i>{@code -offset} in the
  905. * argument {@code str}, if <i>k</i> is not less than
  906. * {@code offset} but is less than {@code offset+str.length()}
  907. * <li>the character at index <i>k</i>{@code -str.length()} in the
  908. * old character sequence, if <i>k</i> is not less than
  909. * {@code offset+str.length()}
  910. * </ul><p>
  911. * The {@code offset} argument must be greater than or equal to
  912. * {@code 0}, and less than or equal to the {@linkplain #length() length}
  913. * of this sequence.
  914. *
  915. * @param offset the offset.
  916. * @param str a string.
  917. * @return a reference to this object.
  918. * @throws StringIndexOutOfBoundsException if the offset is invalid.
  919. */
  920. public AbstractStringBuilder insert(int offset, String str) {
  921. if ((offset < 0) || (offset > length()))
  922. throw new StringIndexOutOfBoundsException(offset);
  923. if (str == null)
  924. str = "null";
  925. int len = str.length();
  926. ensureCapacityInternal(count + len);
  927. System.arraycopy(value, offset, value, offset + len, count - offset);
  928. str.getChars(value, offset);
  929. count += len;
  930. return this;
  931. }
  932. /**
  933. * Inserts the string representation of the {@code char} array
  934. * argument into this sequence.
  935. * <p>
  936. * The characters of the array argument are inserted into the
  937. * contents of this sequence at the position indicated by
  938. * {@code offset}. The length of this sequence increases by
  939. * the length of the argument.
  940. * <p>
  941. * The overall effect is exactly as if the second argument were
  942. * converted to a string by the method {@link String#valueOf(char[])},
  943. * and the characters of that string were then
  944. * {@link #insert(int,String) inserted} into this character
  945. * sequence at the indicated offset.
  946. * <p>
  947. * The {@code offset} argument must be greater than or equal to
  948. * {@code 0}, and less than or equal to the {@linkplain #length() length}
  949. * of this sequence.
  950. *
  951. * @param offset the offset.
  952. * @param str a character array.
  953. * @return a reference to this object.
  954. * @throws StringIndexOutOfBoundsException if the offset is invalid.
  955. */
  956. public AbstractStringBuilder insert(int offset, char[] str) {
  957. if ((offset < 0) || (offset > length()))
  958. throw new StringIndexOutOfBoundsException(offset);
  959. int len = str.length;
  960. ensureCapacityInternal(count + len);
  961. System.arraycopy(value, offset, value, offset + len, count - offset);
  962. System.arraycopy(str, 0, value, offset, len);
  963. count += len;
  964. return this;
  965. }
  966. /**
  967. * Inserts the specified {@code CharSequence} into this sequence.
  968. * <p>
  969. * The characters of the {@code CharSequence} argument are inserted,
  970. * in order, into this sequence at the indicated offset, moving up
  971. * any characters originally above that position and increasing the length
  972. * of this sequence by the length of the argument s.
  973. * <p>
  974. * The result of this method is exactly the same as if it were an
  975. * invocation of this object's
  976. * {@link #insert(int,CharSequence,int,int) insert}(dstOffset, s, 0, s.length())
  977. * method.
  978. *
  979. * <p>If {@code s} is {@code null}, then the four characters
  980. * {@code "null"} are inserted into this sequence.
  981. *
  982. * @param dstOffset the offset.
  983. * @param s the sequence to be inserted
  984. * @return a reference to this object.
  985. * @throws IndexOutOfBoundsException if the offset is invalid.
  986. */
  987. public AbstractStringBuilder insert(int dstOffset, CharSequence s) {
  988. if (s == null)
  989. s = "null";
  990. if (s instanceof String)
  991. return this.insert(dstOffset, (String)s);
  992. return this.insert(dstOffset, s, 0, s.length());
  993. }
  994. /**
  995. * Inserts a subsequence of the specified {@code CharSequence} into
  996. * this sequence.
  997. * <p>
  998. * The subsequence of the argument {@code s} specified by
  999. * {@code start} and {@code end} are inserted,
  1000. * in order, into this sequence at the specified destination offset, moving
  1001. * up any characters originally above that position. The length of this
  1002. * sequence is increased by {@code end - start}.
  1003. * <p>
  1004. * The character at index <i>k</i> in this sequence becomes equal to:
  1005. * <ul>
  1006. * <li>the character at index <i>k</i> in this sequence, if
  1007. * <i>k</i> is less than {@code dstOffset}
  1008. * <li>the character at index <i>k</i>{@code +start-dstOffset} in
  1009. * the argument {@code s}, if <i>k</i> is greater than or equal to
  1010. * {@code dstOffset} but is less than {@code dstOffset+end-start}
  1011. * <li>the character at index <i>k</i>{@code -(end-start)} in this
  1012. * sequence, if <i>k</i> is greater than or equal to
  1013. * {@code dstOffset+end-start}
  1014. * </ul><p>
  1015. * The {@code dstOffset} argument must be greater than or equal to
  1016. * {@code 0}, and less than or equal to the {@linkplain #length() length}
  1017. * of this sequence.
  1018. * <p>The start argument must be nonnegative, and not greater than
  1019. * {@code end}.
  1020. * <p>The end argument must be greater than or equal to
  1021. * {@code start}, and less than or equal to the length of s.
  1022. *
  1023. * <p>If {@code s} is {@code null}, then this method inserts
  1024. * characters as if the s parameter was a sequence containing the four
  1025. * characters {@code "null"}.
  1026. *
  1027. * @param dstOffset the offset in this sequence.
  1028. * @param s the sequence to be inserted.
  1029. * @param start the starting index of the subsequence to be inserted.
  1030. * @param end the end index of the subsequence to be inserted.
  1031. * @return a reference to this object.
  1032. * @throws IndexOutOfBoundsException if {@code dstOffset}
  1033. * is negative or greater than {@code this.length()}, or
  1034. * {@code start} or {@code end} are negative, or
  1035. * {@code start} is greater than {@code end} or
  1036. * {@code end} is greater than {@code s.length()}
  1037. */
  1038. public AbstractStringBuilder insert(int dstOffset, CharSequence s,
  1039. int start, int end) {
  1040. if (s == null)
  1041. s = "null";
  1042. if ((dstOffset < 0) || (dstOffset > this.length()))
  1043. throw new IndexOutOfBoundsException("dstOffset "+dstOffset);
  1044. if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))
  1045. throw new IndexOutOfBoundsException(
  1046. "start " + start + ", end " + end + ", s.length() "
  1047. + s.length());
  1048. int len = end - start;
  1049. ensureCapacityInternal(count + len);
  1050. System.arraycopy(value, dstOffset, value, dstOffset + len,
  1051. count - dstOffset);
  1052. for (int i=start; i<end; i++)
  1053. value[dstOffset++] = s.charAt(i);
  1054. count += len;
  1055. return this;
  1056. }
  1057. /**
  1058. * Inserts the string representation of the {@code boolean}
  1059. * argument into this sequence.
  1060. * <p>
  1061. * The overall effect is exactly as if the second argument were
  1062. * converted to a string by the method {@link String#valueOf(boolean)},
  1063. * and the characters of that string were then
  1064. * {@link #insert(int,String) inserted} into this character
  1065. * sequence at the indicated offset.
  1066. * <p>
  1067. * The {@code offset} argument must be greater than or equal to
  1068. * {@code 0}, and less than or equal to the {@linkplain #length() length}
  1069. * of this sequence.
  1070. *
  1071. * @param offset the offset.
  1072. * @param b a {@code boolean}.
  1073. * @return a reference to this object.
  1074. * @throws StringIndexOutOfBoundsException if the offset is invalid.
  1075. */
  1076. public AbstractStringBuilder insert(int offset, boolean b) {
  1077. return insert(offset, String.valueOf(b));
  1078. }
  1079. /**
  1080. * Inserts the string representation of the {@code char}
  1081. * argument into this sequence.
  1082. * <p>
  1083. * The overall effect is exactly as if the second argument were
  1084. * converted to a string by the method {@link String#valueOf(char)},
  1085. * and the character in that string were then
  1086. * {@link #insert(int,String) inserted} into this character
  1087. * sequence at the indicated offset.
  1088. * <p>
  1089. * The {@code offset} argument must be greater than or equal to
  1090. * {@code 0}, and less than or equal to the {@linkplain #length() length}
  1091. * of this sequence.
  1092. *
  1093. * @param offset the offset.
  1094. * @param c a {@code char}.
  1095. * @return a reference to this object.
  1096. * @throws IndexOutOfBoundsException if the offset is invalid.
  1097. */
  1098. public AbstractStringBuilder insert(int offset, char c) {
  1099. ensureCapacityInternal(count + 1);
  1100. System.arraycopy(value, offset, value, offset + 1, count - offset);
  1101. value[offset] = c;
  1102. count += 1;
  1103. return this;
  1104. }
  1105. /**
  1106. * Inserts the string representation of the second {@code int}
  1107. * argument into this sequence.
  1108. * <p>
  1109. * The overall effect is exactly as if the second argument were
  1110. * converted to a string by the method {@link String#valueOf(int)},
  1111. * and the characters of that string were then
  1112. * {@link #insert(int,String) inserted} into this character
  1113. * sequence at the indicated offset.
  1114. * <p>
  1115. * The {@code offset} argument must be greater than or equal to
  1116. * {@code 0}, and less than or equal to the {@linkplain #length() length}
  1117. * of this sequence.
  1118. *
  1119. * @param offset the offset.
  1120. * @param i an {@code int}.
  1121. * @return a reference to this object.
  1122. * @throws StringIndexOutOfBoundsException if the offset is invalid.
  1123. */
  1124. public AbstractStringBuilder insert(int offset, int i) {
  1125. return insert(offset, String.valueOf(i));
  1126. }
  1127. /**
  1128. * Inserts the string representation of the {@code long}
  1129. * argument into this sequence.
  1130. * <p>
  1131. * The overall effect is exactly as if the second argument were
  1132. * converted to a string by the method {@link String#valueOf(long)},
  1133. * and the characters of that string were then
  1134. * {@link #insert(int,String) inserted} into this character
  1135. * sequence at the indicated offset.
  1136. * <p>
  1137. * The {@code offset} argument must be greater than or equal to
  1138. * {@code 0}, and less than or equal to the {@linkplain #length() length}
  1139. * of this sequence.
  1140. *
  1141. * @param offset the offset.
  1142. * @param l a {@code long}.
  1143. * @return a reference to this object.
  1144. * @throws StringIndexOutOfBoundsException if the offset is invalid.
  1145. */
  1146. public AbstractStringBuilder insert(int offset, long l) {
  1147. return insert(offset, String.valueOf(l));
  1148. }
  1149. /**
  1150. * Inserts the string representation of the {@code float}
  1151. * argument into this sequence.
  1152. * <p>
  1153. * The overall effect is exactly as if the second argument were
  1154. * converted to a string by the method {@link String#valueOf(float)},
  1155. * and the characters of that string were then
  1156. * {@link #insert(int,String) inserted} into this character
  1157. * sequence at the indicated offset.
  1158. * <p>

Large files files are truncated, but you can click here to view the full file