-
Notifications
You must be signed in to change notification settings - Fork 624
Expand file tree
/
Copy pathClientUtils.java
More file actions
95 lines (86 loc) · 3.39 KB
/
ClientUtils.java
File metadata and controls
95 lines (86 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.clickhouse.client.api.internal;
/**
* Class containing utility methods used across the client.
*/
public final class ClientUtils {
private ClientUtils() {}
/**
* Checks whether the given string is non-null and contains at least one non-whitespace character.
*
* @param str the string to check
* @return {@code true} if {@code str} is non-null and has at least one non-whitespace character
*/
public static boolean isNotBlank(String str) {
return str != null && !str.trim().isEmpty();
}
/**
* Checks whether the given string is null or contains only whitespace.
*
* @param str the string to check
* @return {@code true} if {@code str} is null or trims to an empty string
*/
public static boolean isBlank(String str) {
return str == null || str.trim().isEmpty();
}
/**
* Checks whether the given string is null or empty (without trimming).
*
* @param str the string to check
* @return {@code true} if {@code str} is null or has zero length
*/
public static boolean isNullOrEmpty(String str) {
return str == null || str.isEmpty();
}
/**
* Returns {@code value} if it is not blank, otherwise returns {@code defaultValue}.
*
* @param value the candidate value
* @param defaultValue the value to return when {@code value} is null or only whitespace
* @return {@code value} when non-blank, {@code defaultValue} otherwise
*/
public static String defaultIfBlank(String value, String defaultValue) {
return isBlank(value) ? defaultValue : value;
}
/**
* Validates that the given string is non-blank.
*
* @param value the value to validate
* @param name human-readable name used in the error message
* @return {@code value} unchanged
* @throws IllegalArgumentException if {@code value} is null or only whitespace
*/
public static String requireNonBlank(String value, String name) {
if (isBlank(value)) {
throw new IllegalArgumentException("\"" + name + "\" must be non-null and non-blank");
}
return value;
}
/**
* Truncates the given string to at most {@code maxLength} characters. A {@code maxLength} of
* {@code 0} returns an empty string; a negative value throws {@link IllegalArgumentException}.
*
* @param value the string to truncate (may be null)
* @param maxLength maximum number of characters to keep (must be non-negative)
* @return the original string when null or already short enough, or a truncated copy otherwise
* @throws IllegalArgumentException if {@code maxLength} is negative
*/
public static String truncate(String value, int maxLength) {
if (maxLength < 0) {
throw new IllegalArgumentException("maxLength must be non-negative, got " + maxLength);
}
if (value == null || value.length() <= maxLength) {
return value;
}
return value.substring(0, maxLength);
}
/**
* Null-safe, case-insensitive string comparison.
*
* @param a first string (may be null)
* @param b second string (may be null)
* @return {@code true} if both are null or if they are equal ignoring case
*/
public static boolean equalsIgnoreCase(String a, String b) {
return a == null ? b == null : a.equalsIgnoreCase(b);
}
}