自己编写的Java StringFormat工具类

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值