#
whycq
2024-08-15 e54492fb532366faac8aa5d46ad8905ea3ff38d5
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package com.example.agvcontroller.protocol;
 
 
import java.lang.reflect.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
/**
 * Created by vincent on 2019-06-09
 */
public class Cools {
 
    public static boolean isEmpty(Object... objects) {
        for (Object obj : objects){
            if (isEmpty(obj)){
                return true;
            }
        }
        return false;
    }
 
    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Object o) {
        if (o == null) {
            return true;
        }
        if (o instanceof String) {
            if (o.toString().trim().equals("")) {
                return true;
            }
        } else if (o instanceof List) {
            if (((List) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Map) {
            if (((Map) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Set) {
            if (((Set) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Object[]) {
            if (((Object[]) o).length == 0) {
                return true;
            }
        } else if (o instanceof int[]) {
            if (((int[]) o).length == 0) {
                return true;
            }
        } else if (o instanceof long[]) {
            if (((long[]) o).length == 0) {
                return true;
            }
        }
        return false;
    }
 
    public static int sqlLimitIndex(Integer pageIndex, Integer pageSize){
        return (pageIndex - 1) * pageSize;
    }
 
    //public static String enToken(String username, String password){
    //    return AesUtils.encrypt(username, zerofill(password, 16));
    //}
 
    //public static String deTokn(String token, String password){
    //    //return AesUtils.decrypt(token, zerofill(password, 16));
    //}
 
    public static String zerofill(String msg, Integer count){
        if (msg.length() == count){
            return msg;
        } else if (msg.length() > count){
            return msg.substring(0, 16);
        } else {
            StringBuilder msgBuilder = new StringBuilder(msg);
            for (int i = 0; i<count-msg.length(); i++){
                msgBuilder.append("0");
            }
            return msgBuilder.toString();
        }
    }
 
    /**
     * 截取字符串(默认end=true)
     * @param str 被截字符串
     * @param end true:最后一个字符 / false:第一个字符
     */
    public static String deleteChar(String str, boolean end){
        if (isEmpty(str)){
            return "";
        }
        if (end){
            return str.substring(0, str.length()-1);
        } else {
            return str.substring(1);
        }
    }
 
    public static String deleteChar(String str){
        return deleteChar(str, true);
    }
 
 
    /**
     * map 转 对象
     */
    public static <T> T conver(Map<? extends String, ?> map, Class<T> cls){
        T instance = null;
        try {
            Constructor<T> constructor = cls.getDeclaredConstructor();
            boolean constructorAccessible = constructor.isAccessible();
            constructor.setAccessible(true);
            instance = constructor.newInstance();
            constructor.setAccessible(constructorAccessible);
        } catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }
        Class<?> prototype = cls;
        do {
            for (Field field : prototype.getDeclaredFields()){
                if (Modifier.isFinal(field.getModifiers())
                        || Modifier.isStatic(field.getModifiers())
                        || Modifier.isTransient(field.getModifiers())){
                    continue;
                }
                String fieldName = field.getName();
                Object val = null;
                if (map.containsKey(fieldName)){
                    val = map.get(fieldName);
                }
                if (val != null){
                    boolean fieldAccessible = field.isAccessible();
                    field.setAccessible(true);
                    Class<?> type = field.getType();
                    try {
                        Constructor<?> constructor = type.getDeclaredConstructor(String.class);
                        boolean constructorAccessible = constructor.isAccessible();
                        constructor.setAccessible(true);
                        field.set(instance, constructor.newInstance(String.valueOf(val)));
                        constructor.setAccessible(constructorAccessible);
                    } catch (IllegalAccessException
                            | InstantiationException
                            | InvocationTargetException
                            | NoSuchMethodException e) {
                        System.err.println("convert error ===> Class["+prototype+"],Field:["+fieldName+"],Type:["+type+"],Value:["+val+"]");
                    }
                    field.setAccessible(fieldAccessible);
                }
            }
            prototype = prototype.getSuperclass();
        } while (!Object.class.equals(prototype));
        return instance;
    }
 
    /**
     * 对象 转 map
     */
    public static Map<String, Object> conver(Object obj){
        Class<?> cls = obj.getClass();
        Field[] fields = getAllFields(cls);
        Map<String, Object> map = new HashMap<>();
        for (Field field : fields) {
            if (Modifier.isFinal(field.getModifiers())
                    || Modifier.isStatic(field.getModifiers())
                    || Modifier.isTransient(field.getModifiers())){
                continue;
            }
            String key = field.getName();
            boolean flag = field.isAccessible();
            field.setAccessible(true);
            Object val = null;
            try {
                val = field.get(obj);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            field.setAccessible(flag);
            if (val != null){
                map.put(key, val);
            }
        }
        return map;
    }
 
    /**
     * 获取指定Class(及其SuperClass)的成员变量
     */
    public static Field[] getAllFields(Class<?> cls){
        return getAllFields(cls, null);
    }
 
    /**
     * 递归合并基类Field
     */
    private static Field[] getAllFields(Class<?> cls, Field[] params){
        Field[] fields = (params == null) ? cls.getDeclaredFields() : params;
        Class<?> superCls = cls.getSuperclass();
        if (superCls == null || superCls == Object.class){
            return fields;
        }
        Field[] superClsFields = superCls.getDeclaredFields();
        fields = addAll(fields, superClsFields);
        return getAllFields(superCls, fields);
    }
 
    /**
     * 根据fieldName获取Field对象
     */
    public static Field getField(Class<?> cls, String fieldName) {
        Field[] allFields = getAllFields(cls);
        for (Field field : allFields) {
            if (field.getName().equals(fieldName)) {
                return field;
            }
        }
        return null;
    }
 
    /**
     * 获取对象中某个Field的值
     */
    public static Object getFieldValue(Object obj, Field field) {
        if (null == field) {
            return null;
        } else {
            if (obj instanceof Class) {
                obj = null;
            }
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
            try {
                return field.get(obj);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }
 
    /**
     * 数组叠加
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] addAll(T[] array1, T... array2) {
        if (array1 == null) {
            return clone(array2);
        } else if (array2 == null) {
            return clone(array1);
        } else {
            Class<?> cls = array1.getClass().getComponentType();
            T[] joinedArray = (T[]) Array.newInstance(cls, array1.length + array2.length);
            System.arraycopy(array1, 0, joinedArray, 0, array1.length);
 
            try {
                System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
                return joinedArray;
            } catch (ArrayStoreException e) {
                Class<?> type2 = array2.getClass().getComponentType();
                if (!cls.isAssignableFrom(type2)) {
                    throw new RuntimeException("Cannot store " + type2.getName() + " in an array of " + cls.getName(), e);
                } else {
                    throw e;
                }
            }
        }
    }
 
    /**
     * 克隆
     */
    private static <T> T[] clone(T[] array) {
        return array == null ? null : array.clone();
    }
 
    /**
     * map操作
     */
    public static CoolMap add(String key,Object value){
        CoolMap map = new CoolMap();
        map.put(key, value);
        return map;
    }
 
    public static class CoolMap extends HashMap<String, Object>{
 
        public CoolMap add(String key,Object value){
            this.put(key, value);
            return this;
        }
 
        public CoolMap $(String key,Object value){
            this.put(key, value);
            return this;
        }
 
    }
 
    public static String md5(String string){
        try{
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes(StandardCharsets.UTF_8));
            char[] chars = new char[bytes.length * 2];
            for (int i = 0; i < bytes.length; i++) {
                int b = bytes[i];
                chars[i * 2] = hexDigits[(b & 0xF0) >> 4];
                chars[i * 2 + 1] = hexDigits[b & 0x0F];
            }
            return new String(chars).toLowerCase();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("md5加密失败,str=".concat(string));
        }
    }
 
    private static char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 
    //public static Map<String, Object> translate(Object obj){
    //    Class<?> cls = obj.getClass();
    //    Field[] fields = getAllFields(cls);
    //    Map<String, Object> map = new HashMap<>();
    //    for (Field field : fields) {
    //        String key = field.getName();
    //        if (field.isAnnotationPresent(CoolTranslate.class)){
    //            CoolTranslate annotation = field.getAnnotation(CoolTranslate.class);
    //            if (!isEmpty(annotation.value())) {
    //                key = annotation.value();
    //            }
    //        }
    //        boolean flag = field.isAccessible();
    //        field.setAccessible(true);
    //        Object val = null;
    //        try {
    //            val = field.get(obj);
    //        } catch (IllegalAccessException e) {
    //            e.printStackTrace();
    //        }
    //        field.setAccessible(flag);
    //        if (val != null){
    //            map.put(key, val);
    //        }
    //    }
    //    return map;
    //}
 
    public static boolean eq(String str, String str0) {
        if (Cools.isEmpty(str) && Cools.isEmpty(str0)) {
            return true;
        }
        if (Cools.isEmpty(str) && !Cools.isEmpty(str0)) {
            return false;
        }
        if (Cools.isEmpty(str0) && !Cools.isEmpty(str)) {
            return false;
        }
        if (str.equals(str0)) {
            return true;
        }
        return false;
    }
 
}