-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path132.java
More file actions
235 lines (213 loc) · 6.96 KB
/
Copy path132.java
File metadata and controls
235 lines (213 loc) · 6.96 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package info.xiancloud.core.util;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* String utility for general usage.
*
* @author happyyangyuan
*/
public class StringUtil {
public static boolean isEmpty(Object value) {
return null == value || value.equals("");
}
private static final String UNDERLINE = "_";
private static final int INDEX_NOT_FOUND = -1;
/**
* convert camel style string to underline style.
*/
public static String camelToUnderline(String param) {
if (param == null || "".equals(param.trim())) {
return "";
}
int len = param.length();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c = param.charAt(i);
if (Character.isUpperCase(c)) {
sb.append(UNDERLINE);
sb.append(Character.toLowerCase(c));
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* convert underline style string to camel style.
*/
public static String underlineToCamel(String param) {
if (isEmpty(param.trim())) {
return "";
}
StringBuilder sb = new StringBuilder(param);
Matcher mc = Pattern.compile(UNDERLINE).matcher(param);
int i = 0;
while (mc.find()) {
int position = mc.end() - (i++);
sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase());
}
return sb.toString();
}
/**
* string keys are converted from camel style to underline style. a new map is returned, the original map is not changed.
*/
public static Map<String, Object> camelToUnderline(Map<String, Object> map) {
Map<String, Object> newMap = new HashMap<>();
for (String key : map.keySet()) {
newMap.put(camelToUnderline(key), map.get(key));
}
return newMap;
}
/**
* the opposite with {@link #camelToUnderline(Map)}
*/
public static Map<String, Object> underlineToCamel(Map<String, Object> map) {
Map<String, Object> newMap = new HashMap<>();
for (String key : map.keySet()) {
newMap.put(underlineToCamel(key), map.get(key));
}
return newMap;
}
/**
* Judge whether the given decimal number's decimal length is in the given range.
* Talk is cheap, see the code.
*
* @param value the string represents the decimal number.
* @param digit the range for the decimal length.
*/
public static boolean isFitDigit(String value, int digit) {
return (value.split("\\.")[1].length() <= digit);
}
/**
* get the stacktrace string.
*
* @param t the throwable object.
* @return the stacktrace string.
*/
public static String getExceptionStacktrace(Throwable t) {
try (StringWriter errors = new StringWriter()) {
t.printStackTrace(new PrintWriter(errors));
return errors.toString();
} catch (IOException e) {
LOG.error(e);
return null;
}
}
/**
* reverseString
*/
public static String reverseString(String str) {
StringBuilder stringBuffer = new StringBuilder(str);
return stringBuffer.reverse().toString();
}
/**
* delete the comment things in the given string.
*/
public static String removeComment(String text) {
return text.replaceAll("(?<!:)\\/\\/.*|\\/\\*(\\s|.)*?\\*\\/", "");
}
/**
* Find the given object's index in the given array.
* Something like the {@link String#indexOf(String)}
*/
public static int indexOf(final Object[] array, final Object objectToFind) {
return indexOf(array, objectToFind, 0);
}
/**
* Find the given object's index in the given array.
* Something like the {@link String#indexOf(String)}
*/
public static int indexOf(final Object[] array, final Object objectToFind, int startIndex) {
if (array == null) {
return INDEX_NOT_FOUND;
}
if (startIndex < 0) {
startIndex = 0;
}
if (objectToFind == null) {
for (int i = startIndex; i < array.length; i++) {
if (array[i] == null) {
return i;
}
}
} else if (array.getClass().getComponentType().isInstance(objectToFind)) {
for (int i = startIndex; i < array.length; i++) {
if (objectToFind.equals(array[i])) {
return i;
}
}
}
return INDEX_NOT_FOUND;
}
/**
* escape the regex special character: ($()*+.[]?\^{},|)
*/
public static String escapeSpecialChar(String keyword) {
if (!isEmpty(keyword)) {
String[] fbsArr = {"\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|"};
for (String key : fbsArr) {
if (keyword.contains(key)) {
keyword = keyword.replace(key, "\\" + key);
}
}
}
return keyword;
}
/**
* firstCharToLowerCase
*/
public static String firstCharToLowerCase(String str) {
char firstChar = str.charAt(0);
if (firstChar >= 'A' && firstChar <= 'Z') {
char[] arr = str.toCharArray();
arr[0] += ('a' - 'A');
return new String(arr);
}
return str;
}
/**
* firstCharToUpperCase
*/
public static String firstCharToUpperCase(String str) {
char firstChar = str.charAt(0);
if (firstChar >= 'a' && firstChar <= 'z') {
char[] arr = str.toCharArray();
arr[0] -= ('a' - 'A');
return new String(arr);
}
return str;
}
public static boolean notNull(Object... paras) {
if (paras == null)
return false;
for (Object obj : paras)
if (obj == null)
return false;
return true;
}
/**
* split the given string into an array with the given splitter.
* note that all elements in the returned array are trimmed.
*
* @param str the string to be split, if null the split result is en empty string array.
* @param theSplitter theSplitter, regex special character will be escaped.
* @return spitted array or empty array if the input string is empty.
*/
public static String[] split(String str, String theSplitter) {
if (StringUtil.isEmpty(str)) {
return new String[0];
}
return str.trim().split("\\s*" + escapeSpecialChar(theSplitter) + "\\s*");
}
/**
* create a random num
*/
public static String createNum(int length) {
return RandomUtils.getRandomNumbers(length);
}
}