src/share/classes/java/lang/AbstractStringBuilder.java

Print this page




  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import sun.misc.FloatingDecimal;
  29 import java.util.Arrays;

  30 
  31 /**
  32  * A mutable sequence of characters.
  33  * <p>
  34  * Implements a modifiable string. At any point in time it contains some
  35  * particular sequence of characters, but the length and content of the
  36  * sequence can be changed through certain method calls.
  37  *
  38  * @author      Michael McCloskey
  39  * @since       1.5
  40  */
  41 abstract class AbstractStringBuilder implements Appendable, CharSequence {
  42     /**
  43      * The value is used for character storage.
  44      */
  45     char[] value;
  46 
  47     /**
  48      * The count is the number of characters used.
  49      */


1393             value[j] = temp2;
1394             value[n - j] = temp;
1395         }
1396         if (hasSurrogate) {
1397             // Reverse back all valid surrogate pairs
1398             for (int i = 0; i < count - 1; i++) {
1399                 char c2 = value[i];
1400                 if (Character.isLowSurrogate(c2)) {
1401                     char c1 = value[i + 1];
1402                     if (Character.isHighSurrogate(c1)) {
1403                         value[i++] = c1;
1404                         value[i] = c2;
1405                     }
1406                 }
1407             }
1408         }
1409         return this;
1410     }
1411 
1412     /**




















































































1413      * Returns a string representing the data in this sequence.
1414      * A new <code>String</code> object is allocated and initialized to
1415      * contain the character sequence currently represented by this
1416      * object. This <code>String</code> is then returned. Subsequent
1417      * changes to this sequence do not affect the contents of the
1418      * <code>String</code>.
1419      *
1420      * @return  a string representation of this sequence of characters.
1421      */
1422     public abstract String toString();
1423 
1424     /**
1425      * Needed by <tt>String</tt> for the contentEquals method.
1426      */
1427     final char[] getValue() {
1428         return value;
1429     }
1430 
1431 }


  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import sun.misc.FloatingDecimal;
  29 import java.util.Arrays;
  30 import java.util.Iterator;
  31 
  32 /**
  33  * A mutable sequence of characters.
  34  * <p>
  35  * Implements a modifiable string. At any point in time it contains some
  36  * particular sequence of characters, but the length and content of the
  37  * sequence can be changed through certain method calls.
  38  *
  39  * @author      Michael McCloskey
  40  * @since       1.5
  41  */
  42 abstract class AbstractStringBuilder implements Appendable, CharSequence {
  43     /**
  44      * The value is used for character storage.
  45      */
  46     char[] value;
  47 
  48     /**
  49      * The count is the number of characters used.
  50      */


1394             value[j] = temp2;
1395             value[n - j] = temp;
1396         }
1397         if (hasSurrogate) {
1398             // Reverse back all valid surrogate pairs
1399             for (int i = 0; i < count - 1; i++) {
1400                 char c2 = value[i];
1401                 if (Character.isLowSurrogate(c2)) {
1402                     char c1 = value[i + 1];
1403                     if (Character.isHighSurrogate(c1)) {
1404                         value[i++] = c1;
1405                         value[i] = c2;
1406                     }
1407                 }
1408             }
1409         }
1410         return this;
1411     }
1412 
1413     /**
1414      * Append to this sequence a composition of the values returned
1415      * by calling {@link String#valueOf(Object)} on each argument and joining them together
1416      * with the delimiter parameter.
1417      * 
1418      * @param delimiter the String appended between each elements.
1419      * @param first the first element to append.
1420      * @param elements  the other Object elements to join together with the first one.
1421      * @return a reference to this object.
1422      * 
1423      * @throws  NullPointerException
1424      *          If <tt>delimiter</tt> or <tt>elements</tt> is <tt>null</tt>
1425      * 
1426      * @since 1.7
1427      */
1428     public AbstractStringBuilder join(String delimiter, Object first, Object... elements) {
1429         if (delimiter == null) {
1430             throw new NullPointerException("delimiter is null");
1431         }
1432         append(first);
1433         for(Object o : elements) {
1434             append(delimiter).append(o);
1435         }
1436         return this;
1437     }
1438 
1439     /**
1440      * Append to this sequence a composition of the values returned
1441      * by calling {@link String#valueOf(Object)} on each array element
1442      * and joining them together with the delimiter parameter.
1443      * 
1444      * @param delimiter the String appended between each elements.
1445      * @param elements an array of Object element to join together.
1446      * @return a reference to this object.
1447      * 
1448      * @throws  NullPointerException
1449      *          If <tt>delimiter</tt> or <tt>elements</tt> is <tt>null</tt>
1450      * 
1451      * @since 1.7
1452      */
1453      public AbstractStringBuilder join(String delimiter, Object[] elements) {
1454         if (delimiter == null) {
1455             throw new NullPointerException("delimiter is null");
1456         }
1457         int length = elements.length;
1458         if (length == 0) {
1459             return this;
1460         }
1461         append(elements[0]);
1462         for(int i=1; i<length; i++) {
1463             append(delimiter).append(elements[i]);
1464         }
1465         return this;
1466      }
1467 
1468     /**
1469      * Append to this sequence a composition of the values returned
1470      * by calling {@link String#valueOf(Object)} on each argument and joining them together
1471      * with the delimiter parameter.
1472      * 
1473      * @param delimiter the String appended between each elements.
1474      * @param elements an Iterable that will have its elements joined together.
1475      * @return a reference to this object.
1476      * 
1477      * @throws  NullPointerException
1478      *          If <tt>delimiter</tt> or <tt>elements</tt> is <tt>null</tt>
1479      * 
1480      * @since 1.7
1481      */
1482     public AbstractStringBuilder join(String delimiter, Iterable<?> elements) {
1483         if (delimiter == null) {
1484             throw new NullPointerException("delimiter is null");
1485         }
1486         Iterator<?> iterator = elements.iterator();
1487         if (!iterator.hasNext()) {
1488             return this;
1489         }
1490         append(iterator.next());
1491         while(iterator.hasNext()) {
1492             append(delimiter).append(iterator.next());
1493         }
1494         return this;
1495     }
1496     
1497     /**
1498      * Returns a string representing the data in this sequence.
1499      * A new <code>String</code> object is allocated and initialized to
1500      * contain the character sequence currently represented by this
1501      * object. This <code>String</code> is then returned. Subsequent
1502      * changes to this sequence do not affect the contents of the
1503      * <code>String</code>.
1504      *
1505      * @return  a string representation of this sequence of characters.
1506      */
1507     public abstract String toString();
1508 
1509     /**
1510      * Needed by <tt>String</tt> for the contentEquals method.
1511      */
1512     final char[] getValue() {
1513         return value;
1514     }
1515 
1516 }