Commit 2d7ad8b12705f3828aa838d0ea8c71f4a9f473e1
1 parent
f1bf3d3d
feat(*): 优化项目代码
- 优化项目代码
Showing
9 changed files
with
199 additions
and
239 deletions
README.md
fw-hestia-common/src/main/java/cn/fw/hestia/common/utils/DateUtil.kt
1 | 1 | package cn.fw.hestia.common.utils |
2 | 2 | |
3 | -import org.slf4j.Logger | |
4 | 3 | import org.slf4j.LoggerFactory |
5 | 4 | import java.sql.Timestamp |
6 | 5 | import java.text.SimpleDateFormat |
... | ... | @@ -17,8 +16,8 @@ import java.util.* |
17 | 16 | */ |
18 | 17 | |
19 | 18 | object DateUtil { |
20 | - private val sdf: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd") | |
21 | - private val log: Logger = LoggerFactory.getLogger(this::class.java) | |
19 | + private val sdf = SimpleDateFormat("yyyy-MM-dd") | |
20 | + private val log = LoggerFactory.getLogger(this::class.java) | |
22 | 21 | fun parse(date: String?): Date? { |
23 | 22 | try { |
24 | 23 | return sdf.parse(date) |
... | ... | @@ -208,7 +207,7 @@ object DateUtil { |
208 | 207 | val x: Int = c.get(Calendar.YEAR) |
209 | 208 | try { |
210 | 209 | return sdf.parse("$x-01-01") |
211 | - } catch (e: Exception) { | |
210 | + } catch (_: Exception) { | |
212 | 211 | } |
213 | 212 | return null |
214 | 213 | } |
... | ... | @@ -224,7 +223,7 @@ object DateUtil { |
224 | 223 | val x: Int = c.get(Calendar.YEAR) |
225 | 224 | try { |
226 | 225 | return sdf.parse("$x-12-31") |
227 | - } catch (e: Exception) { | |
226 | + } catch (_: Exception) { | |
228 | 227 | } |
229 | 228 | return null |
230 | 229 | } |
... | ... | @@ -304,7 +303,7 @@ object DateUtil { |
304 | 303 | if (date == null) { |
305 | 304 | return "" |
306 | 305 | } |
307 | - val format: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss") | |
306 | + val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss") | |
308 | 307 | return format.format(date) |
309 | 308 | } |
310 | 309 | |
... | ... | @@ -318,7 +317,7 @@ object DateUtil { |
318 | 317 | if (date == null) { |
319 | 318 | return "" |
320 | 319 | } |
321 | - val format: SimpleDateFormat = SimpleDateFormat("yyyy-MM") | |
320 | + val format = SimpleDateFormat("yyyy-MM") | |
322 | 321 | return format.format(date) |
323 | 322 | } |
324 | 323 | |
... | ... | @@ -333,7 +332,7 @@ object DateUtil { |
333 | 332 | if (date == null) { |
334 | 333 | return "" |
335 | 334 | } |
336 | - val format: SimpleDateFormat = SimpleDateFormat(pattern) | |
335 | + val format = SimpleDateFormat(pattern) | |
337 | 336 | return format.format(date) |
338 | 337 | } |
339 | 338 | |
... | ... | @@ -481,7 +480,7 @@ object DateUtil { |
481 | 480 | if (d1 == null || d2 == null) { |
482 | 481 | return null |
483 | 482 | } |
484 | - var result: Long = 0L | |
483 | + var result = 0L | |
485 | 484 | val DAY: Long = 24 * 60 * 60 * 1000L |
486 | 485 | val sub: Long = d1.time - d2.time |
487 | 486 | val daysub: Long = (sub / DAY) |
... | ... | @@ -514,10 +513,10 @@ object DateUtil { |
514 | 513 | return false |
515 | 514 | } |
516 | 515 | val dateTime: LocalDateTime = date2LocalDateTime(date) |
517 | - val sdf: SimpleDateFormat = SimpleDateFormat("HH") | |
516 | + val sdf = SimpleDateFormat("HH") | |
518 | 517 | val str: String = sdf.format(localDateTime2Date(dateTime)) |
519 | 518 | val a: Int = str.toInt() |
520 | - return a >= 0 && a <= 6 | |
519 | + return a in 0..6 | |
521 | 520 | } |
522 | 521 | |
523 | 522 | /** |
... | ... | @@ -527,11 +526,11 @@ object DateUtil { |
527 | 526 | * @return |
528 | 527 | */ |
529 | 528 | fun isMorning(date: Date): Boolean { |
530 | - if (Objects.isNull(date)) { | |
529 | + if (!Objects.nonNull(date)) { | |
531 | 530 | return false |
532 | 531 | } |
533 | 532 | val dateTime: LocalDateTime = date2LocalDateTime(date) |
534 | - val sdf: SimpleDateFormat = SimpleDateFormat("HH") | |
533 | + val sdf = SimpleDateFormat("HH") | |
535 | 534 | val str: String = sdf.format(localDateTime2Date(dateTime)) |
536 | 535 | val a: Int = str.toInt() |
537 | 536 | return a in 7..12 |
... | ... | @@ -548,10 +547,10 @@ object DateUtil { |
548 | 547 | return false |
549 | 548 | } |
550 | 549 | val dateTime: LocalDateTime = date2LocalDateTime(date) |
551 | - val df: SimpleDateFormat = SimpleDateFormat("HH") | |
550 | + val df = SimpleDateFormat("HH") | |
552 | 551 | val str: String = df.format(localDateTime2Date(dateTime)) |
553 | 552 | val a: Int = str.toInt() |
554 | - return a > 12 && a <= 18 | |
553 | + return a in 13..18 | |
555 | 554 | } |
556 | 555 | |
557 | 556 | /** |
... | ... | @@ -565,9 +564,9 @@ object DateUtil { |
565 | 564 | return false |
566 | 565 | } |
567 | 566 | val dateTime: LocalDateTime = date2LocalDateTime(date) |
568 | - val df: SimpleDateFormat = SimpleDateFormat("HH") | |
567 | + val df = SimpleDateFormat("HH") | |
569 | 568 | val str: String = df.format(localDateTime2Date(dateTime)) |
570 | 569 | val a: Int = str.toInt() |
571 | - return a > 18 && a <= 23 | |
570 | + return a in 19..23 | |
572 | 571 | } |
573 | 572 | } | ... | ... |
fw-hestia-common/src/main/java/cn/fw/hestia/common/utils/StringUtils.kt
1 | -package cn.fw.hestia.common.utils; | |
1 | +package cn.fw.hestia.common.utils | |
2 | 2 | |
3 | - | |
4 | -import java.io.IOException; | |
5 | -import java.io.UnsupportedEncodingException; | |
6 | -import java.util.ArrayList; | |
7 | -import java.util.Arrays; | |
8 | -import java.util.List; | |
9 | -import java.util.UUID; | |
10 | -import java.util.regex.Matcher; | |
11 | -import java.util.regex.Pattern; | |
3 | +import java.io.UnsupportedEncodingException | |
4 | +import java.util.* | |
5 | +import java.util.regex.Pattern | |
12 | 6 | |
13 | 7 | /** |
14 | 8 | * 字符串工具 |
... | ... | @@ -16,21 +10,20 @@ import java.util.regex.Pattern; |
16 | 10 | * @author: kurisu |
17 | 11 | * @version: 1.0 |
18 | 12 | */ |
19 | -public final class StringUtils { | |
13 | + | |
14 | +object StringUtils { | |
20 | 15 | |
21 | 16 | /** |
22 | 17 | * 空字符串 |
23 | 18 | */ |
24 | - public static final String EMPTY = ""; | |
19 | + const val EMPTY: String = "" | |
20 | + | |
25 | 21 | /** |
26 | 22 | * 特殊字符正则表达式 |
27 | 23 | */ |
28 | - public static final String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]"; | |
24 | + const val regEx: String = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]" | |
29 | 25 | |
30 | - public static final String regNo = "^[-+]?\\d*$"; | |
31 | - | |
32 | - private StringUtils() { | |
33 | - } | |
26 | + const val regNo: String = "^[-+]?\\d*$" | |
34 | 27 | |
35 | 28 | /** |
36 | 29 | * 首字母小写 |
... | ... | @@ -38,11 +31,11 @@ public final class StringUtils { |
38 | 31 | * @param s String |
39 | 32 | * @return String |
40 | 33 | */ |
41 | - public static String firstCharLowerCase(String s) { | |
34 | + fun firstCharLowerCase(s: String): String { | |
42 | 35 | if (isValid(s)) { |
43 | - return s.substring(0, 1).toLowerCase() + s.substring(1); | |
36 | + return s.substring(0, 1).lowercase(Locale.getDefault()) + s.substring(1) | |
44 | 37 | } |
45 | - return s; | |
38 | + return s | |
46 | 39 | } |
47 | 40 | |
48 | 41 | /** |
... | ... | @@ -52,9 +45,9 @@ public final class StringUtils { |
52 | 45 | * @param prefix |
53 | 46 | * @return |
54 | 47 | */ |
55 | - public static String removePrefix(String s, String prefix) { | |
56 | - int index = s.indexOf(prefix); | |
57 | - return index == 0 ? s.substring(prefix.length()) : s; | |
48 | + fun removePrefix(s: String, prefix: String): String { | |
49 | + val index = s.indexOf(prefix) | |
50 | + return if (index == 0) s.substring(prefix.length) else s | |
58 | 51 | } |
59 | 52 | |
60 | 53 | /** |
... | ... | @@ -64,8 +57,8 @@ public final class StringUtils { |
64 | 57 | * @param suffix |
65 | 58 | * @return |
66 | 59 | */ |
67 | - public static String removeSuffix(String s, String suffix) { | |
68 | - return s.endsWith(suffix) ? s.substring(0, s.length() - suffix.length()) : s; | |
60 | + fun removeSuffix(s: String, suffix: String): String { | |
61 | + return if (s.endsWith(suffix)) s.substring(0, s.length - suffix.length) else s | |
69 | 62 | } |
70 | 63 | |
71 | 64 | /** |
... | ... | @@ -74,11 +67,11 @@ public final class StringUtils { |
74 | 67 | * @param s String |
75 | 68 | * @return String |
76 | 69 | */ |
77 | - public static String firstCharUpperCase(String s) { | |
70 | + fun firstCharUpperCase(s: String): String { | |
78 | 71 | if (isValid(s)) { |
79 | - return s.substring(0, 1).toUpperCase() + s.substring(1); | |
72 | + return s.substring(0, 1).uppercase(Locale.getDefault()) + s.substring(1) | |
80 | 73 | } |
81 | - return s; | |
74 | + return s | |
82 | 75 | } |
83 | 76 | |
84 | 77 | /** |
... | ... | @@ -87,8 +80,10 @@ public final class StringUtils { |
87 | 80 | * @param obj |
88 | 81 | * @return boolean |
89 | 82 | */ |
90 | - public static boolean isValid(Object obj) { | |
91 | - return obj != null && !obj.toString().isEmpty(); | |
83 | + fun isValid(obj: Any?): Boolean { | |
84 | + return obj?.run { | |
85 | + toString().isNotEmpty() | |
86 | + } ?: false | |
92 | 87 | } |
93 | 88 | |
94 | 89 | /** |
... | ... | @@ -97,8 +92,10 @@ public final class StringUtils { |
97 | 92 | * @param obj |
98 | 93 | * @return |
99 | 94 | */ |
100 | - public static boolean isEmpty(Object obj) { | |
101 | - return obj == null || obj.toString().isEmpty(); | |
95 | + fun isEmpty(obj: Any?): Boolean { | |
96 | + return obj?.run { | |
97 | + toString().isEmpty() | |
98 | + } ?: true | |
102 | 99 | } |
103 | 100 | |
104 | 101 | /** |
... | ... | @@ -107,12 +104,11 @@ public final class StringUtils { |
107 | 104 | * @param obj |
108 | 105 | * @return |
109 | 106 | */ |
110 | - public static boolean isNumber(Object obj) { | |
107 | + fun isNumber(obj: Any): Boolean { | |
111 | 108 | if (isEmpty(obj)) { |
112 | - return false; | |
109 | + return false | |
113 | 110 | } |
114 | - Pattern compile = Pattern.compile(regNo); | |
115 | - return compile.matcher(obj.toString()).matches(); | |
111 | + return Pattern.compile(regNo).matcher(obj.toString()).matches() | |
116 | 112 | } |
117 | 113 | |
118 | 114 | /** |
... | ... | @@ -121,8 +117,8 @@ public final class StringUtils { |
121 | 117 | * @param obj |
122 | 118 | * @return boolean |
123 | 119 | */ |
124 | - public static String asString(Object obj) { | |
125 | - return obj != null ? obj.toString() : ""; | |
120 | + fun asString(obj: Any?): String { | |
121 | + return obj?.toString() ?: "" | |
126 | 122 | } |
127 | 123 | |
128 | 124 | /** |
... | ... | @@ -130,14 +126,14 @@ public final class StringUtils { |
130 | 126 | * |
131 | 127 | * @param values |
132 | 128 | */ |
133 | - public static String tryThese(Object... values) { | |
134 | - for (int i = 0; i < values.length; i++) { | |
135 | - String value = StringUtils.asString(values[i]); | |
136 | - if (!value.isEmpty()) { | |
137 | - return value; | |
129 | + fun tryThese(vararg values: Any?): String { | |
130 | + for (o in values) { | |
131 | + val value = asString(o) | |
132 | + if (value.isNotEmpty()) { | |
133 | + return value | |
138 | 134 | } |
139 | 135 | } |
140 | - return ""; | |
136 | + return "" | |
141 | 137 | } |
142 | 138 | |
143 | 139 | /** |
... | ... | @@ -147,8 +143,8 @@ public final class StringUtils { |
147 | 143 | * @param v2 |
148 | 144 | * @return |
149 | 145 | */ |
150 | - public static String tryThese(String v1, String v2) { | |
151 | - return tryThese(new Object[]{v1, v2}); | |
146 | + fun tryThese(v1: String, v2: String): String { | |
147 | + return tryThese(*arrayOf<Any>(v1, v2)) | |
152 | 148 | } |
153 | 149 | |
154 | 150 | /** |
... | ... | @@ -158,8 +154,8 @@ public final class StringUtils { |
158 | 154 | * @param split |
159 | 155 | * @return 字符串 |
160 | 156 | */ |
161 | - public static <T> String join(T[] list, String split) { | |
162 | - return join(list, split, ""); | |
157 | + fun <T> join(list: Array<T>?, split: String?): String? { | |
158 | + return join(list, split, "") | |
163 | 159 | } |
164 | 160 | |
165 | 161 | /** |
... | ... | @@ -169,41 +165,18 @@ public final class StringUtils { |
169 | 165 | * @param split |
170 | 166 | * @return 字符串 |
171 | 167 | */ |
172 | - public static <T> String join(T[] list, String split, String wrap) { | |
168 | + fun <T> join(list: Array<T>?, split: String?, wrap: String?): String? { | |
173 | 169 | if (list == null) { |
174 | - return null; | |
170 | + return null | |
175 | 171 | } |
176 | - StringBuilder s = new StringBuilder(128); | |
177 | - for (int i = 0; i < list.length; i++) { | |
172 | + val s = StringBuilder(128) | |
173 | + for (i in list.indices) { | |
178 | 174 | if (i > 0) { |
179 | - s.append(split); | |
175 | + s.append(split) | |
180 | 176 | } |
181 | - s.append(wrap).append(list[i]).append(wrap); | |
177 | + s.append(wrap).append(list[i]).append(wrap) | |
182 | 178 | } |
183 | - return s.toString(); | |
184 | - } | |
185 | - | |
186 | - /** | |
187 | - * 连接 | |
188 | - * | |
189 | - * @param list | |
190 | - * @param split | |
191 | - * @param wrap | |
192 | - * @return | |
193 | - */ | |
194 | - public static <T> String join(List<T> list, String split, String wrap) { | |
195 | - return join(list.toArray(), split, wrap); | |
196 | - } | |
197 | - | |
198 | - /** | |
199 | - * 连接字符串 | |
200 | - * | |
201 | - * @param list | |
202 | - * @param split | |
203 | - * @return 字符串 | |
204 | - */ | |
205 | - public static String join(List<?> list, String split) { | |
206 | - return join(list.toArray(), split); | |
179 | + return s.toString() | |
207 | 180 | } |
208 | 181 | |
209 | 182 | /** |
... | ... | @@ -214,14 +187,15 @@ public final class StringUtils { |
214 | 187 | * @param end } |
215 | 188 | * @return String |
216 | 189 | */ |
217 | - public static String wrap(String begin, String input, String end) { | |
218 | - if (!input.startsWith(begin)) { | |
219 | - input = begin + input; | |
190 | + fun wrap(begin: String, input: String, end: String): String { | |
191 | + var inputStr = input | |
192 | + if (!inputStr.startsWith(begin)) { | |
193 | + inputStr = begin + input | |
220 | 194 | } |
221 | - if (!input.endsWith(end)) { | |
222 | - input = input + end; | |
195 | + if (!inputStr.endsWith(end)) { | |
196 | + inputStr += end | |
223 | 197 | } |
224 | - return input; | |
198 | + return inputStr | |
225 | 199 | } |
226 | 200 | |
227 | 201 | /** |
... | ... | @@ -231,8 +205,8 @@ public final class StringUtils { |
231 | 205 | * @param regex |
232 | 206 | * @return |
233 | 207 | */ |
234 | - public static List<String> matchs(String input, String regex) { | |
235 | - return matchs(input, regex, 0); | |
208 | + fun matchs(input: String, regex: String): List<String> { | |
209 | + return matchs(input, regex, 0) | |
236 | 210 | } |
237 | 211 | |
238 | 212 | /** |
... | ... | @@ -242,14 +216,14 @@ public final class StringUtils { |
242 | 216 | * @param regex |
243 | 217 | * @return |
244 | 218 | */ |
245 | - public static List<String> matchs(String input, String regex, int group) { | |
246 | - Pattern pattern = Pattern.compile(regex); | |
247 | - Matcher match = pattern.matcher(input); | |
248 | - List<String> matches = new ArrayList<String>(); | |
219 | + fun matchs(input: CharSequence, regex: String, group: Int): List<String> { | |
220 | + val pattern = Pattern.compile(regex) | |
221 | + val match = pattern.matcher(input) | |
222 | + val matches: MutableList<String> = ArrayList() | |
249 | 223 | while (match.find()) { |
250 | - matches.add(match.group(group)); | |
224 | + matches.add(match.group(group)) | |
251 | 225 | } |
252 | - return matches; | |
226 | + return matches | |
253 | 227 | } |
254 | 228 | |
255 | 229 | /** |
... | ... | @@ -260,9 +234,9 @@ public final class StringUtils { |
260 | 234 | * @param group |
261 | 235 | * @return |
262 | 236 | */ |
263 | - public static String matchFirst(String input, String regex, int group) { | |
264 | - List<String> matches = matchs(input, regex, group); | |
265 | - return matches.isEmpty() ? null : matches.get(0); | |
237 | + fun matchFirst(input: String, regex: String, group: Int): String? { | |
238 | + val matches = matchs(input, regex, group) | |
239 | + return if (matches.isEmpty()) null else matches[0] | |
266 | 240 | } |
267 | 241 | |
268 | 242 | /** |
... | ... | @@ -270,8 +244,8 @@ public final class StringUtils { |
270 | 244 | * |
271 | 245 | * @return |
272 | 246 | */ |
273 | - public static String getShorterString(String str, int maxLength) { | |
274 | - return getShorterString(str, "...", maxLength); | |
247 | + fun getShorterString(str: String, maxLength: Int): String { | |
248 | + return getShorterString(str, "...", maxLength) | |
275 | 249 | } |
276 | 250 | |
277 | 251 | /** |
... | ... | @@ -282,32 +256,35 @@ public final class StringUtils { |
282 | 256 | * @param length |
283 | 257 | * @return |
284 | 258 | */ |
285 | - public static String getShorterString(String input, String tail, int length) { | |
286 | - tail = isValid(tail) ? tail : ""; | |
287 | - StringBuffer buffer = new StringBuffer(512); | |
259 | + fun getShorterString(input: String, tail: String?, length: Int): String { | |
260 | + var tailStr = tail | |
261 | + tailStr = if (isValid(tailStr)) tailStr else "" | |
262 | + val buffer = StringBuffer(512) | |
288 | 263 | try { |
289 | - int len = input.getBytes("GBK").length; | |
264 | + val len = input.toByteArray(charset("GBK")).size | |
290 | 265 | if (len > length) { |
291 | - int ln = 0; | |
292 | - for (int i = 0; ln < length; i++) { | |
293 | - String temp = input.substring(i, i + 1); | |
294 | - if (temp.getBytes("GBK").length == 2) { | |
295 | - ln += 2; | |
266 | + var ln = 0 | |
267 | + var i = 0 | |
268 | + while (ln < length) { | |
269 | + val temp = input.substring(i, i + 1) | |
270 | + if (temp.toByteArray(charset("GBK")).size == 2) { | |
271 | + ln += 2 | |
296 | 272 | } else { |
297 | - ln++; | |
273 | + ln++ | |
298 | 274 | } |
299 | 275 | |
300 | 276 | if (ln <= length) { |
301 | - buffer.append(temp); | |
277 | + buffer.append(temp) | |
302 | 278 | } |
279 | + i++ | |
303 | 280 | } |
304 | 281 | } else { |
305 | - return input; | |
282 | + return input | |
306 | 283 | } |
307 | - buffer.append(tail); | |
308 | - } catch (UnsupportedEncodingException ignored) { | |
284 | + buffer.append(tailStr) | |
285 | + } catch (ignored: UnsupportedEncodingException) { | |
309 | 286 | } |
310 | - return buffer.toString(); | |
287 | + return buffer.toString() | |
311 | 288 | } |
312 | 289 | |
313 | 290 | /** |
... | ... | @@ -315,12 +292,12 @@ public final class StringUtils { |
315 | 292 | * |
316 | 293 | * @return |
317 | 294 | */ |
318 | - public static String getBytesString(String input, String code) { | |
295 | + fun getBytesString(input: String, code: String): String { | |
319 | 296 | try { |
320 | - byte[] b = input.getBytes(code); | |
321 | - return Arrays.toString(b); | |
322 | - } catch (UnsupportedEncodingException e) { | |
323 | - return String.valueOf(code.hashCode()); | |
297 | + val b = input.toByteArray(charset(code)) | |
298 | + return b.contentToString() | |
299 | + } catch (e: UnsupportedEncodingException) { | |
300 | + return code.hashCode().toString() | |
324 | 301 | } |
325 | 302 | } |
326 | 303 | |
... | ... | @@ -330,21 +307,20 @@ public final class StringUtils { |
330 | 307 | * @param input |
331 | 308 | * @return |
332 | 309 | */ |
333 | - public static String getFieldString(String input) { | |
334 | - if (input == null) { | |
335 | - return null; | |
336 | - } | |
337 | - String field = input.toLowerCase(); | |
338 | - String[] values = field.split("\\_"); | |
339 | - StringBuffer b = new StringBuffer(input.length()); | |
340 | - for (int i = 0; i < values.length; i++) { | |
341 | - if (i == 0) { | |
342 | - b.append(values[i]); | |
343 | - } else { | |
344 | - b.append(firstCharUpperCase(values[i])); | |
310 | + fun getFieldString(input: String?): String? { | |
311 | + return input?.run { | |
312 | + val field = lowercase(Locale.getDefault()) | |
313 | + val values = field.split("_".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | |
314 | + val b = StringBuffer(length) | |
315 | + for (i in values.indices) { | |
316 | + if (i == 0) { | |
317 | + b.append(values[i]) | |
318 | + } else { | |
319 | + b.append(firstCharUpperCase(values[i])) | |
320 | + } | |
345 | 321 | } |
322 | + b.toString() | |
346 | 323 | } |
347 | - return b.toString(); | |
348 | 324 | } |
349 | 325 | |
350 | 326 | /** |
... | ... | @@ -353,8 +329,8 @@ public final class StringUtils { |
353 | 329 | * @param columnName |
354 | 330 | * @return |
355 | 331 | */ |
356 | - public static String toFieldName(String columnName) { | |
357 | - return getFieldString(columnName); | |
332 | + fun toFieldName(columnName: String?): String? { | |
333 | + return getFieldString(columnName) | |
358 | 334 | } |
359 | 335 | |
360 | 336 | /** |
... | ... | @@ -363,19 +339,19 @@ public final class StringUtils { |
363 | 339 | * @param field |
364 | 340 | * @return |
365 | 341 | */ |
366 | - public static String toColumnName(String field) { | |
342 | + fun toColumnName(field: String?): String? { | |
367 | 343 | if (field == null) { |
368 | - return null; | |
344 | + return null | |
369 | 345 | } |
370 | - StringBuffer b = new StringBuffer(field.length() + 3); | |
371 | - for (int i = 0; i < field.length(); i++) { | |
372 | - Character char1 = field.charAt(i); | |
346 | + val b = StringBuffer(field.length + 3) | |
347 | + for (i in field.indices) { | |
348 | + val char1 = field[i] | |
373 | 349 | if (Character.isUpperCase(char1) && i != 0) { |
374 | - b.append("_"); | |
350 | + b.append("_") | |
375 | 351 | } |
376 | - b.append(char1); | |
352 | + b.append(char1) | |
377 | 353 | } |
378 | - return b.toString(); | |
354 | + return b.toString() | |
379 | 355 | } |
380 | 356 | |
381 | 357 | /** |
... | ... | @@ -385,11 +361,11 @@ public final class StringUtils { |
385 | 361 | * @return |
386 | 362 | * @throws IOException |
387 | 363 | */ |
388 | - public static String toJsonValue(Object value) { | |
389 | - if (value instanceof Number) { | |
390 | - return value.toString(); | |
364 | + fun toJsonValue(value: Any): String { | |
365 | + return if (value is Number) { | |
366 | + value.toString() | |
391 | 367 | } else { |
392 | - return "'" + value.toString() + "'"; | |
368 | + "'$value'" | |
393 | 369 | } |
394 | 370 | } |
395 | 371 | |
... | ... | @@ -399,11 +375,11 @@ public final class StringUtils { |
399 | 375 | * @param value |
400 | 376 | * @return |
401 | 377 | */ |
402 | - public static String toUUID(String value) { | |
378 | + fun toUUID(value: String?): String { | |
403 | 379 | if (value == null) { |
404 | - throw new RuntimeException("value is null!"); | |
380 | + throw RuntimeException("value is null!") | |
405 | 381 | } |
406 | - return UUID.nameUUIDFromBytes(value.getBytes()).toString(); | |
382 | + return UUID.nameUUIDFromBytes(value.toByteArray()).toString() | |
407 | 383 | } |
408 | 384 | |
409 | 385 | /** |
... | ... | @@ -413,45 +389,45 @@ public final class StringUtils { |
413 | 389 | * @param styleName |
414 | 390 | * @return 相应的值 |
415 | 391 | */ |
416 | - public static String getStyleValue(String styleString, String styleName) { | |
417 | - String[] styles = styleString.split(";"); | |
418 | - for (int i = 0; i < styles.length; i++) { | |
419 | - String tempValue = styles[i].trim(); | |
420 | - if (tempValue.startsWith(styleName)) { | |
421 | - String[] style = tempValue.split(":"); | |
422 | - return style[1]; | |
392 | + fun getStyleValue(styleString: String, styleName: String?): String { | |
393 | + val styles = styleString.split(";".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | |
394 | + for (s in styles) { | |
395 | + val tempValue = s.trim { it <= ' ' } | |
396 | + if (tempValue.startsWith(styleName!!)) { | |
397 | + val style = tempValue.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | |
398 | + return style[1] | |
423 | 399 | } |
424 | 400 | } |
425 | - return ""; | |
401 | + return "" | |
426 | 402 | } |
427 | 403 | |
428 | 404 | /** |
429 | 405 | * 生成重复次字符 |
430 | 406 | * |
431 | - * @param charactor | |
407 | + * @param character | |
432 | 408 | * @param repeat |
433 | 409 | * @return |
434 | 410 | */ |
435 | - public static String getRepeat(String charactor, int repeat) { | |
436 | - return repeat(charactor, repeat, ""); | |
411 | + fun getRepeat(character: String, repeat: Int): String { | |
412 | + return repeat(character, repeat, "") | |
437 | 413 | } |
438 | 414 | |
439 | 415 | /** |
440 | 416 | * 生成重复次字符 |
441 | 417 | * |
442 | - * @param charactor | |
418 | + * @param character | |
443 | 419 | * @param repeat |
444 | 420 | * @return |
445 | 421 | */ |
446 | - public static String repeat(String charactor, int repeat, String split) { | |
447 | - StringBuilder s = new StringBuilder(charactor.length() * repeat); | |
448 | - for (int i = 0; i < repeat; i++) { | |
422 | + fun repeat(character: String, repeat: Int, split: String?): String { | |
423 | + val s = StringBuilder(character.length * repeat) | |
424 | + for (i in 0 until repeat) { | |
449 | 425 | if (i != 0) { |
450 | - s.append(split != null ? split : ""); | |
426 | + s.append(split ?: "") | |
451 | 427 | } |
452 | - s.append(charactor); | |
428 | + s.append(character) | |
453 | 429 | } |
454 | - return s.toString(); | |
430 | + return s.toString() | |
455 | 431 | } |
456 | 432 | |
457 | 433 | /** |
... | ... | @@ -460,13 +436,13 @@ public final class StringUtils { |
460 | 436 | * @param text |
461 | 437 | * @return |
462 | 438 | */ |
463 | - public static int length(String text) { | |
464 | - int len = text.length(); | |
439 | + fun length(text: String): Int { | |
440 | + var len = text.length | |
465 | 441 | try { |
466 | - len = text.getBytes("GBK").length;//SQLServer数据库用的GBK编码 | |
467 | - } catch (UnsupportedEncodingException ignored) { | |
442 | + len = text.toByteArray(charset("GBK")).size //SQLServer数据库用的GBK编码 | |
443 | + } catch (ignored: UnsupportedEncodingException) { | |
468 | 444 | } |
469 | - return len; | |
445 | + return len | |
470 | 446 | } |
471 | 447 | |
472 | 448 | /** |
... | ... | @@ -476,16 +452,16 @@ public final class StringUtils { |
476 | 452 | * @param data from 旧值 |
477 | 453 | * @param to from 新值 |
478 | 454 | */ |
479 | - public static String replaceString(String data, String from, String to) { | |
480 | - StringBuffer buf = new StringBuffer(data.length()); | |
481 | - int pos = -1; | |
482 | - int i = 0; | |
483 | - while ((pos = data.indexOf(from, i)) != -1) { | |
484 | - buf.append(data.substring(i, pos)).append(to); | |
485 | - i = pos + from.length(); | |
455 | + fun replaceString(data: String, from: String, to: String?): String { | |
456 | + val buf = StringBuffer(data.length) | |
457 | + var pos: Int | |
458 | + var i = 0 | |
459 | + while ((data.indexOf(from, i).also { pos = it }) != -1) { | |
460 | + buf.append(data.substring(i, pos)).append(to) | |
461 | + i = pos + from.length | |
486 | 462 | } |
487 | - buf.append(data.substring(i)); | |
488 | - return buf.toString(); | |
463 | + buf.append(data.substring(i)) | |
464 | + return buf.toString() | |
489 | 465 | } |
490 | 466 | |
491 | 467 | /** |
... | ... | @@ -494,24 +470,22 @@ public final class StringUtils { |
494 | 470 | * @param s |
495 | 471 | * @return |
496 | 472 | */ |
497 | - public static String escapeQueryChars(String s) { | |
498 | - if (StringUtils.isEmpty(s)) { | |
499 | - return s; | |
473 | + fun escapeQueryChars(s: String): String { | |
474 | + if (isEmpty(s)) { | |
475 | + return s | |
500 | 476 | } |
501 | - StringBuilder sb = new StringBuilder(); | |
477 | + val sb = StringBuilder() | |
502 | 478 | //查询字符串一般不会太长,挨个遍历也花费不了多少时间 |
503 | - for (int i = 0; i < s.length(); i++) { | |
504 | - char c = s.charAt(i); | |
505 | - if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' | |
506 | - || c == ':' || c == '^' || c == '[' || c == ']' || c == '\"' | |
507 | - || c == '{' || c == '}' || c == '~' || c == '*' || c == '?' | |
508 | - || c == '|' || c == '&' || c == ';' || c == '/' || c == '.' | |
509 | - || c == '$' || c == '%' || c == '<' || c == '>' || Character.isWhitespace(c)) { | |
510 | - sb.append('\\'); | |
479 | + for (element in s) { | |
480 | + if (element == '\\' || element == '+' || element == '-' || element == '!' || element == '(' || element == ')' || element == ':' || element == '^' || element == '[' || element == ']' || element == '\"' || element == '{' || element == '}' || element == '~' || element == '*' || element == '?' || element == '|' || element == '&' || element == ';' || element == '/' || element == '.' || element == '$' || element == '%' || element == '<' || element == '>' || Character.isWhitespace( | |
481 | + element | |
482 | + ) | |
483 | + ) { | |
484 | + sb.append('\\') | |
511 | 485 | } |
512 | - sb.append(c); | |
486 | + sb.append(element) | |
513 | 487 | } |
514 | - return sb.toString(); | |
488 | + return sb.toString() | |
515 | 489 | } |
516 | 490 | |
517 | 491 | |
... | ... | @@ -522,23 +496,14 @@ public final class StringUtils { |
522 | 496 | * @param defaultStr |
523 | 497 | * @return |
524 | 498 | */ |
525 | - public static String getStrWithDefault(CharSequence str, String defaultStr) { | |
526 | - if (isEmpty(defaultStr)) { | |
527 | - defaultStr = ""; | |
499 | + fun getStrWithDefault(str: CharSequence, defaultStr: String?): String? { | |
500 | + var defaultString = defaultStr | |
501 | + if (isEmpty(defaultString)) { | |
502 | + defaultString = "" | |
528 | 503 | } |
529 | 504 | if (isEmpty(str)) { |
530 | - return defaultStr; | |
505 | + return defaultString | |
531 | 506 | } |
532 | - return str.toString(); | |
533 | - } | |
534 | - | |
535 | - public static void main(String[] args) { | |
536 | - System.out.println(toUUID("1")); | |
537 | - System.out.println(removePrefix("abcd123", "ab")); | |
538 | - System.out.println(removeSuffix("abcd123", "123")); | |
539 | - System.out.println(toColumnName("usernameId")); | |
540 | - System.out.println(getFieldString("user_name_id")); | |
541 | - System.out.println(repeat("?", 10, ",")); | |
542 | - length("AAA中国()111222bb"); | |
507 | + return str.toString() | |
543 | 508 | } |
544 | 509 | } |
545 | 510 | \ No newline at end of file | ... | ... |
fw-hestia-dao/src/main/java/cn/fw/hestia/dao/MessageHistoryDao.kt
... | ... | @@ -13,5 +13,4 @@ import org.springframework.stereotype.Repository |
13 | 13 | * @date : 2023-12-18 09:44 |
14 | 14 | */ |
15 | 15 | @Repository |
16 | -interface MessageHistoryDao : BaseMapper<MessageHistory> { | |
17 | -} | |
18 | 16 | \ No newline at end of file |
17 | +interface MessageHistoryDao : BaseMapper<MessageHistory> | |
19 | 18 | \ No newline at end of file | ... | ... |
fw-hestia-dao/src/main/java/cn/fw/hestia/dao/SendLogDao.kt
... | ... | @@ -13,5 +13,4 @@ import org.springframework.stereotype.Repository |
13 | 13 | * @date : 2023-12-18 09:45 |
14 | 14 | */ |
15 | 15 | @Repository |
16 | -interface SendLogDao : BaseMapper<SendLog> { | |
17 | -} | |
18 | 16 | \ No newline at end of file |
17 | +interface SendLogDao : BaseMapper<SendLog> | |
19 | 18 | \ No newline at end of file | ... | ... |
fw-hestia-rpc/src/main/java/cn/fw/hestia/rpc/AbsBaseRpcService.java
... | ... | @@ -49,7 +49,7 @@ public abstract class AbsBaseRpcService { |
49 | 49 | @Nullable |
50 | 50 | protected <E> E getFromCache(@NonNull final String key, Class<E> clazz) { |
51 | 51 | String cache = getFromCache(key); |
52 | - if (StringUtils.isEmpty(cache)) { | |
52 | + if (StringUtils.INSTANCE.isEmpty(cache)) { | |
53 | 53 | return null; |
54 | 54 | } |
55 | 55 | return JSON.parseObject(cache, clazz); | ... | ... |
fw-hestia-rpc/src/main/java/cn/fw/hestia/rpc/passport/TemplateMessageService.java
... | ... | @@ -41,12 +41,12 @@ public class TemplateMessageService { |
41 | 41 | WxMpTempMessageParam param = new WxMpTempMessageParam(); |
42 | 42 | param.setCusId(messageParam.getMemberId()); |
43 | 43 | param.setTempId(messageParam.getTemplateCode()); |
44 | - if (StringUtils.isValid(messageParam.getKeywords())) { | |
44 | + if (StringUtils.INSTANCE.isValid(messageParam.getKeywords())) { | |
45 | 45 | List<WxMpTempMessageData> keywords = JSONArray.parseArray(messageParam.getKeywords(), WxMpTempMessageData.class); |
46 | 46 | param.setKeyWordList(keywords); |
47 | 47 | } |
48 | 48 | String pagePath = getPagePath(messageParam.getPath(), messageParam.getSceneToken()); |
49 | - if (StringUtils.isValid(pagePath)) { | |
49 | + if (StringUtils.INSTANCE.isValid(pagePath)) { | |
50 | 50 | param.setPagePath(pagePath); |
51 | 51 | } |
52 | 52 | Message<?> msg = wxMpTemplateMessageApi.send(param); |
... | ... | @@ -63,11 +63,11 @@ public class TemplateMessageService { |
63 | 63 | |
64 | 64 | |
65 | 65 | private String getPagePath(String path, Long sceneToken) { |
66 | - if (StringUtils.isEmpty(path)) { | |
66 | + if (StringUtils.INSTANCE.isEmpty(path)) { | |
67 | 67 | return null; |
68 | 68 | } |
69 | 69 | StringBuilder sb = new StringBuilder(path); |
70 | - if (StringUtils.isValid(path)) { | |
70 | + if (StringUtils.INSTANCE.isValid(path)) { | |
71 | 71 | sb.append("?sceneToken=").append(sceneToken); |
72 | 72 | } |
73 | 73 | return sb.toString(); | ... | ... |
fw-hestia-service/src/main/java/cn/fw/hestia/service/buz/MessageCenterBizService.kt
... | ... | @@ -50,7 +50,6 @@ class MessageCenterBizService( |
50 | 50 | private val sendMsgProducer: SendMsgProducer, |
51 | 51 | private val redisTemplate: StringRedisTemplate, |
52 | 52 | private val settingProperty: SettingProperty, |
53 | - @Value("\${spring.cache.custom.global-prefix}:template:code") private val keyPrefix: String, | |
54 | 53 | @Value("\${spring.cache.custom.global-prefix}:template:send:scene:") |
55 | 54 | private val prefix: String |
56 | 55 | ) { |
... | ... | @@ -293,7 +292,7 @@ class MessageCenterBizService( |
293 | 292 | } |
294 | 293 | } |
295 | 294 | |
296 | - val keywords = Arrays.asList( | |
295 | + val keywords = listOf( | |
297 | 296 | WxMpTempMessageData(settingProperty.typeSort1, param.title), |
298 | 297 | WxMpTempMessageData(settingProperty.typeSort2, if (StringUtils.isEmpty(param.remark)) "~" else param.remark) |
299 | 298 | ) | ... | ... |
fw-hestia-service/src/main/java/cn/fw/hestia/service/data/SendLogService.kt
... | ... | @@ -11,5 +11,4 @@ import com.baomidou.mybatisplus.extension.service.IService |
11 | 11 | * @desc : 消息发送日志服务 |
12 | 12 | * @date : 2023-12-18 09:47 |
13 | 13 | */ |
14 | -interface SendLogService : IService<SendLog> { | |
15 | -} | |
16 | 14 | \ No newline at end of file |
15 | +interface SendLogService : IService<SendLog> | |
17 | 16 | \ No newline at end of file | ... | ... |