思路
用泛型将相同类型中的通用方法提取出来,如多个枚举类中的getEnumByKey、getValueByKey等方法,ResultUtil中的返回方法。
枚举案例
enumInterface
public interface EFEnum<K,V> {
K getKey();
V getValue();
}
enumEnum
@Getter
@AllArgsConstructor
public enum OrderEnum implements EFEnum<String,String>{
DEALING("0", "处理中"),
SUCCESS("1", "成功"),
FAIL("2", "失败");
private String code;
private String message;
@Override
public String getKey() {
return this.code;
}
@Override
public String getValue() {
return this.message;
}
}
enumUtil
public class EnumUtils {
public static <E extends EFEnum<K, V>, K, V> V getValueByKey(Class<E> clazz, K key) {
EFEnum<K, V> e = getEnumByKey(clazz, key);
return e == null ? null : e.getValue();
}
public static <E extends EFEnum<K, V>, K, V> K getKeyByValue(Class<E> clazz, V value) {
EFEnum<K, V> e = getEnumByValue(clazz, value);
return e == null ? null : e.getKey();
}
public static <E extends EFEnum<K, V>, K, V> EFEnum<K, V> getEnumByKey(Class<E> clazz, K key) {
EFEnum<K, V>[] enums = clazz.getEnumConstants();
return Arrays.stream(enums)
.filter(e -> Objects.equals(key, e.getKey()))
.findFirst().orElse(null);
}
public static <E extends EFEnum<K, V>, K, V> EFEnum<K, V> getEnumByValue(Class<E> clazz, V value) {
EFEnum<K, V>[] enums = clazz.getEnumConstants();
return Arrays.stream(enums)
.filter(e -> Objects.equals(value, e.getValue()))
.findFirst().orElse(null);
}
}
ResultUtil案例
ResultUtil
@NoArgsConstructor
public class ResultUtil {
public static <E extends CommonResp> E result(Class<E> clazz, E e, boolean b) {
E result = null;
try {
result = clazz.newInstance();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
}
if (b) {
BeanUtils.copyProperties(e, result);
result.setRspCd(CommonRespCMCCEnum.SUCCESS.getCode());
result.setRspInf(CommonRespCMCCEnum.SUCCESS.getMessage());
} else {
result.setRspCd(CommonRespCMCCEnum.SYSTEM_ERROR.getCode());
result.setRspInf(CommonRespCMCCEnum.SYSTEM_ERROR.getMessage());
}
System.out.println(result.getClass().toString());
return result;
}
public static <E extends CommonResp> E success(Class<E> clazz, E e) {
E result = null;
try {
result = clazz.newInstance();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
}
result.setRspCd(CommonRespCMCCEnum.SUCCESS.getCode());
result.setRspInf(CommonRespCMCCEnum.SUCCESS.getMessage());
BeanUtils.copyProperties(e, result);
return result;
}
public static <E extends CommonResp> E fail(Class<E> clazz, E e) {
E result = null;
try {
result = clazz.newInstance();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
}
result.setRspCd(CommonRespCMCCEnum.SYSTEM_ERROR.getCode());
result.setRspInf(CommonRespCMCCEnum.SYSTEM_ERROR.getMessage());
return result;
}
public static void main(String[] args) {
// RepayPlanResp resp = fun1(RepayPlanResp.class, true);
// RepayPlanReq a = fun1(RepayPlanReq.class, true);
// RepayApplyResp b = fun1(RepayApplyResp.class, true);
RepayPlanDetail detail = RepayPlanDetail.builder().orgOrdNo("123").orgPlanNo("abc").lateFee(BigDecimal.valueOf(141)).build();
List<RepayPlanDetail> list = new ArrayList<>();
list.add(detail);
RepayApplyResp re1 = RepayApplyResp.builder().planList(list).build();
re1 = result(RepayApplyResp.class, re1, false);
System.out.println(re1.toString());
}
}
调用
return ResultUtil.fail(RepayPlanResp.class, null);
return ResultUtil.success(RepayPlanResp.class, result);