JAVA类型转换以及值复制工具类
有时候在开发过程中可能需要转换这个对象那个值的,可能有时候一些开发者会直接写在代码中,这样的作法是看着代码会比较臃肿,复用性不高,不太美观。有一些会将其独立出来写成一个工具类,这样的好处是,复用性高,可读性强。此工具类就是对各种值进行互相转换的工具类以及对相同属性值的对象进行值复制操作的工具类。如果有错误的地方欢迎大家指正。
package com.jx.utils; import java.lang.reflect.Field; import java.math.BigDecimal; import java.util.*; /** * @Author jingxuan * @Date 2020/9/24 * @Description 类型转换以及值复制 */ public class CaseUtils { /** * 将Object转换成字符串 * * @param param param * @param replace replace value * @return */ public static String caseString(Object param, String replace) { return param == null ? replace : String.valueOf(param); } /** * 将Object转换成字符串 * * @param param param * @return */ public static String caseString(Object param) { return caseString(param, ""); } /** * 将数组char转换成字符串 * * @param param param * @param replace replace value * @return */ public static String caseString(char[] param, String replace) { return param == null ? replace : String.valueOf(param); } /** * 将数组char转换成字符串 * * @param param param * @return */ public static String caseString(char[] param) { return caseString(param, ""); } /** * 将字符串转换成Integer如果参数为空则为默认参数 * * @param param * @param replace * @return */ public static Integer caseInteger(String param, Integer replace) { return param == null || param.length() == 0 ? replace : Integer.valueOf(param); } /** * 将字符串转换成Integer类型,默认为NUll * * @param param * @return */ public static Integer caseInteger(String param) { return caseInteger(param, null); } /** * 将long转换成Integer类型 * * @param param * @return */ public static Integer caseInteger(Long param) { return param == null ? null : param.intValue(); } /** * 将字符串转换成BigDecimal * * @param param * @param replace * @return */ public static BigDecimal caseBigDecimal(String param, Integer replace) { if ((param == null || param.length() == 0)) { if (replace == null) { return null; } return new BigDecimal(replace); } return new BigDecimal(param); } /** * 将字符串转换成BigDecimal * * @param param * @return */ public static BigDecimal caseBigDecimal(String param) { return caseBigDecimal(param, null); } /** * 将double转换成BigDecimal类型 * * @param param * @return */ public static BigDecimal caseBigDecimal(Double param) { return caseBigDecimal(param, null); } /** * 将double转换成BigDecimal类型 * * @param param * @param replace * @return */ public static BigDecimal caseBigDecimal(Double param, Double replace) { if (param == null) { if (replace == null) { return null; } return new BigDecimal(replace); } return new BigDecimal(param); } /** * 将Integer转成BigDecimal * * @param param * @param replace * @return */ public static BigDecimal caseBigDecimal(Integer param, Integer replace) { if (param == null) { if (replace == null) { return null; } return new BigDecimal(replace); } return new BigDecimal(param); } /** * 将Integer转成BigDecimal * * @param param * @return */ public static BigDecimal caseBigDecimal(Integer param) { return caseBigDecimal(param, null); } /** * 将Float转成BigDecimal * * @param param * @param replace * @return */ public static BigDecimal caseBigDecimal(Float param, Float replace) { if (param == null) { if (replace == null) { return null; } return new BigDecimal(replace); } return new BigDecimal(param); } /** * 将Float转成BigDecimal * * @param param * @return */ public static BigDecimal caseBigDecimal(Float param) { return caseBigDecimal(param, null); } /** * 将Object转换成map * * @param param 转换参数 * @param ignoreNull 是否忽略NUll值 * @return */ public static Map<String, Object> caseMap(Object param, boolean ignoreNull) { if (param == null) { return null; } Field[] fields = param.getClass().getDeclaredFields(); Map<String, Object> map = new HashMap<>(fields.length); try { for (Field field : fields) { field.setAccessible(true); Object value = field.get(param); if (ignoreNull && value == null) { continue; } map.put(field.getName(), field.get(param)); } return map; } catch (IllegalAccessException e) { throw new IllegalArgumentException("can not resolve param :", e); } } /** * 将Object转换成Map * * @param param 转换参数 * @return map */ public static Map<String, Object> caseMap(Object param) { return caseMap(param, false); } /** * 复制属性到指定的目标类 * * @param source * @param target * @param <F> * @param <T> * @return */ public static <F, T> T BeanCopy(F source, Class<T> target) { if (source == null || target == null) { return null; } try { T instance = target.newInstance(); //复制操作 BeanCopyAttributes(getFieldMap(source.getClass()), getFieldMap(target), source, instance); return instance; } catch (Exception e) { throw new IllegalArgumentException("can not caseClass param :", e); } } /** * 复制属性到指定的目标对象中 * * @param source * @param target */ public static void BeanCopy(Object source, Object target) { if (source == null || target == null) { return; } try { //复制操作 BeanCopyAttributes(getFieldMap(source.getClass()), getFieldMap(target.getClass()), source, target); } catch (Exception e) { throw new IllegalArgumentException("can not caseClass param :", e); } } public static <F, T> List<T> BeanCopy(List<F> list, Class<T> target) { if (list == null || target == null || list.isEmpty()) { return null; } try { Map<String, Field> targetFieldMap = getFieldMap(target); Map<String, Field> sourceFieldMap = getFieldMap(list.get(0).getClass()); List<T> result = new ArrayList<>(list.size()); for (F source : list) { T instance = target.newInstance(); //复制操作 BeanCopyAttributes(sourceFieldMap, targetFieldMap, source, instance); result.add(instance); } return result; } catch (Exception e) { throw new IllegalArgumentException("can not caseClass param :", e); } } /** * 复制属性值 * * @param sourceFieldMap * @param targetFieldMap * @param source * @param target */ private static void BeanCopyAttributes(Map<String, Field> sourceFieldMap, Map<String, Field> targetFieldMap, Object source, Object target) { if (sourceFieldMap == null || targetFieldMap == null || sourceFieldMap.isEmpty()) { return; } try { for (Map.Entry<String, Field> entry : sourceFieldMap.entrySet()) { if (targetFieldMap.containsKey(entry.getKey())) { Field field = entry.getValue(); Field targetClassField = targetFieldMap.get(entry.getKey()); if (field.getGenericType() == targetClassField.getGenericType()) { field.setAccessible(true); targetClassField.setAccessible(true); //设置值 targetClassField.set(target, field.get(source)); } } } } catch (Exception e) { throw new IllegalArgumentException("can not copy param error :", e); } } /** * 解析类的字段 * * @param var * @return */ private static Map<String, Field> getFieldMap(Class var) { //class类对象的map Map<String, Field> fieldMap = new HashMap<>(); //获取所有的属性 while (var != null) { Field[] declaredFields = var.getDeclaredFields(); for (Field field : declaredFields) { if(!fieldMap.containsKey(field.getName())){ fieldMap.put(field.getName(), field); } } //得到父类,然后赋给自己直到顶节点为空 var = var.getSuperclass(); } return fieldMap; } }