-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStrings.java
More file actions
191 lines (177 loc) · 6.04 KB
/
Strings.java
File metadata and controls
191 lines (177 loc) · 6.04 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Copyright 2023 Greptime Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.greptime.common.util;
import java.util.ArrayList;
import java.util.List;
/**
* Static utility methods pertaining to {@code String} or {@code CharSequence}
* instances.
*/
public final class Strings {
public static String toString(Object obj) {
if (obj == null) {
return "null";
}
return obj.toString();
}
/**
* Converts a null string to an empty string.
*
* @param string the string to check
* @return the given string if it is non-null; the empty string otherwise
*/
public static String nullToEmpty(String string) {
return (string == null) ? "" : string;
}
/**
* Converts an empty string to a null string.
*
* @param string the string to check
* @return the given string if it is nonempty; {@code null} otherwise
*/
public static String emptyToNull(String string) {
return isNullOrEmpty(string) ? null : string;
}
/**
* Checks if the given string is null or is the empty string.
*
* @param str the string to check
* @return {@code true} if the given string is null or is the empty string
*/
public static boolean isNullOrEmpty(String str) {
return str == null || str.isEmpty();
}
/**
* Checks if a string is whitespace, empty ("") or null.
* <p>
* ``` java
* Strings.isBlank(null) = true
* Strings.isBlank("") = true
* Strings.isBlank(" ") = true
* Strings.isBlank("bob") = false
* Strings.isBlank(" bob ") = false
* ```
*
* @param str the string to check
* @return {@code true} if the given string is null, empty or whitespace only
*/
public static boolean isBlank(String str) {
int strLen;
if (str != null && (strLen = str.length()) != 0) {
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return false;
}
}
}
return true;
}
/**
* Checks if a string is not empty (""), not null and not whitespace only.
* <p>
* ``` java
* Strings.isNotBlank(null) = false
* Strings.isNotBlank("") = false
* Strings.isNotBlank(" ") = false
* Strings.isNotBlank("bob") = true
* Strings.isNotBlank(" bob ") = true
* ```
*
* @param str the string to check
* @return {@code true} if the given string is not null, empty or whitespace only
*/
public static boolean isNotBlank(String str) {
return !isBlank(str);
}
/**
* Splits the provided text into an array, separator specified.
* <p>
* A null input String returns null.
* <p>
* ``` java
* Strings.split(null, *) = null
* Strings.split("", *) = []
* Strings.split("a.b.c", '.') = ["a", "b", "c"]
* Strings.split("a..b.c", '.') = ["a", "b", "c"]
* Strings.split("a:b:c", '.') = ["a:b:c"]
* Strings.split("a b c", ' ') = ["a", "b", "c"]
* ```
*
* @param str the string to split
* @param separator the separator
* @return an array of strings
*/
public static String[] split(String str, char separator) {
return split(str, separator, false);
}
/**
* Splits the provided text into an array, separator specified,
* if {@code} true, preserving all tokens, including empty tokens created
* by adjacent separators.
* <p>
* A null input String returns null.
* <p>
* ``` java
* Strings.split(null, *, true) = null
* Strings.split("", *, true) = []
* Strings.split("a.b.c", '.', true) = ["a", "b", "c"]
* Strings.split("a..b.c", '.', true) = ["a", "", "b", "c"]
* Strings.split("a:b:c", '.', true) = ["a:b:c"]
* Strings.split("a b c", ' ', true) = ["a", "b", "c"]
* Strings.split("a b c ", ' ', true) = ["a", "b", "c", ""]
* Strings.split("a b c ", ' ', true) = ["a", "b", "c", "", ""]
* Strings.split(" a b c", ' ', true) = ["", a", "b", "c"]
* Strings.split(" a b c", ' ', true) = ["", "", a", "b", "c"]
* Strings.split(" a b c ", ' ', true) = ["", a", "b", "c", ""]
* ```
*
* @param str the string to split
* @param separator the separator
* @param preserveAllTokens {@code true} if all tokens should be preserved, including empty tokens created by adjacent separators
* @return an array of strings
*/
public static String[] split(String str, char separator, boolean preserveAllTokens) {
if (str == null) {
return null;
}
int len = str.length();
if (len == 0) {
return EMPTY_STRING_ARRAY;
}
List<String> list = new ArrayList<>();
int i = 0, start = 0;
boolean match = false;
while (i < len) {
if (str.charAt(i) == separator) {
if (match || preserveAllTokens) {
list.add(str.substring(start, i));
match = false;
}
start = ++i;
continue;
}
match = true;
i++;
}
if (match || preserveAllTokens) {
list.add(str.substring(start, i));
}
String[] arr = new String[list.size()];
return list.toArray(arr);
}
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private Strings() {}
}