java通过key-list构造对象

java通过key-list构造对象

给定一组key组成的list,和它对应的value,凭空构造出一个对象

Object buildObject(final List<String> paths, final Object object) {
        if (null == paths) {
            throw DataMException.asDataMException(
                    "Path不能为null,该异常代表系统编程错误, 请联系DataX开发团队 !");
        }

        if (1 == paths.size() && StringUtils.isBlank(paths.get(0))) {
            return object;
        }

        Object child = object;
        for (int i = paths.size() - 1; i >= 0; i--) {
            String path = paths.get(i);

            if (isPathMap(path)) {
                Map<String, Object> mapping = new HashMap<String, Object>();
                mapping.put(path, child);
                child = mapping;
                continue;
            }

            if (isPathList(path)) {
                List<Object> lists = new ArrayList<Object>(
                        this.getIndex(path) + 1);
                expand(lists, this.getIndex(path) + 1);
                lists.set(this.getIndex(path), child);
                child = lists;
                continue;
            }

            throw DataMException.asDataMException("路径[%s]出现非法值类型[%s],该异常代表系统编程错误, 请联系DataX开发团队! .");
        }

        return child;
    }
原文地址:https://www.cnblogs.com/wangbin2188/p/14188495.html