• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

evolvedbinary / elemental / 982

29 Apr 2025 08:34PM UTC coverage: 56.409% (+0.007%) from 56.402%
982

push

circleci

adamretter
[feature] Improve README.md badges

28451 of 55847 branches covered (50.94%)

Branch coverage included in aggregate %.

77468 of 131924 relevant lines covered (58.72%)

0.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

39.37
/exist-core/src/main/java/org/exist/util/StringUtil.java
1
/*
2
 * Elemental
3
 * Copyright (C) 2024, Evolved Binary Ltd
4
 *
5
 * admin@evolvedbinary.com
6
 * https://www.evolvedbinary.com | https://www.elemental.xyz
7
 *
8
 * Use of this software is governed by the Business Source License 1.1
9
 * included in the LICENSE file and at www.mariadb.com/bsl11.
10
 *
11
 * Change Date: 2028-04-27
12
 *
13
 * On the date above, in accordance with the Business Source License, use
14
 * of this software will be governed by the Apache License, Version 2.0.
15
 *
16
 * Additional Use Grant: Production use of the Licensed Work for a permitted
17
 * purpose. A Permitted Purpose is any purpose other than a Competing Use.
18
 * A Competing Use means making the Software available to others in a commercial
19
 * product or service that: substitutes for the Software; substitutes for any
20
 * other product or service we offer using the Software that exists as of the
21
 * date we make the Software available; or offers the same or substantially
22
 * similar functionality as the Software.
23
 */
24
package org.exist.util;
25

26
import javax.annotation.Nullable;
27
import java.util.Optional;
28

29
/**
30
 * Utilities for working with String.
31
 *
32
 * @author <a href="mailto:adam@evolvedbinary.com">Adam Retter</a>
33
 */
