package com.bigcommerce.api;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.bigcommerce.http.QueryStringBuilder;

/**
 * Builds a query to filter the results of a collection request.
 * 
 * @author Jenith
 * 
 */
public class Filter {

	private Map<String, String> parameters;

	private Filter(Map<String, String> parameters) {
		this.parameters = parameters;
	}

	public static Filter create() {
		return create(null);
	}

	/**
	* Factory method, creates an instance of a filter.
	* Used to build URLs to collection endpoints.
	*/
	public static Filter create(Map<String, String> parameters) {
		return (parameters == null) ? new Filter(new HashMap<String, String>())
				: new Filter(parameters);
	}

	public void add(String parameter, String value) {
		this.parameters.put(parameter, value);
	}

	/**
	 * Converts the filter into a URL querystring that can be applied as GET
	 * parameters.
	 * 
	 * @return string
	 */
	public String toQuery() {
		String query = null;
		try {
			query = httpBuildQuery(this.parameters);
		} catch (UnsupportedEncodingException e) {
			// TODO Log
			e.printStackTrace();
		}
		return (query != null) ? query : "";
	}

	private String httpBuildQuery(Map<String, String> data)
			throws UnsupportedEncodingException {
		QueryStringBuilder builder = new QueryStringBuilder();
		for (Entry<String, String> pair : data.entrySet()) {
			builder.addQueryParameter(pair.getKey(), pair.getValue());
		}
		return builder.encode("UTF-8");
	}

}

@@ -84,119 +84,103 @@
 ava.
-util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Map.Entry;%0A%0Aimport com.bigcommerce.http.QueryStringBuilder
+net.URLEncoder;%0Aimport java.nio.charset.Charset;%0Aimport java.util.HashMap;%0Aimport java.util.Map
 ;%0A%0A/
@@ -268,16 +268,31 @@
 r Jenith
+ Michael Raj. Y
 %0A * %0A */
@@ -509,16 +509,17 @@
 %0A%0A%09/**%0A%09
+ 
 * Factor
@@ -560,19 +560,16 @@
  filter.
-%0A%09*
  Used to
@@ -582,16 +582,20 @@
  URLs to
+%0A%09 *
  collect
@@ -610,16 +610,17 @@
 oints.%0A%09
+ 
 */%0A%09publ
@@ -1059,17 +1059,8 @@
 ll;%0A
-%09%09try %7B%0A%09
 %09%09qu
@@ -1104,242 +1104,241 @@
 ;%0A%09%09
-%7D catch (UnsupportedEncodingException e) %7B%0A%09%09%09// TODO Log%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09return (query != null) ? query : %22%22;%0A%09%7D%0A%0A%09private String httpBuildQuery(Map%3CString, String%3E data)%0A%09%09%09throws UnsupportedEncodingException %7B%0A%09%09Query
+return (query != null) ? query : %22%22;%0A%09%7D%0A%0A%09/**%0A%09 * Build a query string.%0A%09 * %0A%09 * @param params%0A%09 * @return%0A%09 */%0A%09protected String httpBuildQuery(Map%3CString, String%3E params) %7B%0A%09%09if (params == null %7C%7C params.isEmpty())%0A%09%09%09return %22%22;%0A%09%09
 Stri
@@ -1351,27 +1351,17 @@
 der 
+s
 b
-uilder
  = new 
-Query
 Stri
@@ -1374,18 +1374,38 @@
 der(
+%22?%22
 );%0A
+%09%09int i = 0;%0A
 %09%09for (
+Map.
 Entr
@@ -1426,19 +1426,22 @@
 ng%3E 
-pair : data
+entry : params
 .ent
@@ -1458,102 +1458,353 @@
 %0A%09%09%09
-builder.addQueryParameter(pair.getKey(), pair.getValue()
+String key = entry.getKey();%0A%09%09%09String value = entry.getValue();%0A%09%09%09if (i %3E 0) %7B%0A%09%09%09%09sb.append(%22&%22
 );%0A
+%09
 %09%09%7D%0A%09%09
-return builder.encode(%22UTF-8%22
+%09try %7B%0A%09%09%09%09sb.append(key)%0A%09%09%09%09%09%09.append(%22=%22)%0A%09%09%09%09%09%09.append(URLEncoder.encode(value, Charset%0A%09%09%09%09%09%09%09%09.defaultCharset().name()));%0A%09%09%09%7D catch (UnsupportedEncodingException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%09%09%09i++;%0A%09%09%7D%0A%09%09return sb.toString(
 );%0A%09

Update Filter.java