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.io.ObjectStreamField;
29 import java.io.UnsupportedEncodingException;
30 import java.lang.annotation.Native;
31 import java.nio.charset.Charset;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Comparator;
35 import java.util.Formatter;
36 import java.util.Locale;
37 import java.util.Objects;
38 import java.util.Spliterator;
39 import java.util.StringJoiner;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
42 import java.util.regex.PatternSyntaxException;
43 import java.util.stream.Collectors;
44 import java.util.stream.IntStream;
45 import java.util.stream.Stream;
46 import java.util.stream.StreamSupport;
47 import jdk.internal.HotSpotIntrinsicCandidate;
48 import jdk.internal.vm.annotation.Stable;
49
50 import static java.util.function.Predicate.not;
51
52 /**
53 * The {@code String} class represents character strings. All
54 * string literals in Java programs, such as {@code "abc"}, are
55 * implemented as instances of this class.
56 * <p>
57 * Strings are constant; their values cannot be changed after they
58 * are created. String buffers support mutable strings.
59 * Because String objects are immutable they can be shared. For example:
2955 *
2956 * @return string with margins removed, indentation adjusted and
2957 * line terminators normalized
2958 *
2959 * @see String#align()
2960 *
2961 * @since 12
2962 */
2963 public String align(int n) {
2964 if (isEmpty()) {
2965 return "";
2966 }
2967 int outdent = lines().filter(not(String::isBlank))
2968 .mapToInt(String::indexOfNonWhitespace)
2969 .min()
2970 .orElse(0);
2971 return indent(n - outdent, true);
2972 }
2973
2974 /**
2975 * This object (which is already a string!) is itself returned.
2976 *
2977 * @return the string itself.
2978 */
2979 public String toString() {
2980 return this;
2981 }
2982
2983 /**
2984 * Returns a stream of {@code int} zero-extending the {@code char} values
2985 * from this sequence. Any char which maps to a <a
2986 * href="{@docRoot}/java.base/java/lang/Character.html#unicode">surrogate code
2987 * point</a> is passed through uninterpreted.
2988 *
2989 * @return an IntStream of char values from this sequence
2990 * @since 9
2991 */
2992 @Override
2993 public IntStream chars() {
2994 return StreamSupport.intStream(
|
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.io.ObjectStreamField;
29 import java.io.UnsupportedEncodingException;
30 import java.lang.annotation.Native;
31 import java.nio.charset.Charset;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Comparator;
35 import java.util.Formatter;
36 import java.util.Locale;
37 import java.util.Objects;
38 import java.util.Spliterator;
39 import java.util.StringJoiner;
40 import java.util.function.Function;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
43 import java.util.regex.PatternSyntaxException;
44 import java.util.stream.Collectors;
45 import java.util.stream.IntStream;
46 import java.util.stream.Stream;
47 import java.util.stream.StreamSupport;
48 import jdk.internal.HotSpotIntrinsicCandidate;
49 import jdk.internal.vm.annotation.Stable;
50
51 import static java.util.function.Predicate.not;
52
53 /**
54 * The {@code String} class represents character strings. All
55 * string literals in Java programs, such as {@code "abc"}, are
56 * implemented as instances of this class.
57 * <p>
58 * Strings are constant; their values cannot be changed after they
59 * are created. String buffers support mutable strings.
60 * Because String objects are immutable they can be shared. For example:
2956 *
2957 * @return string with margins removed, indentation adjusted and
2958 * line terminators normalized
2959 *
2960 * @see String#align()
2961 *
2962 * @since 12
2963 */
2964 public String align(int n) {
2965 if (isEmpty()) {
2966 return "";
2967 }
2968 int outdent = lines().filter(not(String::isBlank))
2969 .mapToInt(String::indexOfNonWhitespace)
2970 .min()
2971 .orElse(0);
2972 return indent(n - outdent, true);
2973 }
2974
2975 /**
2976 * This method allows the application of a function to {@code this}
2977 * string. The function should expect a single String argument
2978 * and produce an {@code R} result.
2979 *
2980 * @param f functional interface to a apply
2981 *
2982 * @param <R> class of the result
2983 *
2984 * @return the result of applying the function to this string
2985 *
2986 * @see java.util.function.Function
2987 *
2988 * @since 12
2989 */
2990 public <R> R transform(Function<String, R> f) {
2991 return f.apply(this);
2992 }
2993
2994 /**
2995 * This object (which is already a string!) is itself returned.
2996 *
2997 * @return the string itself.
2998 */
2999 public String toString() {
3000 return this;
3001 }
3002
3003 /**
3004 * Returns a stream of {@code int} zero-extending the {@code char} values
3005 * from this sequence. Any char which maps to a <a
3006 * href="{@docRoot}/java.base/java/lang/Character.html#unicode">surrogate code
3007 * point</a> is passed through uninterpreted.
3008 *
3009 * @return an IntStream of char values from this sequence
3010 * @since 9
3011 */
3012 @Override
3013 public IntStream chars() {
3014 return StreamSupport.intStream(
|