泛型类,泛型方法的使用

泛型类,泛型方法的使用

传递入对象类型,返回出对象类型都是泛型

接口

public interface CloudParamGetter<IN, OUT> {

    OUT get(IN in);
}

实现

public class VmParamGetter implements CloudParamGetter<FamaVm, InstanceSpec> {

    @Override
    public InstanceSpec get(FamaVm vm) {
        InstanceSpec spec = new InstanceSpec();
        spec.setAz(vm.getAz());
        spec.setName(vm.getName());
        spec.setInstanceType(vm.getInstanceType());
        JsonArray json = vm.getTagJson();
        List<Tag> tagSpecs = Lists.newArrayList();
        for (JsonElement element : json) {
            JsonObject obj = element.getAsJsonObject();
            for (String s : obj.keySet()) {
                String v = obj.get(s).getAsString();
                Tag tag = new Tag();
                tag.setKey(s);
                tag.setValue(v);
                tagSpecs.add(tag);
            }
        }
        spec.setUserTags(tagSpecs);
        InstanceDiskAttachmentSpec systemDisk = new InstanceDiskAttachmentSpec();
        int diskSize = vm.getDiskSize();
        if (diskSize <= 50) {
            systemDisk.setDiskCategory("local");
        } else {
            systemDisk.setDiskCategory("cloud");
        }
        systemDisk.setAutoDelete(true);
        DiskSpec diskSpec = new DiskSpec();
        diskSpec.setAz(vm.getAz());
        diskSpec.setDiskType(vm.getDiskType());
        diskSpec.setName(buildDiskName(vm.getAppEnName()));
        diskSpec.setDiskSizeGB(vm.getDiskSize());
        systemDisk.setCloudDiskSpec(diskSpec);
        spec.setSystemDisk(systemDisk);
        spec.setImageId(vm.getImageId());
        InstanceNetworkInterfaceAttachmentSpec primaryNetworkInterface = new InstanceNetworkInterfaceAttachmentSpec();
        NetworkInterfaceSpec networkInterface = new NetworkInterfaceSpec();
        networkInterface.setSubnetId(vm.getSubnetId());
        primaryNetworkInterface.setNetworkInterface(networkInterface);
        spec.setPrimaryNetworkInterface(primaryNetworkInterface);
        if (vm.getEnvType() == EnvType.TEST) {
            spec.setPassword("Focus2!_+");
        }
        return spec;
    }

    public String buildDiskName(String appEnName){
        String diskName = "DISK-" + appEnName.replace(".","_");
        if (diskName.length() <= 32){
            return diskName;
        }
        return diskName.substring(0,32);
    }
}

泛型方法用于解析http返回值

    protected <T> T parseData(ResponseEntity<String> response, Class<T> dataType) {
        Map<String, Object> responseData = JSON.parseObject(response.getBody(), new TypeReference<Map<String, Object>>() {
        });
        T t = JSON.parseObject(responseData.get(DATA).toString(), dataType);
        return t;
    }

    protected <T> List<T> parseArray(ResponseEntity<String> response, Class<T> dataType){
        Map<String, Object> responseData = JSON.parseObject(response.getBody(), new TypeReference<Map<String, Object>>() {
        });
        List<T> list = JSON.parseArray(responseData.get(DATA).toString(), dataType);
        return list;
    }
posted @ 2021-06-01 15:34  SpecialSpeculator  阅读(57)  评论(0编辑  收藏  举报