author | jjg |
Tue, 27 Mar 2018 16:25:40 -0700 | |
changeset 49433 | b6671a111395 |
parent 49115 | ecfaa82c53be |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49115
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.lang; |
|
27 |
||
17210
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
28 |
import java.util.NoSuchElementException; |
49115
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
29 |
import java.util.Objects; |
17210
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
30 |
import java.util.PrimitiveIterator; |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
31 |
import java.util.Spliterator; |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
32 |
import java.util.Spliterators; |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
33 |
import java.util.function.IntConsumer; |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
34 |
import java.util.stream.IntStream; |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
35 |
import java.util.stream.StreamSupport; |
2 | 36 |
|
37 |
/** |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
38 |
* A {@code CharSequence} is a readable sequence of {@code char} values. This |
2 | 39 |
* interface provides uniform, read-only access to many different kinds of |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
40 |
* {@code char} sequences. |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
41 |
* A {@code char} value represents a character in the <i>Basic |
2 | 42 |
* Multilingual Plane (BMP)</i> or a surrogate. Refer to <a |
43 |
* href="Character.html#unicode">Unicode Character Representation</a> for details. |
|
44 |
* |
|
45 |
* <p> This interface does not refine the general contracts of the {@link |
|
46 |
* java.lang.Object#equals(java.lang.Object) equals} and {@link |
|
49115
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
47 |
* java.lang.Object#hashCode() hashCode} methods. The result of testing two objects |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
48 |
* that implement {@code CharSequence} for equality is therefore, in general, undefined. |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
49 |
* Each object may be implemented by a different class, and there |
2 | 50 |
* is no guarantee that each class will be capable of testing its instances |
51 |
* for equality with those of the other. It is therefore inappropriate to use |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
52 |
* arbitrary {@code CharSequence} instances as elements in a set or as keys in |
2 | 53 |
* a map. </p> |
54 |
* |
|
55 |
* @author Mike McCloskey |
|
56 |
* @since 1.4 |
|
57 |
* @spec JSR-51 |
|
58 |
*/ |
|
59 |
||
60 |
public interface CharSequence { |
|
61 |
||
62 |
/** |
|
63 |
* Returns the length of this character sequence. The length is the number |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
64 |
* of 16-bit {@code char}s in the sequence. |
2 | 65 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
66 |
* @return the number of {@code char}s in this sequence |
2 | 67 |
*/ |
68 |
int length(); |
|
69 |
||
70 |
/** |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
71 |
* Returns the {@code char} value at the specified index. An index ranges from zero |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
72 |
* to {@code length() - 1}. The first {@code char} value of the sequence is at |
2 | 73 |
* index zero, the next at index one, and so on, as for array |
18776 | 74 |
* indexing. |
2 | 75 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
76 |
* <p>If the {@code char} value specified by the index is a |
49433
b6671a111395
8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents:
49115
diff
changeset
|
77 |
* <a href="{@docRoot}/java.base/java/lang/Character.html#unicode">surrogate</a>, the surrogate |
2 | 78 |
* value is returned. |
79 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
80 |
* @param index the index of the {@code char} value to be returned |
2 | 81 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
82 |
* @return the specified {@code char} value |
2 | 83 |
* |
84 |
* @throws IndexOutOfBoundsException |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
85 |
* if the {@code index} argument is negative or not less than |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
86 |
* {@code length()} |
2 | 87 |
*/ |
88 |
char charAt(int index); |
|
89 |
||
90 |
/** |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
91 |
* Returns a {@code CharSequence} that is a subsequence of this sequence. |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
92 |
* The subsequence starts with the {@code char} value at the specified index and |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
93 |
* ends with the {@code char} value at index {@code end - 1}. The length |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
94 |
* (in {@code char}s) of the |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
95 |
* returned sequence is {@code end - start}, so if {@code start == end} |
18776 | 96 |
* then an empty sequence is returned. |
2 | 97 |
* |
98 |
* @param start the start index, inclusive |
|
99 |
* @param end the end index, exclusive |
|
100 |
* |
|
101 |
* @return the specified subsequence |
|
102 |
* |
|
103 |
* @throws IndexOutOfBoundsException |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
104 |
* if {@code start} or {@code end} are negative, |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
105 |
* if {@code end} is greater than {@code length()}, |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
106 |
* or if {@code start} is greater than {@code end} |
2 | 107 |
*/ |
108 |
CharSequence subSequence(int start, int end); |
|
109 |
||
110 |
/** |
|
111 |
* Returns a string containing the characters in this sequence in the same |
|
112 |
* order as this sequence. The length of the string will be the length of |
|
18776 | 113 |
* this sequence. |
2 | 114 |
* |
115 |
* @return a string consisting of exactly this sequence of characters |
|
116 |
*/ |
|
117 |
public String toString(); |
|
118 |
||
17210
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
119 |
/** |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
120 |
* Returns a stream of {@code int} zero-extending the {@code char} values |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
121 |
* from this sequence. Any char which maps to a <a |
49433
b6671a111395
8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents:
49115
diff
changeset
|
122 |
* href="{@docRoot}/java.base/java/lang/Character.html#unicode">surrogate code |
17210
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
123 |
* point</a> is passed through uninterpreted. |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
124 |
* |
42225
07097c84db68
8169808: Stream returning methods should specify if they are late binding
psandoz
parents:
32033
diff
changeset
|
125 |
* <p>The stream binds to this sequence when the terminal stream operation |
07097c84db68
8169808: Stream returning methods should specify if they are late binding
psandoz
parents:
32033
diff
changeset
|
126 |
* commences (specifically, for mutable sequences the spliterator for the |
45138 | 127 |
* stream is <a href="../util/Spliterator.html#binding"><em>late-binding</em></a>). |
42225
07097c84db68
8169808: Stream returning methods should specify if they are late binding
psandoz
parents:
32033
diff
changeset
|
128 |
* If the sequence is modified during that operation then the result is |
07097c84db68
8169808: Stream returning methods should specify if they are late binding
psandoz
parents:
32033
diff
changeset
|
129 |
* undefined. |
17210
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
130 |
* |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
131 |
* @return an IntStream of char values from this sequence |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
132 |
* @since 1.8 |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
133 |
*/ |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
134 |
public default IntStream chars() { |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
135 |
class CharIterator implements PrimitiveIterator.OfInt { |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
136 |
int cur = 0; |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
137 |
|
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
138 |
public boolean hasNext() { |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
139 |
return cur < length(); |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
140 |
} |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
141 |
|
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
142 |
public int nextInt() { |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
143 |
if (hasNext()) { |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
144 |
return charAt(cur++); |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
145 |
} else { |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
146 |
throw new NoSuchElementException(); |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
147 |
} |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
148 |
} |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
149 |
|
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
150 |
@Override |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
151 |
public void forEachRemaining(IntConsumer block) { |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
152 |
for (; cur < length(); cur++) { |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
153 |
block.accept(charAt(cur)); |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
154 |
} |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
155 |
} |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
156 |
} |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
157 |
|
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
158 |
return StreamSupport.intStream(() -> |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
159 |
Spliterators.spliterator( |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
160 |
new CharIterator(), |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
161 |
length(), |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
162 |
Spliterator.ORDERED), |
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18776
diff
changeset
|
163 |
Spliterator.SUBSIZED | Spliterator.SIZED | Spliterator.ORDERED, |
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18776
diff
changeset
|
164 |
false); |
17210
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
165 |
} |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
166 |
|
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
167 |
/** |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
168 |
* Returns a stream of code point values from this sequence. Any surrogate |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
169 |
* pairs encountered in the sequence are combined as if by {@linkplain |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
170 |
* Character#toCodePoint Character.toCodePoint} and the result is passed |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
171 |
* to the stream. Any other code units, including ordinary BMP characters, |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
172 |
* unpaired surrogates, and undefined code units, are zero-extended to |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
173 |
* {@code int} values which are then passed to the stream. |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
174 |
* |
42225
07097c84db68
8169808: Stream returning methods should specify if they are late binding
psandoz
parents:
32033
diff
changeset
|
175 |
* <p>The stream binds to this sequence when the terminal stream operation |
07097c84db68
8169808: Stream returning methods should specify if they are late binding
psandoz
parents:
32033
diff
changeset
|
176 |
* commences (specifically, for mutable sequences the spliterator for the |
45138 | 177 |
* stream is <a href="../util/Spliterator.html#binding"><em>late-binding</em></a>). |
42225
07097c84db68
8169808: Stream returning methods should specify if they are late binding
psandoz
parents:
32033
diff
changeset
|
178 |
* If the sequence is modified during that operation then the result is |
07097c84db68
8169808: Stream returning methods should specify if they are late binding
psandoz
parents:
32033
diff
changeset
|
179 |
* undefined. |
17210
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
180 |
* |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
181 |
* @return an IntStream of Unicode code points from this sequence |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
182 |
* @since 1.8 |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
183 |
*/ |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
184 |
public default IntStream codePoints() { |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
185 |
class CodePointIterator implements PrimitiveIterator.OfInt { |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
186 |
int cur = 0; |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
187 |
|
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
188 |
@Override |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
189 |
public void forEachRemaining(IntConsumer block) { |
18141 | 190 |
final int length = length(); |
191 |
int i = cur; |
|
192 |
try { |
|
193 |
while (i < length) { |
|
194 |
char c1 = charAt(i++); |
|
195 |
if (!Character.isHighSurrogate(c1) || i >= length) { |
|
196 |
block.accept(c1); |
|
197 |
} else { |
|
198 |
char c2 = charAt(i); |
|
199 |
if (Character.isLowSurrogate(c2)) { |
|
200 |
i++; |
|
201 |
block.accept(Character.toCodePoint(c1, c2)); |
|
202 |
} else { |
|
203 |
block.accept(c1); |
|
204 |
} |
|
205 |
} |
|
206 |
} |
|
207 |
} finally { |
|
208 |
cur = i; |
|
17210
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
209 |
} |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
210 |
} |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
211 |
|
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
212 |
public boolean hasNext() { |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
213 |
return cur < length(); |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
214 |
} |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
215 |
|
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
216 |
public int nextInt() { |
18141 | 217 |
final int length = length(); |
218 |
||
219 |
if (cur >= length) { |
|
17210
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
220 |
throw new NoSuchElementException(); |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
221 |
} |
18141 | 222 |
char c1 = charAt(cur++); |
223 |
if (Character.isHighSurrogate(c1) && cur < length) { |
|
224 |
char c2 = charAt(cur); |
|
225 |
if (Character.isLowSurrogate(c2)) { |
|
226 |
cur++; |
|
227 |
return Character.toCodePoint(c1, c2); |
|
228 |
} |
|
229 |
} |
|
230 |
return c1; |
|
17210
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
231 |
} |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
232 |
} |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
233 |
|
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
234 |
return StreamSupport.intStream(() -> |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
235 |
Spliterators.spliteratorUnknownSize( |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
236 |
new CodePointIterator(), |
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
237 |
Spliterator.ORDERED), |
20182
7e90af95fc4e
8025002: "".codePoints().sorted().iterator().hasNext() causes NegativeArraySizeException
psandoz
parents:
18822
diff
changeset
|
238 |
Spliterator.ORDERED, |
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18776
diff
changeset
|
239 |
false); |
17210
8a90d05f28d8
8012665: add CharSequence.chars, CharSequence.codePoints
mduigou
parents:
14342
diff
changeset
|
240 |
} |
49115
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
241 |
|
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
242 |
/** |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
243 |
* Compares two {@code CharSequence} instances lexicographically. Returns a |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
244 |
* negative value, zero, or a positive value if the first sequence is lexicographically |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
245 |
* less than, equal to, or greater than the second, respectively. |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
246 |
* |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
247 |
* <p> |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
248 |
* The lexicographical ordering of {@code CharSequence} is defined as follows. |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
249 |
* Consider a {@code CharSequence} <i>cs</i> of length <i>len</i> to be a |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
250 |
* sequence of char values, <i>cs[0]</i> to <i>cs[len-1]</i>. Suppose <i>k</i> |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
251 |
* is the lowest index at which the corresponding char values from each sequence |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
252 |
* differ. The lexicographic ordering of the sequences is determined by a numeric |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
253 |
* comparison of the char values <i>cs1[k]</i> with <i>cs2[k]</i>. If there is |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
254 |
* no such index <i>k</i>, the shorter sequence is considered lexicographically |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
255 |
* less than the other. If the sequences have the same length, the sequences are |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
256 |
* considered lexicographically equal. |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
257 |
* |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
258 |
* |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
259 |
* @param cs1 the first {@code CharSequence} |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
260 |
* @param cs2 the second {@code CharSequence} |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
261 |
* |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
262 |
* @return the value {@code 0} if the two {@code CharSequence} are equal; |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
263 |
* a negative integer if the first {@code CharSequence} |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
264 |
* is lexicographically less than the second; or a |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
265 |
* positive integer if the first {@code CharSequence} is |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
266 |
* lexicographically greater than the second. |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
267 |
* |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
268 |
* @since 11 |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
269 |
*/ |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
270 |
@SuppressWarnings("unchecked") |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
271 |
public static int compare(CharSequence cs1, CharSequence cs2) { |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
272 |
if (Objects.requireNonNull(cs1) == Objects.requireNonNull(cs2)) { |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
273 |
return 0; |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
274 |
} |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
275 |
|
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
276 |
if (cs1.getClass() == cs2.getClass() && cs1 instanceof Comparable) { |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
277 |
return ((Comparable<Object>) cs1).compareTo(cs2); |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
278 |
} |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
279 |
|
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
280 |
for (int i = 0, len = Math.min(cs1.length(), cs2.length()); i < len; i++) { |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
281 |
char a = cs1.charAt(i); |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
282 |
char b = cs2.charAt(i); |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
283 |
if (a != b) { |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
284 |
return a - b; |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
285 |
} |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
286 |
} |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
287 |
|
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
288 |
return cs1.length() - cs2.length(); |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
289 |
} |
ecfaa82c53be
8137326: Methods for comparing CharSequence, StringBuilder, and StringBuffer
joehw
parents:
47216
diff
changeset
|
290 |
|
2 | 291 |
} |