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
| import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashSet; import java.util.List; import java.util.Set;
public class BaseEnumValidator {
public static boolean isInEnum(EnumValid annotation, String value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Object[] objects = getEnumConstants(annotation); Method method = getEnumMethod(annotation); for (Object o : objects) { if (value.equals(method.invoke(o))) { return true; } } return false; }
public static boolean isInEnum(EnumValid annotation, Integer value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Object[] objects = getEnumConstants(annotation); Method method = getEnumMethod(annotation); for (Object o : objects) { if (value.equals(method.invoke(o))) { return true; } } return false; }
public static boolean isInEnumByListString(EnumValid annotation, List<String> valueList) throws Exception { Object[] objects = getEnumConstants(annotation); Method method = getEnumMethod(annotation);
Set<String> set = new HashSet<>(); for (Object o : objects) { set.add((String) method.invoke(o)); }
for (String value : valueList) { if (!set.contains(value)) { return false; } } return true; }
public static boolean isInEnumByListInteger(EnumValid annotation, List<Integer> valueList) throws Exception { Object[] objects = getEnumConstants(annotation); Method method = getEnumMethod(annotation);
Set<Integer> set = new HashSet<>(); for (Object o : objects) { set.add((Integer) method.invoke(o)); }
for (Integer value : valueList) { if (!set.contains(value)) { return false; } } return true; }
private static Object[] getEnumConstants(EnumValid annotation) { return annotation.enumClass().getEnumConstants(); }
private static Method getEnumMethod(EnumValid annotation) throws NoSuchMethodException { return annotation.enumClass().getMethod(annotation.method()); } }
|