34
public class StringUtil {
×
35

36
    /**
37
     * Determines if a String is null or empty.
38
     *
39
     * @param string the string to test.
40
     *
41
     * @return true if the String is null or empty.
42
     */
43
    public static boolean isNullOrEmpty(@Nullable final String string) {
44
        return string == null || string.isEmpty();
1!
45
    }
46

47
    /**
48
     * Returns the input String if it is null or empty.
49
     * otherwise returns the provided default String.
50
     *
51
     * @param string the string to test.
52
     * @param defaultString the default string if the test fails
53
     *
54
     * @return the String if it is null or empty, otherwise the default String.
55
     */
56
    public static @Nullable String isNullOrEmpty(@Nullable final String string, @Nullable final String defaultString) {
57
        return isNullOrEmpty(string) ? string : defaultString;
×
58
    }
59

60
    /**
61
     * Determines if a String is not null, and is not empty.
62
     *
63
     * @param string the string to test.
64
     *
65
     * @return true if the String is not null, and is not empty.
66
     */
67
    public static boolean notNullOrEmpty(@Nullable final String string) {
68
        return string != null && !string.isEmpty();
1✔
69
    }
70

71
    /**
72
     * Returns the input String if it is not null, and is not empty,
73
     * otherwise returns the provided default String.
74
     *
75
     * @param string the string to test.
76
     * @param defaultString the default string if the test fails
77
     *
78
     * @return the String if it is not null, and is not empty, otherwise the default String.
79
     */
80
    public static @Nullable String notNullOrEmpty(@Nullable final String string, @Nullable final String defaultString) {
81
        return notNullOrEmpty(string) ? string : defaultString;
1✔
82
    }
83

84
    /**
85
     * Returns an Optional of the input String if it is not null, and is not empty, otherwise {@link Optional#empty()}.
86
     *
87
     * @param string the string to test.
88
     *
89
     * @return an Optional of the String if it is not null, and is not empty, otherwise {@link Optional#empty()}.
90
     */
91
    public static Optional<String> notNullOrEmptyOptional(@Nullable final String string) {
92
        if (string == null || string.isEmpty()) {
×
93
            return Optional.empty();
×
94
        }
95
        return Optional.of(string);
×
96
    }
97

98
    /**
99
     * Determines if a String is null, empty, or whitespace only.
100
     *
101
     * @param string the string to test.
102
     *
103
     * @return true if the String is null, empty, or whitespace only.
104
     */
105
    public static boolean isNullOrEmptyOrWs(@Nullable final String string) {
106
        return string == null || string.trim().isEmpty();
×
107
    }
108

109
    /**
110
     * Returns the input String if it is null, empty, or is whitespace only,
111
     * otherwise returns the provided default String.
112
     *
113
     * @param string the string to test.
114
     * @param defaultString the default string if the test fails
115
     *
116
     * @return the String if it is null, is empty, or is whitespace only, otherwise the default String.
117
     */
118
    public static @Nullable String isNullOrEmptyOrWs(@Nullable final String string, @Nullable final String defaultString) {
119
        return isNullOrEmptyOrWs(string) ? string : defaultString;
×
120
    }
121

122
    /**
123
     * Determines if a String is not null, is not empty, and is not whitespace only.
124
     *
125
     * @param string the string to test.
126
     *
127
     * @return true if the String is not null, is not empty, and is not whitespace only.
128
     */
129
    public static boolean notNullOrEmptyOrWs(@Nullable final String string) {
130
        return string != null && !string.trim().isEmpty();
1✔
131
    }
132

133
    /**
134
     * Returns the input String if it is not null, is not empty, and is not whitespace only,
135
     * otherwise returns the provided default String.
136
     *
137
     * @param string the string to test.
138
     * @param defaultString the default string if the test fails
139
     *
140
     * @return the String if it is not null, is not empty, and is not whitespace only, otherwise the default String.
141
     */
142
    public static @Nullable String notNullOrEmptyOrWs(@Nullable final String string, @Nullable final String defaultString) {
143
        return notNullOrEmptyOrWs(string) ? string : defaultString;
1✔
144
    }
145

146
    /**
147
     * Returns an Optional of the input String if it is not null, is not empty, and is not whitespace only, otherwise {@link Optional#empty()}.
148
     *
149
     * @param string the string to test.
150
     *
151
     * @return an Optional of the String if it is not null, is not empty, and is not whitespace only, otherwise {@link Optional#empty()}.
152
     */
153
    public static Optional<String> notNullOrEmptyOrWsOptional(@Nullable final String string) {
154
        if (string == null || string.trim().isEmpty()) {
×
155
            return Optional.empty();
×
156
        }
157
        return Optional.of(string);
×
158
    }
159

160
    /**
161
     * Returns null if the string is empty or null, otherwise returns the string.
162
     *
163
     * @param string the string to test.
164
     *
165
     * @return null if the string is empty or null, otherwise returns the string.
166
     */
167
    public static @Nullable String nullIfEmpty(@Nullable final String string) {
168
        return notNullOrEmpty(string, null);
1✔
169
    }
170

171
    /**
172
     * Capitalizes the first character of a string.
173
     *
174
     * @param string the string to capitalize.
175
     *
176
     * @return the capitalized string.
177
     */
178
    public static @Nullable String capitalize(@Nullable final String string) {
179
        if (string == null) {
1!
180
            return null;
×
181
        }
182

183
        final int firstCharCodePoint = string.codePointAt(0);
1✔
184
        final int capitalizedFirstCharCodePoint = Character.toTitleCase(firstCharCodePoint);
1✔
185
        if (firstCharCodePoint == capitalizedFirstCharCodePoint) {
1!
186
            // already capitalized
187
            return string;
×
188
        }
189

190
        final int charsLen = string.length();
1✔
191
        final int[] newCodePoints = new int[charsLen];
1✔
192
        int newCodePointOffset = 0;
1✔
193
        newCodePoints[newCodePointOffset++] = capitalizedFirstCharCodePoint; // copy the first code point
1✔
194
        for (int existingCodePointOffset = Character.charCount(capitalizedFirstCharCodePoint); existingCodePointOffset < charsLen; ) {
1✔
195
            final int existingCodePoint = string.codePointAt(existingCodePointOffset);
1✔
196
            newCodePoints[newCodePointOffset++] = existingCodePoint; // copy the remaining ones
1✔
197
            existingCodePointOffset += Character.charCount(existingCodePoint);
1✔
198
        }
199

200
        return new String(newCodePoints, 0, newCodePointOffset);
1✔
201
    }
202

203
    /**
204
     * Returns true if the string starts with the prefix.
205
     *
206
     * @param string the string to test.
207
     * @param prefix the prefix to look for.
208
     *
209
     * @return true if the string starts with the prefix, false otherwise.
210
     */
211
    public static boolean startsWith(@Nullable final String string, @Nullable final String prefix) {
212
        if (string == null || prefix == null) {
×
213
            return false;
×
214
        }
215

216
        return string.startsWith(prefix);
×
217
    }
218

219
    /**
220
     * Returns true if the string starts with any one of the prefixes.
221
     *
222
     * @param string the string to test.
223
     * @param prefixes the prefixes to look for.
224
     *
225
     * @return true if the string starts with any one of the prefixes, false otherwise.
226
     */
227
    public static boolean startsWith(@Nullable final String string, @Nullable final String[] prefixes) {
228
        if (string == null || prefixes == null) {
1!
229
            return false;
×
230
        }
231

232
        for (final String prefix : prefixes) {
1✔
233
            if (prefix != null && string.startsWith(prefix)) {
1!
234
                    return true;
1✔
235
            }
236
        }
237

238
        return false;
1✔
239
    }
240

241
    /**
242
     * Returns true if the string ends with the prefix.
243
     *
244
     * @param string the string to test.
245
     * @param prefix the prefix to look for.
246
     *
247
     * @return true if the string ends with the prefix, false otherwise.
248
     */
249
    public static boolean endsWith(@Nullable final String string, @Nullable final String prefix) {
250
        if (string == null || prefix == null) {
×
251
            return false;
×
252
        }
253

254
        return string.endsWith(prefix);
×
255
    }
256

257
    /**
258
     * Returns true if the string ends with any one of the prefixes.
259
     *
260
     * @param string the string to test.
261
     * @param prefixes the prefixes to look for.
262
     *
263
     * @return true if the string ends with any one of the prefixes, false otherwise.
264
     */
265
    public static boolean endsWith(@Nullable final String string, @Nullable final String[] prefixes) {
266
        if (string == null || prefixes == null) {
×
267
            return false;
×
268
        }
269

270
        for (final String prefix : prefixes) {
×
271
            if (prefix != null && string.endsWith(prefix)) {
×
272
                return true;
×
273
            }
274
        }
275

276
        return false;
×
277
    }
278

279
    /**
280
     * Returns the substring before the last occurrence of a match.
281
     *
282
     * @param string the string to test
283
     * @param match the match to search for
284
     *
285
     * @return the substring before the last match, or if no match, the entire string.
286
     */
287
    public static @Nullable String substringBeforeLast(@Nullable final String string, @Nullable final String match) {
288
        if (isNullOrEmpty(string) || isNullOrEmpty(match)) {
×
289
            return string;
×
290
        }
291

292
        final int idx = string.lastIndexOf(match);
×
293
        if (idx == -1) {
×
294
            return string;
×
295
        }
296

297
        return string.substring(0, idx);
×
298
    }
299
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc