java中实现实例复制

1、工具类如下:

package com.merit.performancemonitoring.util;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;

@Slf4j
public class BeanCopyUtil {
    /**
     * 相同对象合并,将原对象的非空属性的值赋值给目标对象
     *
     * @param origin      源对象
     * @param destination 目标对象
     * @param <T>         对象的类型
     */
    public static <T> void merge(T origin, T destination) {
        if (origin == null || destination == null) {
            return;
        }
        if (!origin.getClass().equals(destination.getClass())) {
            return;
        }
        Field[] fields = origin.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                Object value = field.get(origin);
                if (null != value) {
                    field.set(destination, value);
                }
            } catch (IllegalAccessException e) {
                log.error("访问对象异常", e);
            }
            field.setAccessible(false);
        }
    }
}

2、具体实例:

private List<RdsPerformanceEntity> makeupData(DescribeDBInstancePerformanceResponse response) {
        Map<String, String> maps = rdsConfig.getDbInstanceIpMaps();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        List<RdsPerformanceEntity> rdsList = new ArrayList<>();
        RdsPerformanceEntity rds = RdsPerformanceEntity.builder()
                .requestId(response.getRequestId())
                .dBInstanceId(response.getDBInstanceId())
                .engine(response.getEngine())
                .startTime(response.getStartTime())
                .endTime(response.getEndTime())
                .build();
        for (DescribeDBInstancePerformanceResponse.PerformanceKey performanceKey : response.getPerformanceKeys()) {
            RdsPerformanceEntity rdsKey = RdsPerformanceEntity.builder().build();
            BeanCopyUtil.merge(rds, rdsKey);
            rdsKey.setKey(performanceKey.getKey());
            rdsKey.setUnit(performanceKey.getUnit());
            rdsKey.setValueFormat(performanceKey.getValueFormat());
            for (DescribeDBInstancePerformanceResponse.PerformanceKey.PerformanceValue pValue : performanceKey.getValues()) {
                RdsPerformanceEntity rdsValue = RdsPerformanceEntity.builder().build();
                BeanCopyUtil.merge(rdsKey, rdsValue);
                rdsValue.setValue(pValue.getValue());
                rdsValue.setDate(pValue.getDate());
                Date date = new Date();
                rdsValue.setCreateTime(df.format(date));
                String hostIp = maps.get(rdsValue.getDBInstanceId());
                if (hostIp == null) {
                    hostIp = "null";
                }
                rdsValue.setHostIp(hostIp);
                rdsValue.setId(sdf.format(date) + hostIp);
                rdsList.add(rdsValue);
            }
        }
        return rdsList;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值