001/*
002 * Configurate
003 * Copyright (C) zml and Configurate contributors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.spongepowered.configurate.util;
018
019import static java.util.Objects.requireNonNull;
020
021/**
022 * Extra string utilities.
023 *
024 * @since 4.0.0
025 */
026public final class Strings {
027
028    private Strings() {}
029
030    /**
031     * Create a new string with the contents of the provided string repeated
032     * {@code times} times.
033     *
034     * <p>Available in {@link String} itself as of JDK 11.
035     *
036     * @param content text to repeat
037     * @param times amount to repeat
038     * @return repeated string
039     * @since 4.0.0
040     */
041    public static String repeat(final String content, final int times) {
042        requireNonNull(content, "content");
043
044        switch (times) {
045            case 0: return "";
046            case 1: return content;
047            default:
048                final StringBuilder ret = new StringBuilder(content.length() * times);
049                for (int i = 0; i < times; ++i) {
050                    ret.append(content);
051                }
052                return ret.toString();
053        }
054    }
055
056    /**
057     * Return if the input string is empty or has no non-whitespace characters.
058     *
059     * <p>This matches the behaviour of {@code String.isBlank()}
060     * in Java 11+.</p>
061     *
062     * @param test input string to test
063     * @return if the input string is blank
064     * @since 4.0.0
065     */
066    public static boolean isBlank(final String test) {
067        for (int i = 0, len = test.length(); i < len; ++i) {
068            if (!Character.isWhitespace(test.charAt(i))) {
069                return false;
070            }
071        }
072        return true;
073    }
074
075}