泛型类,泛型方法的使用
传递入对象类型,返回出对象类型都是泛型
接口
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);
}
}