package net.yoojia.asynchttp.support;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

/**
 * @author : 桥下一粒砂
 * @email  : chenyoca@gmail.com
 * @date   : 2012-10-23
 * @desc   : A http connection parameters wrapper
 */
public class ParamsWrapper {

	public final ArrayList<NameValue> nameValueArray = new ArrayList<NameValue>();
	public final ArrayList<PathParam> pathParamArray = new ArrayList<PathParam>();
	
	public static class NameValue{
		public final String name;
		public final String value;
		public NameValue(String name,Object value){
			this.name = name;
			this.value = String.valueOf(value);
		}
	}
	
	public static class PathParam{
		public final NameValue param;
		public final String path;
		public PathParam(String name, Object value, String path){
			this.param = new NameValue(name,value);
			this.path = path;
		}
	}
	
	public ParamsWrapper put(String name,String value){
		appendToParamsArray(name,value);
		return this;
	}
	
	public ParamsWrapper put(String name,int value){
		appendToParamsArray(name,value);
		return this;
	}

	public ParamsWrapper put(String name,boolean value){
		appendToParamsArray(name,value);
		return this;
	}

	public ParamsWrapper put(String name,float value){
		appendToParamsArray(name,value);
		return this;
	}

	public ParamsWrapper put(String name,long value){
		appendToParamsArray(name,value);
		return this;
	}

	public ParamsWrapper put(String name,double value){
		appendToParamsArray(name,value);
		return this;
	}
	
	public ParamsWrapper put(String name,String fileName,String path){
		pathParamArray.add(new PathParam(name, fileName, path));
		return this;
	}
	
	private ParamsWrapper appendToParamsArray(String name,Object value){
		if(name != null && value != null 
				&& !"".equals(name) && !"".equals(value) ){
			nameValueArray.add(new NameValue(name, value));
		}
		return this;
	}
	
	@Override
	public String toString(){
		try {
			return getStringParams();
		} catch (UnsupportedEncodingException exp) {
			exp.printStackTrace();
			return "Params contains unsupport encoding content";
		}
	}
	
	/**
	 * 取出其参数组
	 * @return 参数字符串 i.e:"name=chenyoca&age=24&email=chenyoca@gmail.com"
	 * @throws UnsupportedEncodingException 
	 */
	public String getStringParams() throws UnsupportedEncodingException{
		return getStringParams("utf-8");
	}
	
	/**
	 * 取出字符型参数组，并指定URL参数编码类型
	 * @param urlEncoding 编码类型
	 * @return null或者参数字符串 i.e:"name=chenyoca&age=24&email=chenyoca@gmail.com"
	 * @throws UnsupportedEncodingException
	 */
	public String getStringParams(String urlEncoding) throws UnsupportedEncodingException{
		StringBuffer buffer = new StringBuffer();
		final int size = nameValueArray.size();
		for(int i=0;i<size;i++){
			NameValue param = nameValueArray.get(i);
			buffer.append(param.name).append("=")
				.append(URLEncoder.encode(param.value,urlEncoding))
				.append("&");
		}
		if(buffer.length()>0) buffer.deleteCharAt(buffer.length()-1);
		return buffer.length() > 0 ? buffer.toString() : null;
	}
	
}

@@ -1667,24 +1667,282 @@
 urn this;%0A%09%7D
+%0A%0A%09public boolean containsValue(String name)%7B%0A%09%09boolean contains = false;%0A%09%09if( name == null ) return contains;%0A%09%09for(NameValue item : nameValueArray)%7B%0A%09%09%09if( name.equalsIgnoreCase( item.name ))%7B%0A%09%09%09%09contains = true;%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return contains;%0A%09%7D
 %0A%09%0A%09private 
@@ -2896,18 +2896,19 @@
 StringBu
-ff
+ild
 er buffe
@@ -2927,88 +2927,23 @@
 ngBu
-ffer();%0A%09%09final int size = nameValueArray.size();%0A%09%09for(int i=0;i%3Csize;i++)%7B%0A%09%09%09
+ilder();%0A%09%09for(
 Name
@@ -2954,17 +2954,17 @@
 e param 
-=
+:
  nameVal
@@ -2974,16 +2974,10 @@
 rray
-.get(i);
+)%7B
 %0A%09%09%09

添加用AsyncHttpConnection模块，并修正新版本代码