Java Generic: Generic dynamic acknowledgment method using Return Type

Returns dynamic objects according to the generic type

public <T extends PackObject> T  unPackMessage(String interfaceCode, String respValue, Class<T>  clazz, String sysType) throws IOException {
    log.info(">> unPackMessage start, interfaceCode is {}, respValue is {}", interfaceCode, respValue);
    Map<String, Object> result = null;
    // get pack_convert.prop file content
    PackConvertMessage packConvertMessage = getPackConvertFileContent();
    String func = packConvertMessage.getInterfaceMapping().get(interfaceCode);
    if (StringUtils.isBlank(func)){
        log.error("<< unPackMessage error, No response message with current interface configured");
        return null;
    }
    String respSerialNumber = packConvertMessage.getRespFuncMapping().get(func);
    String currPackType = PackConvertConstant.SystermType.CLIENT.equals(sysType) ? client_pack_type : pack_type;
    switch (currPackType){
        case PackConvertConstant.PackType.NON_FIXED_LENGTH:
            result = unpackNoFixedMessage(respValue, connect_char, respSerialNumber, PACK_CONVERT_FILE_MAPPING_SEPARATOR, sysType);
            break;
        case  PackConvertConstant.PackType.FIXED_LENGTH:
            result = unpackFixedMessage(respValue, respSerialNumber, PACK_CONVERT_FILE_MAPPING_SEPARATOR);
            break;
        case  PackConvertConstant.PackType.XML:
            result = unpackXmlMessage(respValue, respSerialNumber, PACK_CONVERT_FILE_MAPPING_SEPARATOR);
            break;
        case PackConvertConstant.PackType.JSON:
            result = unpackJsonMessage(respValue, respSerialNumber);
            break;
    }
    log.info("<< unPackMessage success, return value is {}", JSON.toJSONString(result));
    T t = JSON.toJavaObject(JSON.parseObject(JSON.toJSONString(result)), clazz);
    return t;
}

The dynamic returns a collection of generic type

 public <E extends BaseLiquidation, T extends BaseLiquidation> List<E> convertFileToObj(String localFilePath, Class<E> clazz, Class<T> tclazz) throws IOException, NoSuchMethodException, IllegalAccessException, InstantiationException {
     logger.info(">> convertFileToObj, localFilePath is {}, clazz is {}", localFilePath, clazz.getName());
     BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(localFilePath),"utf-8"));
     StringBuilder sb = new StringBuilder();
     String line;
     while ((line = br.readLine()) != null){
         sb.append(line);
         sb.append(System.getProperty("line.separator"));
     }
     List<E> result = messageToObj(sb.toString(), clazz);
     logger.info("<< convertFileToObj end");
     return result;
}

 

public <E extends BaseLiquidation> List<E> convertFileToObj(String localFilePath, Class<E> clazz) throws IOException, NoSuchMethodException, IllegalAccessException, InstantiationException {
    logger.info(">> convertFileToObj, localFilePath is {}, clazz is {}", localFilePath, clazz.getName());
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(localFilePath),"utf-8"));
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = br.readLine()) != null){
        sb.append(line);
        sb.append(System.getProperty("line.separator"));
    }
    List<E> result = messageToObj(sb.toString(), clazz);
    logger.info("<< convertFileToObj end");
    return result;
}

Basic introduction generic method

  • public is very important to return the value of the intermediate <T>, it indicates that this method is a generic method declarations
  • Only declared <T> method is a generic method used in the generic class of members of a generic method can not be regarded as a generic method
  • <T> indicates that the method will use the generic type T, it can only be used at this time in the process of the generic type T
  • As with the definition of generic class, T can be easily written in arbitrary label, such as T, E, K, V, S, etc.

Generic restrictions upper and lower bounds

  • Generic restrictions on border: Incoming type can only be a subclass of the specified class, such as Class <extend PackObject?>
  • Generic restrictions at the border: incoming type can only be the parent of the specified class, such as DiagnosticListener <super JavaFileObject?>

Also described is attached common generic identification

  • Some common generic identity is T (type), E (element), K (key), V (value) ,? (Wildcard uncertain) these, in fact, to fill any letter will do

 

Guess you like

Origin www.cnblogs.com/xhy-shine/p/11200769.html