src/share/classes/java/lang/CharSequence.java

Print this page
rev 7005 : 8012665: CharSequence.chars, CharSequence.codePoints
Reviewed-by:
Contributed-by: smarks
   1 /*
   2  * Copyright (c) 2000, 2011, 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 
  26 package java.lang;
  27 







  28 
  29 /**
  30  * A <tt>CharSequence</tt> is a readable sequence of <code>char</code> values. This
  31  * interface provides uniform, read-only access to many different kinds of
  32  * <code>char</code> sequences.
  33  * A <code>char</code> value represents a character in the <i>Basic
  34  * Multilingual Plane (BMP)</i> or a surrogate. Refer to <a
  35  * href="Character.html#unicode">Unicode Character Representation</a> for details.
  36  *
  37  * <p> This interface does not refine the general contracts of the {@link
  38  * java.lang.Object#equals(java.lang.Object) equals} and {@link
  39  * java.lang.Object#hashCode() hashCode} methods.  The result of comparing two
  40  * objects that implement <tt>CharSequence</tt> is therefore, in general,
  41  * undefined.  Each object may be implemented by a different class, and there
  42  * is no guarantee that each class will be capable of testing its instances
  43  * for equality with those of the other.  It is therefore inappropriate to use
  44  * arbitrary <tt>CharSequence</tt> instances as elements in a set or as keys in
  45  * a map. </p>
  46  *
  47  * @author Mike McCloskey


  91      * @param   end     the end index, exclusive
  92      *
  93      * @return  the specified subsequence
  94      *
  95      * @throws  IndexOutOfBoundsException
  96      *          if <tt>start</tt> or <tt>end</tt> are negative,
  97      *          if <tt>end</tt> is greater than <tt>length()</tt>,
  98      *          or if <tt>start</tt> is greater than <tt>end</tt>
  99      */
 100     CharSequence subSequence(int start, int end);
 101 
 102     /**
 103      * Returns a string containing the characters in this sequence in the same
 104      * order as this sequence.  The length of the string will be the length of
 105      * this sequence. </p>
 106      *
 107      * @return  a string consisting of exactly this sequence of characters
 108      */
 109     public String toString();
 110 



























































































 111 }
   1 /*
   2  * Copyright (c) 2000, 2013, 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 
  26 package java.lang;
  27 
  28 import java.util.NoSuchElementException;
  29 import java.util.PrimitiveIterator;
  30 import java.util.Spliterator;
  31 import java.util.Spliterators;
  32 import java.util.function.IntConsumer;
  33 import java.util.stream.IntStream;
  34 import java.util.stream.StreamSupport;
  35 
  36 /**
  37  * A <tt>CharSequence</tt> is a readable sequence of <code>char</code> values. This
  38  * interface provides uniform, read-only access to many different kinds of
  39  * <code>char</code> sequences.
  40  * A <code>char</code> value represents a character in the <i>Basic
  41  * Multilingual Plane (BMP)</i> or a surrogate. Refer to <a
  42  * href="Character.html#unicode">Unicode Character Representation</a> for details.
  43  *
  44  * <p> This interface does not refine the general contracts of the {@link
  45  * java.lang.Object#equals(java.lang.Object) equals} and {@link
  46  * java.lang.Object#hashCode() hashCode} methods.  The result of comparing two
  47  * objects that implement <tt>CharSequence</tt> is therefore, in general,
  48  * undefined.  Each object may be implemented by a different class, and there
  49  * is no guarantee that each class will be capable of testing its instances
  50  * for equality with those of the other.  It is therefore inappropriate to use
  51  * arbitrary <tt>CharSequence</tt> instances as elements in a set or as keys in
  52  * a map. </p>
  53  *
  54  * @author Mike McCloskey


  98      * @param   end     the end index, exclusive
  99      *
 100      * @return  the specified subsequence
 101      *
 102      * @throws  IndexOutOfBoundsException
 103      *          if <tt>start</tt> or <tt>end</tt> are negative,
 104      *          if <tt>end</tt> is greater than <tt>length()</tt>,
 105      *          or if <tt>start</tt> is greater than <tt>end</tt>
 106      */
 107     CharSequence subSequence(int start, int end);
 108 
 109     /**
 110      * Returns a string containing the characters in this sequence in the same
 111      * order as this sequence.  The length of the string will be the length of
 112      * this sequence. </p>
 113      *
 114      * @return  a string consisting of exactly this sequence of characters
 115      */
 116     public String toString();
 117 
 118     /**
 119      * Returns a stream of {@code int} zero-extending the {@code char} values
 120      * from this sequence.  Any char which maps to a <a
 121      * href="{@docRoot}/java/lang/Character.html#unicode">surrogate code
 122      * point</a> is passed through uninterpreted.
 123      *
 124      * <p>If the sequence is mutated while the stream is being read, the
 125      * result is undefined.
 126      *
 127      * @return an IntStream of char values from this sequence
 128      * @since 1.8
 129      */
 130     public default IntStream chars() {
 131         class CharIterator implements PrimitiveIterator.OfInt {
 132             int cur = 0;
 133 
 134             public boolean hasNext() {
 135                 return cur < length();
 136             }
 137 
 138             public int nextInt() {
 139                 if (hasNext()) {
 140                     return charAt(cur++);
 141                 } else {
 142                     throw new NoSuchElementException();
 143                 }
 144             }
 145 
 146             @Override
 147             public void forEachRemaining(IntConsumer block) {
 148                 for (; cur < length(); cur++) {
 149                     block.accept(charAt(cur));
 150                 }
 151             }
 152         }
 153 
 154         return StreamSupport.intStream(() ->
 155                 Spliterators.spliterator(
 156                         new CharIterator(),
 157                         length(),
 158                         Spliterator.ORDERED),
 159                 Spliterator.SUBSIZED | Spliterator.SIZED | Spliterator.ORDERED);
 160     }
 161 
 162     /**
 163      * Returns a stream of code point values from this sequence.  Any surrogate
 164      * pairs encountered in the sequence are combined as if by {@linkplain
 165      * Character#toCodePoint Character.toCodePoint} and the result is passed
 166      * to the stream. Any other code units, including ordinary BMP characters,
 167      * unpaired surrogates, and undefined code units, are zero-extended to
 168      * {@code int} values which are then passed to the stream.
 169      *
 170      * <p>If the sequence is mutated while the stream is being read, the result
 171      * is undefined.
 172      *
 173      * @return an IntStream of Unicode code points from this sequence
 174      * @since 1.8
 175      */
 176     public default IntStream codePoints() {
 177         class CodePointIterator implements PrimitiveIterator.OfInt {
 178             int cur = 0;
 179 
 180             @Override
 181             public void forEachRemaining(IntConsumer block) {
 182                 while (cur < length()) {
 183                     int cp = Character.codePointAt(CharSequence.this, cur);
 184                     cur += Character.charCount(cp);
 185                     block.accept(cp);
 186                 }
 187             }
 188 
 189             public boolean hasNext() {
 190                 return cur < length();
 191             }
 192 
 193             public int nextInt() {
 194                 if (!hasNext()) {
 195                     throw new NoSuchElementException();
 196                 }
 197                 int cp = Character.codePointAt(CharSequence.this, cur);
 198                 cur += Character.charCount(cp);
 199                 return cp;
 200             }
 201         }
 202 
 203         return StreamSupport.intStream(() ->
 204                 Spliterators.spliteratorUnknownSize(
 205                         new CodePointIterator(),
 206                         Spliterator.ORDERED),
 207                 Spliterator.SUBSIZED | Spliterator.SIZED | Spliterator.ORDERED);
 208     }
 209 }