月球大数据地理空间分析展示平台-【后端】-月球后台服务
1
13693261870
2024-11-13 eb40365c9cffb2269fd3cbd31b050c33455bc84a
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
package com.moon.server.helper;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
import java.lang.reflect.Method;
 
@SuppressWarnings("ALL")
public class EnumHelper {
    private static final String GETTER_PREFIX = "get";
 
    private static Log log = LogFactory.getLog(EnumHelper.class);
 
    public static <T extends Enum<T>> T nameOf(Class<T> clazz, String name) {
        return Enum.valueOf(clazz, name);
    }
 
    public static <T extends Enum<T>> T getByString(Class<T> clazz, String propertyName, String value) {
        String getterMethodName = GETTER_PREFIX + StringHelper.firstCharToUpperCase(propertyName);
        T result = null;
        try {
            T[] arr = clazz.getEnumConstants();
            Method targetMethod = clazz.getDeclaredMethod(getterMethodName);
            String typeCodeVal = null;
            for (T entity : arr) {
                typeCodeVal = targetMethod.invoke(entity).toString();
                if (typeCodeVal.contentEquals(value)) {
                    result = entity;
                    break;
                }
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
 
        return result;
    }
 
    public static <T extends Enum<T>> T getByInt(Class<T> clazz, String propertyName, int value) {
        String getterMethodName = GETTER_PREFIX + StringHelper.firstCharToUpperCase(propertyName);
        T result = null;
        try {
            T[] arr = clazz.getEnumConstants();
            Method targetMethod = clazz.getDeclaredMethod(getterMethodName);
            int typeCodeVal;
            for (T entity : arr) {
                typeCodeVal = Integer.parseInt(targetMethod.invoke(entity).toString());
                if (typeCodeVal == value) {
                    result = entity;
                    break;
                }
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        return result;
    }
}