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
|
public class Converts {
private static <T> void formatVoField(Object source, int keepDecimal, String unit, String replaceStr, T data, Field field) { if (Objects.equals("serialVersionUID", field.getName())) { return; } Object oldTemp = null; try { oldTemp = ReflectionUtil.invokeGetterMethod(source, field.getName()); } catch (Exception e) { e.printStackTrace(); }
switch (field.getType().getSimpleName()) { case "Float": if (ObjectUtil.isNotNull(oldTemp)) { if (keepDecimal == -1) { oldTemp = new BigDecimal((float) oldTemp); } else { oldTemp = new BigDecimal((float) oldTemp) .setScale(keepDecimal, BigDecimal.ROUND_HALF_UP); } oldTemp = String.valueOf(oldTemp).concat(unit); } else { oldTemp = replaceStr; } break; case "Integer": if (ObjectUtil.isNotNull(oldTemp)) { oldTemp = String.valueOf(oldTemp).concat(unit); } else { oldTemp = replaceStr; } break; case "BigDecimal": if (ObjectUtil.isNotNull(oldTemp)) { if (keepDecimal == -1) { oldTemp = new BigDecimal(oldTemp.toString()); } else { oldTemp = new BigDecimal(oldTemp.toString()) .setScale(keepDecimal, BigDecimal.ROUND_HALF_UP); } oldTemp = String.valueOf(oldTemp).concat(unit); } else { oldTemp = replaceStr; } break; default: } if (ObjectUtil.isNotNull(oldTemp)) { ReflectionUtil.invokeSetterMethod(data, field.getName(), oldTemp); } }
public static <T> T toRespVo(Object source, final Class<T> voClass) { T data = null; try { data = voClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { e.printStackTrace(); } Map<String, Field> voFieldsMap = Arrays.stream(voClass.getDeclaredFields()) .collect(Collectors.toMap(Field::getName, Function.identity())); for (Field field : source.getClass().getDeclaredFields()) { RespVoProperty property = Optional.ofNullable(voFieldsMap.get(field.getName())) .map(annotationClass -> annotationClass.getAnnotation(RespVoProperty.class)) .orElse(null); if (Objects.isNull(property)) { formatVoField(source, -1, "", "", data, field); } else { formatVoField(source, property.keepDecimal(), property.unit(), property.replaceStr(), data, field); } } return data; }
public static <T> List<T> toRespVos(List<? extends Object> sources, final Class<T> voClass) { List<T> targets = new ArrayList<>(); if (CollectionUtils.isEmpty(sources)) { return targets; } sources.parallelStream().forEach(source -> targets.add(toRespVo(source, voClass))); return targets; }
public static void main(String[] args) { List<Person> list = new ArrayList<>(); Person data1 = new Person(); data1.setName("李三"); data1.setAge(14); data1.setMoney(BigDecimal.ONE); data1.setWeight(15.2f);
System.out.println("-----------------单对象转化vo----------------");
System.out.println(toRespVo(data1, PersonVo.class));
System.out.println("--------------------------------------------");
System.out.println("-----------------集合对象转化vo----------------");
list.add(data1);
Person data2 = new Person(); data2.setName("wangwu"); data2.setAge(45); data2.setMoney(BigDecimal.valueOf(6.41f)); data2.setWeight(null); list.add(data2);
Person data3 = new Person(); data3.setName("张思"); data3.setAge(null); data3.setMoney(BigDecimal.valueOf(25f)); data3.setWeight(17.689f); list.add(data3);
System.out.println(toRespVos(list, PersonVo.class)); } }
|