Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/lang/CharSequence.java
38829 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.lang;2627import java.util.NoSuchElementException;28import java.util.PrimitiveIterator;29import java.util.Spliterator;30import java.util.Spliterators;31import java.util.function.IntConsumer;32import java.util.stream.IntStream;33import java.util.stream.StreamSupport;3435/**36* A <tt>CharSequence</tt> is a readable sequence of <code>char</code> values. This37* interface provides uniform, read-only access to many different kinds of38* <code>char</code> sequences.39* A <code>char</code> value represents a character in the <i>Basic40* Multilingual Plane (BMP)</i> or a surrogate. Refer to <a41* href="Character.html#unicode">Unicode Character Representation</a> for details.42*43* <p> This interface does not refine the general contracts of the {@link44* java.lang.Object#equals(java.lang.Object) equals} and {@link45* java.lang.Object#hashCode() hashCode} methods. The result of comparing two46* objects that implement <tt>CharSequence</tt> is therefore, in general,47* undefined. Each object may be implemented by a different class, and there48* is no guarantee that each class will be capable of testing its instances49* for equality with those of the other. It is therefore inappropriate to use50* arbitrary <tt>CharSequence</tt> instances as elements in a set or as keys in51* a map. </p>52*53* @author Mike McCloskey54* @since 1.455* @spec JSR-5156*/5758public interface CharSequence {5960/**61* Returns the length of this character sequence. The length is the number62* of 16-bit <code>char</code>s in the sequence.63*64* @return the number of <code>char</code>s in this sequence65*/66int length();6768/**69* Returns the <code>char</code> value at the specified index. An index ranges from zero70* to <tt>length() - 1</tt>. The first <code>char</code> value of the sequence is at71* index zero, the next at index one, and so on, as for array72* indexing.73*74* <p>If the <code>char</code> value specified by the index is a75* <a href="{@docRoot}/java/lang/Character.html#unicode">surrogate</a>, the surrogate76* value is returned.77*78* @param index the index of the <code>char</code> value to be returned79*80* @return the specified <code>char</code> value81*82* @throws IndexOutOfBoundsException83* if the <tt>index</tt> argument is negative or not less than84* <tt>length()</tt>85*/86char charAt(int index);8788/**89* Returns a <code>CharSequence</code> that is a subsequence of this sequence.90* The subsequence starts with the <code>char</code> value at the specified index and91* ends with the <code>char</code> value at index <tt>end - 1</tt>. The length92* (in <code>char</code>s) of the93* returned sequence is <tt>end - start</tt>, so if <tt>start == end</tt>94* then an empty sequence is returned.95*96* @param start the start index, inclusive97* @param end the end index, exclusive98*99* @return the specified subsequence100*101* @throws IndexOutOfBoundsException102* if <tt>start</tt> or <tt>end</tt> are negative,103* if <tt>end</tt> is greater than <tt>length()</tt>,104* or if <tt>start</tt> is greater than <tt>end</tt>105*/106CharSequence subSequence(int start, int end);107108/**109* Returns a string containing the characters in this sequence in the same110* order as this sequence. The length of the string will be the length of111* this sequence.112*113* @return a string consisting of exactly this sequence of characters114*/115public String toString();116117/**118* Returns a stream of {@code int} zero-extending the {@code char} values119* from this sequence. Any char which maps to a <a120* href="{@docRoot}/java/lang/Character.html#unicode">surrogate code121* point</a> is passed through uninterpreted.122*123* <p>If the sequence is mutated while the stream is being read, the124* result is undefined.125*126* @return an IntStream of char values from this sequence127* @since 1.8128*/129public default IntStream chars() {130class CharIterator implements PrimitiveIterator.OfInt {131int cur = 0;132133public boolean hasNext() {134return cur < length();135}136137public int nextInt() {138if (hasNext()) {139return charAt(cur++);140} else {141throw new NoSuchElementException();142}143}144145@Override146public void forEachRemaining(IntConsumer block) {147for (; cur < length(); cur++) {148block.accept(charAt(cur));149}150}151}152153return StreamSupport.intStream(() ->154Spliterators.spliterator(155new CharIterator(),156length(),157Spliterator.ORDERED),158Spliterator.SUBSIZED | Spliterator.SIZED | Spliterator.ORDERED,159false);160}161162/**163* Returns a stream of code point values from this sequence. Any surrogate164* pairs encountered in the sequence are combined as if by {@linkplain165* Character#toCodePoint Character.toCodePoint} and the result is passed166* to the stream. Any other code units, including ordinary BMP characters,167* unpaired surrogates, and undefined code units, are zero-extended to168* {@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 result171* is undefined.172*173* @return an IntStream of Unicode code points from this sequence174* @since 1.8175*/176public default IntStream codePoints() {177class CodePointIterator implements PrimitiveIterator.OfInt {178int cur = 0;179180@Override181public void forEachRemaining(IntConsumer block) {182final int length = length();183int i = cur;184try {185while (i < length) {186char c1 = charAt(i++);187if (!Character.isHighSurrogate(c1) || i >= length) {188block.accept(c1);189} else {190char c2 = charAt(i);191if (Character.isLowSurrogate(c2)) {192i++;193block.accept(Character.toCodePoint(c1, c2));194} else {195block.accept(c1);196}197}198}199} finally {200cur = i;201}202}203204public boolean hasNext() {205return cur < length();206}207208public int nextInt() {209final int length = length();210211if (cur >= length) {212throw new NoSuchElementException();213}214char c1 = charAt(cur++);215if (Character.isHighSurrogate(c1) && cur < length) {216char c2 = charAt(cur);217if (Character.isLowSurrogate(c2)) {218cur++;219return Character.toCodePoint(c1, c2);220}221}222return c1;223}224}225226return StreamSupport.intStream(() ->227Spliterators.spliteratorUnknownSize(228new CodePointIterator(),229Spliterator.ORDERED),230Spliterator.ORDERED,231false);232}233}234235236