常用方法总结

//判断字符串是否为空

org.springframework.util.StringUtils.hasLength(str)

//判断集合框架list等是否为空

org.springframework.util.CollectionUtils.isEmpty(collection)

//把list转化为逗号隔开的字符串

    public static final String DEFAULT_LIST_DELIMTER = ",";

    public static String getStringByList(List<? extends Object> list) {

    return getStringByList(list, DEFAULT_LIST_DELIMTER);

    }

    

    public static String getStringByList(List<? extends Object> list, String delimter) {

    StringBuffer sb = new StringBuffer();

    if (! CollectionUtils.isEmpty(list)) {

    for (int i = 0 ; i < list.size(); i++) {

    sb.append(list.get(i).toString() + (i == (list.size() - 1) ? "" : delimter));

    }

    }

    return sb.toString();

    }

//String类型转化为Int型通用方法

    public static Integer convertStringToInt(String str) {

        return convertStringToInt(str, 0);

    }

    public static Integer convertStringToInt(String str, Integer defaultValue) {

        if (! org.springframework.util.StringUtils.hasLength(str)) {

            return defaultValue;

        }

        try {

            return Integer.parseInt(str.trim());

        } catch (Exception ex) {

            log.error("exception in convertStringToInt orignal value=" + str);

            return defaultValue;

        }

    }

//logger定义

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

private static final Logger logger = LoggerFactory.getLogger(xxxx.class);

//常量定义

public interface familyConstants {

String ERROR_RESULT_TEMPALTE = "{\"result\":\"0\",\"resultText\":\"%s\"}";

String SUCCESS_RESULT_TEMPALTE = "{\"result\":\"1\",\"resultText\":\"%s\"}";

String OPERATE_SUCCESS = "operate.success";

String OPERATE_ERROR = "operate.error";

String PARAMETER_NOT_FOUND = "parameter.not.found";

}

//PropertiesContainer 

定义

public  class  PropertiesContainer {

public static Properties properties=null;

public static String get(String key) {

    if(properties!=null){

        String value=properties.getProperty(key);

        if(StringUtils.isNotBlank(value)) {

            return value;

        } else{

                log.error("未定义key为["+key+"]的properties");

                return null;

        }

    } else{

         return null;

    }

}

}

使用:PropertiesContainer.get(key);

//String.format

定义方法

    public static String format(String format, Object... args) {

        return new Formatter().format(format, args).toString();

    }

 

使用

String.format(format, args);

String.format(constants.ERROR_RESULT_TEMPALTE,

                    PropertiesContainer.get(constants.PARAMETER_NOT_FOUND))

猜你喜欢

转载自zgx945.iteye.com/blog/2287595