import java.util.Map;
/**
* format a string like:${name} is a good ${gender}, with values which is a Map.
* @author wanglu
* @date 2014-7-22
* @email wanglu.zero@gmail.com
* @version 1.0
*/
public class StringFormat {
private String template;
private String prefix;
private String suffix;
private final static String PLACEHOLDER_PREFIX = "\\$\\{";
private final static String PLACEHOLDER_SUFFIX = "\\}";
private final static String PLACEHOLDER_PATTERN = ".*"+PLACEHOLDER_PREFIX+".*"+PLACEHOLDER_SUFFIX+".*";
/**
* equals StringFormat(template, "", "", true)
* @param template
*/
public StringFormat(String template){
this(template, "", "", true);
}
/**
* equals StringFormat(template,"","", isCheckPlaceHolder)
* @param template
*/
public StringFormat(String template, boolean isCheckPlaceHolder){
this(template,"","", isCheckPlaceHolder);
}
/**
* equals StringFormat(template, prefix, "", true)
* @param template
*/
public StringFormat(String template, String prefix){
this(template, prefix, "", true);
}
/**
* equals StringFormat(template, prefix, suffix, true)
* @param template
*/
public StringFormat(String template, String prefix, String suffix){
this(template, prefix, suffix, true);
}
/**
* equals StringFormat(template, prefix, "", isCheckPlaceHolder)
* @param template
*/
public StringFormat(String template, String prefix, boolean isCheckPlaceHolder){
this(template, prefix, "", isCheckPlaceHolder);
}
/**
* @param template 字符串模板
* @param prefix 前缀
* @param suffix 后缀
* @param isCheckPlaceHolder, when is true, template at least has one placeholder,otherwise will throw a exception
*/
public StringFormat(String template, String prefix, String suffix, boolean isCheckPlaceHolder){
if(template == null || template.trim().equals("")){
throw new IllegalArgumentException("template can't be null or empty or blank");
}
if( isCheckPlaceHolder && !hasPlaceHolder(template) ){
throw new IllegalArgumentException("template has not any placeholder");
}
this.template = template;
this.prefix = (prefix == null ? "" : prefix);
this.suffix = (suffix == null ? "" : suffix);
}
/**
* 核心方法
* @param values Map类型的值
* @return 替换好的字符串
* @throws Exception
*/
public String format(Map<?,?> values) throws Exception{
return format(values, true);
}
/**
*
* @param values
* @param isCheckReplaceAll, after replace,check template has any placehoder
* @return String
* @throws Exception
*/
public String format(Map<?,?> values, boolean isCheckReplaceAll) throws Exception{
if( values == null ){
throw new NullPointerException("values can't be null");
}
String template = this.template;
for (Object key : values.keySet() ) {
String value = this.prefix + values.get(key).toString() + this.suffix;
template = template.replaceAll( getPlaceHolder(key.toString()) , value);
}
if( isCheckReplaceAll && hasPlaceHolder(template) ){
throw new Exception("does not replace all placeholder");
}
return template;
}
public String getTemplate(){
return this.template;
}
private static String getPlaceHolder(String key){
if( key == null ){
throw new NullPointerException("key can't be null");
}
return PLACEHOLDER_PREFIX + key + PLACEHOLDER_SUFFIX;
}
private static boolean hasPlaceHolder(String value){
if( value == null ){
throw new NullPointerException("value can't be null");
}
return value.matches( PLACEHOLDER_PATTERN );
}
}
转载于:https://my.oschina.net/zero2hero/blog/296585