001/*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2025-2026 The Enola <https://enola.dev> Authors
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *     https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package dev.enola.common;
019
020import com.google.common.base.Splitter;
021
022import org.jspecify.annotations.Nullable;
023
024import java.util.regex.Pattern;
025
026public final class MoreStrings {
027
028    private static final Splitter WORD_SPLITTER =
029            Splitter.on(Pattern.compile("[-_\\s]+")).omitEmptyStrings();
030
031    public static int compare(@Nullable String s1, @Nullable String s2) {
032        if (s1 == null && s2 == null) return 0;
033        if (s1 == null) return -1;
034        if (s2 == null) return 1;
035        return s1.compareTo(s2);
036    }
037
038    /**
039     * Converts a delimiter-separated string (e.g. kebab-case, snake_case, or space-separated) into
040     * generic Title Case with space separators.
041     */
042    public static String toTitleCase(@Nullable String input) {
043        if (input == null || input.isEmpty()) {
044            return "";
045        }
046        var words = WORD_SPLITTER.split(input);
047        var sb = new StringBuilder();
048        for (var word : words) {
049            if (!sb.isEmpty()) {
050                sb.append(' ');
051            }
052            sb.append(Character.toUpperCase(word.charAt(0)));
053            if (word.length() > 1) {
054                sb.append(word.substring(1));
055            }
056        }
057        return sb.toString();
058    }
059
060    /**
061     * Returns the trimmed string, or {@code null} if the input is null, empty, or whitespace-only.
062     *
063     * <p>Complementary to Guava's {@link com.google.common.base.Strings#emptyToNull(String)}.
064     */
065    public static @Nullable String trimToNull(@Nullable String input) {
066        if (input == null || input.isBlank()) {
067            return null;
068        }
069        return input.trim();
070    }
071
072    /**
073     * Replaces consecutive whitespace characters with a single space and trims leading and trailing
074     * whitespace. Returns {@code null} if input is null, empty, or whitespace-only.
075     *
076     * @param input the string to normalize
077     * @return the normalized string, or {@code null} if input is blank or null
078     */
079    public static @Nullable String normalizeWhitespace(@Nullable String input) {
080        if (input == null || input.isBlank()) {
081            return null;
082        }
083        return input.replaceAll("\\s+", " ").trim();
084    }
085
086    private MoreStrings() {}
087}