1 /* 2 * Copyright (c) 2000, 2017, 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 {@code CharSequence} is a readable sequence of {@code char} values. This 38 * interface provides uniform, read-only access to many different kinds of 39 * {@code char} sequences. 40 * A {@code char} 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 {@code CharSequence} 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 {@code CharSequence} instances as elements in a set or as keys in 52 * a map. </p> 53 * 54 * @author Mike McCloskey 55 * @since 1.4 56 * @spec JSR-51 57 */ 58 59 public interface CharSequence { 60 61 /** 62 * Returns the length of this character sequence. The length is the number 63 * of 16-bit {@code char}s in the sequence. 64 * 65 * @return the number of {@code char}s in this sequence 66 */ 67 int length(); 68 220 } 221 char c1 = charAt(cur++); 222 if (Character.isHighSurrogate(c1) && cur < length) { 223 char c2 = charAt(cur); 224 if (Character.isLowSurrogate(c2)) { 225 cur++; 226 return Character.toCodePoint(c1, c2); 227 } 228 } 229 return c1; 230 } 231 } 232 233 return StreamSupport.intStream(() -> 234 Spliterators.spliteratorUnknownSize( 235 new CodePointIterator(), 236 Spliterator.ORDERED), 237 Spliterator.ORDERED, 238 false); 239 } 240 } | 1 /* 2 * Copyright (c) 2000, 2018, 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.Objects; 30 import java.util.PrimitiveIterator; 31 import java.util.Spliterator; 32 import java.util.Spliterators; 33 import java.util.function.IntConsumer; 34 import java.util.stream.IntStream; 35 import java.util.stream.StreamSupport; 36 37 /** 38 * A {@code CharSequence} is a readable sequence of {@code char} values. This 39 * interface provides uniform, read-only access to many different kinds of 40 * {@code char} sequences. 41 * A {@code char} value represents a character in the <i>Basic 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 47 * java.lang.Object#hashCode() hashCode} methods. The result of testing two objects 48 * that implement {@code CharSequence} for equality is therefore, in general, undefined. 49 * Each object may be implemented by a different class, and there 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 52 * arbitrary {@code CharSequence} instances as elements in a set or as keys in 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 64 * of 16-bit {@code char}s in the sequence. 65 * 66 * @return the number of {@code char}s in this sequence 67 */ 68 int length(); 69 221 } 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; 231 } 232 } 233 234 return StreamSupport.intStream(() -> 235 Spliterators.spliteratorUnknownSize( 236 new CodePointIterator(), 237 Spliterator.ORDERED), 238 Spliterator.ORDERED, 239 false); 240 } 241 242 /** 243 * Compares two {@code CharSequence} instances lexicographically. Returns a 244 * negative value, zero, or a positive value if the first sequence is lexicographically 245 * less than, equal to, or greater than the second, respectively. 246 * 247 * <p> 248 * The lexicographical ordering of {@code CharSequence} is defined as follows. 249 * Consider a {@code CharSequence} <i>cs</i> of length <i>len</i> to be a 250 * sequence of char values, <i>cs[0]</i> to <i>cs[len-1]</i>. Suppose <i>k</i> 251 * is the lowest index at which the corresponding char values from each sequence 252 * differ. The lexicographic ordering of the sequences is determined by a numeric 253 * comparison of the char values <i>cs1[k]</i> with <i>cs2[k]</i>. If there is 254 * no such index <i>k</i>, the shorter sequence is considered lexicographically 255 * less than the other. If the sequences have the same length, the sequences are 256 * considered lexicographically equal. 257 * 258 * 259 * @param cs1 the first {@code CharSequence} 260 * @param cs2 the second {@code CharSequence} 261 * 262 * @return the value {@code 0} if the two {@code CharSequence} are equal; 263 * a negative integer if the first {@code CharSequence} 264 * is lexicographically less than the second; or a 265 * positive integer if the first {@code CharSequence} is 266 * lexicographically greater than the second. 267 * 268 * @since 11 269 */ 270 @SuppressWarnings("unchecked") 271 public static int compare(CharSequence cs1, CharSequence cs2) { 272 if (Objects.requireNonNull(cs1) == Objects.requireNonNull(cs2)) { 273 return 0; 274 } 275 276 if (cs1.getClass() == cs2.getClass() && cs1 instanceof Comparable) { 277 return ((Comparable<Object>) cs1).compareTo(cs2); 278 } 279 280 for (int i = 0, len = Math.min(cs1.length(), cs2.length()); i < len; i++) { 281 char a = cs1.charAt(i); 282 char b = cs2.charAt(i); 283 if (a != b) { 284 return a - b; 285 } 286 } 287 288 return cs1.length() - cs2.length(); 289 } 290 291 } |