处理List「JavaBean」数据中的bigDecimal保留3位小数

其中towTuple是元组```

private <T> void handleBeanListBigDecimalScale(List<T> list, Class<T> clazz) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		if (list != null && list.size() > 0 && clazz != null) {
			Map<String, TwoTuple<Method, Method>> methodMap = new HashMap<String, TwoTuple<Method, Method>>();

			Method[] methods = clazz.getMethods();
			if (methods != null && methods.length > 0) {
				for (Method method : methods) {
					if (method.getReturnType().equals(BigDecimal.class)) {
						String methodName = method.getName();
						if (methodName.contains("get")) {
							String middleName = methodName.replaceFirst("get", "");
							TwoTuple<Method, Method> twoTuple = new TwoTuple<Method, Method>();
							twoTuple.setFirst(method);
							methodMap.put(middleName, twoTuple);
						}
					}
				}

				for (Method method : methods) {
					String methodName = method.getName();
					if (!methodName.contains("get")) {
						if (methodName.contains("set")) {
							String middleName = methodName.replaceFirst("set", "");
							TwoTuple<Method, Method> methodTwoTuple = methodMap.get(middleName);
							if (methodTwoTuple != null) {
								methodTwoTuple.setSecond(method);
								continue;
							}
							methodMap.remove(middleName);
						}
					}
				}
			}

			Iterator<T> it = list.iterator();
			while (it.hasNext()) {
				T item = it.next();
				methodMap.values();
				for (TwoTuple<Method, Method> methodTwoTuple : methodMap.values()) {
					Method getMethod = methodTwoTuple.getFirst();
					BigDecimal value = (BigDecimal) getMethod.invoke(item);
					if (value != null) {
						Method setMethod = methodTwoTuple.getSecond();
						setMethod.invoke(item, value.setScale(3, BigDecimal.ROUND_HALF_UP));
					}
				}
			}
		}
	}

你可能感兴趣的:(java)