001/*
002 * Copyright (c) 2023 Chris K Wensel <[email protected]>. All Rights Reserved.
003 *
004 * This Source Code Form is subject to the terms of the Mozilla Public
005 * License, v. 2.0. If a copy of the MPL was not distributed with this
006 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
007 */
008
009package clusterless.commons.util;
010
011
012import com.google.common.base.CaseFormat;
013import com.google.common.base.Joiner;
014import com.google.common.collect.Maps;
015
016import java.util.Arrays;
017import java.util.List;
018import java.util.Map;
019import java.util.Objects;
020import java.util.function.Function;
021import java.util.stream.Collectors;
022
023/**
024 * Strings is a utility class for working with strings.
025 */
026public class Strings {
027    public static String joinCleanDash(Object... objects) {
028        return joinClean("-", objects);
029    }
030
031    public static String joinDash(Object... objects) {
032        return join("-", objects);
033    }
034
035    /**
036     * use with {@link #nullOr(Object, Function)}
037     *
038     * @param separator
039     * @param objects
040     * @return
041     */
042    public static String joinClean(String separator, Object... objects) {
043        List<String> clean = Arrays.stream(objects)
044                .map(o -> Objects.toString(o, null))
045                .map(Strings::emptyToNull)
046                .collect(Collectors.toList());
047
048        return Joiner.on(separator)
049                .skipNulls()
050                .join(clean);
051    }
052
053    public static String join(String separator, Object... objects) {
054        List<String> clean = Arrays.stream(objects)
055                .map(o -> Objects.toString(o, null))
056                .map(Strings::emptyToNull)
057                .collect(Collectors.toList());
058
059        if (clean.stream().anyMatch(Objects::isNull)) {
060            return null;
061        }
062
063        return Joiner.on(separator)
064                .join(clean);
065    }
066
067    public static String upperCamel(String string) {
068        return CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, string);
069    }
070
071    public static String lowerUnderscoreToCamelCase(String string) {
072        if (string == null) {
073            return null;
074        }
075
076        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, string);
077    }
078
079    public static String camelToLowerHyphen(String string) {
080        if (string == null) {
081            return null;
082        }
083
084        return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, string);
085    }
086
087    public static String camelToLowerCamel(String string) {
088        if (string == null) {
089            return null;
090        }
091
092        return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, string);
093    }
094
095    public static String camelToLowerUnderscore(String string) {
096        if (string == null) {
097            return null;
098        }
099
100        return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, string);
101    }
102
103    public static String camelToUpperUnderscore(String string) {
104        if (string == null) {
105            return null;
106        }
107
108        return CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, string);
109    }
110
111    public static String lowerHyphenToUpperCamel(String string) {
112        if (string == null) {
113            return null;
114        }
115
116        return CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, string);
117    }
118
119    public static String emptyToNull(String string) {
120        if (string == null || string.isEmpty()) {
121            return null;
122        }
123
124        return string;
125    }
126
127    public static String nullToEmpty(Object value) {
128        if (value == null) {
129            return "";
130        }
131
132        return value.toString();
133    }
134
135    public static <T> String nullOr(T e, Function<T, String> f) {
136        if (e == null) {
137            return null;
138        }
139
140        return f.apply(e);
141    }
142
143    public static String removeLast(char delim, String value) {
144        if (value != null && value.lastIndexOf(delim) == value.length() - 1) {
145            value = value.substring(0, value.length() - 1);
146        }
147
148        return value;
149    }
150
151    public static String joinEscaped(Map<String, String> map, String delimiter) {
152        return Joiner.on(delimiter)
153                .withKeyValueSeparator("=")
154                .join(Maps.transformEntries(map, (key, value) -> value == null ? null : String.format("\"%s\"", value)));
155    }
156}