18045010223
2025-07-07 0d3a683a0c97154b1f2e6657398664537e4e3e82
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
package org.yzh.commons.util;
 
import org.springframework.util.StringUtils;
 
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;
import java.util.function.Supplier;
 
/**
 * @author yezhihao
 * https://gitee.com/yezhihao/jt808-server
 */
public class StrUtils {
 
    public static <T> T ifNull(T obj, T defObj) {
        return (obj != null) ? obj : defObj;
    }
 
    public static <T> T ifNull(T obj, Supplier<T> defObj) {
        return (obj != null) ? obj : defObj.get();
    }
 
    public static String ifBlank(String str, String defStr) {
        return (str == null || str.isBlank()) ? defStr : str;
    }
 
    public static Long parseLong(String num) {
        return parseLong(num, null);
    }
 
    public static Long parseLong(String num, Long defVal) {
        try {
            return Long.parseLong(num);
        } catch (NumberFormatException e) {
            return defVal;
        }
    }
 
    public static String toUnderline(String str) {
        StringBuilder result = new StringBuilder(str.length() + 4);
        char[] chars = str.toCharArray();
 
        result.append(Character.toLowerCase(chars[0]));
 
        for (int i = 1; i < chars.length; i++) {
            char c = chars[i];
            if (Character.isUpperCase(c))
                result.append('_').append(Character.toLowerCase(c));
            else
                result.append(c);
        }
        return result.toString();
    }
 
    public static String leftPad(String str, int size, char ch) {
        int length = str.length();
        int pads = size - length;
        if (pads > 0) {
            char[] result = new char[size];
            str.getChars(0, length, result, pads);
            while (pads > 0)
                result[--pads] = ch;
            return new String(result);
        }
        return str;
    }
 
    public static String rightPad(String str, int size, char ch) {
        int length = str.length();
        if (length < size) {
            char[] result = new char[size];
            str.getChars(0, length, result, 0);
            while (length < size)
                result[length++] = ch;
            return new String(result);
        }
        return str;
    }
 
    public static Set<Integer> toSet(int... num) {
        if (num == null || num.length == 0) {
            return Set.of();
        }
        Set<Integer> result;
        if (num.length <= 3) {
            result = new TreeSet<>();
        } else {
            result = new HashSet<>(num.length << 1);
        }
        for (int i : num) {
            result.add(i);
        }
        return result;
    }
 
    public static boolean isNum(CharSequence val) {
        if (!StringUtils.hasText(val)) {
            return false;
        }
        int sz = val.length();
        for (int i = 0; i < sz; i++) {
            if (!Character.isDigit(val.charAt(i))) {
                return false;
            }
        }
        return true;
    }
 
    public static String truncateDecimal(double num, int maximumFractionDigits) {
        return truncateDecimal(Double.toString(num), maximumFractionDigits);
    }
 
    public static String truncateDecimal(String num, int maximumFractionDigits) {
        return truncateDecimal(num, maximumFractionDigits, new StringBuilder(10)).toString();
    }
 
    public static StringBuilder truncateDecimal(double num, int maximumFractionDigits, StringBuilder sb) {
        return truncateDecimal(Double.toString(num), maximumFractionDigits, sb);
    }
 
    public static StringBuilder truncateDecimal(String num, int maximumFractionDigits, StringBuilder sb) {
        int end = num.indexOf('.') + 1 + maximumFractionDigits;
        if (end < num.length()) {
            sb.append(num, 0, end);
        } else {
            sb.append(num);
        }
        return sb;
    }
 
    private static final char[] hexCode = "0123456789abcdef".toCharArray();
 
    public static String bytes2Hex(byte[] bytes) {
        char[] hex = new char[bytes.length << 1];
        for (int j = 0, i = 0; i < bytes.length; i++) {
            byte b = bytes[i];
            hex[j++] = hexCode[(b >> 4) & 0xF];
            hex[j++] = hexCode[(b & 0xF)];
        }
        return new String(hex);
    }
 
    public static byte[] hex2Bytes(String hex) {
        final int len = hex.length();
 
        if (len % 2 != 0) {
            throw new IllegalArgumentException("hexBinary needs to be even-length: " + hex);
        }
 
        byte[] out = new byte[len >> 1];
        for (int i = 0; i < len; i += 2) {
 
            int h = hexToBin(hex.charAt(i));
            int l = hexToBin(hex.charAt(i + 1));
            if (h == -1 || l == -1) {
                throw new IllegalArgumentException("contains illegal character for hexBinary: " + hex);
            }
            out[i >> 1] = (byte) (h * 16 + l);
        }
        return out;
    }
 
    public static int hexToBin(char ch) {
        if ('0' <= ch && ch <= '9') {
            return ch - '0';
        }
        if ('A' <= ch && ch <= 'F') {
            return ch - ('A' - 10);
        }
        if ('a' <= ch && ch <= 'f') {
            return ch - ('a' - 10);
        }
        return -1;
    }
 
    public static String simpleUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }
}