All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.logicommerce.utilities.builders.QueryParamsBuilder Maven / Gradle / Ivy

package com.logicommerce.utilities.builders;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.stream.Collectors;

public class QueryParamsBuilder {

	private static final String ENCODE = "UTF-8";

	private static final String PARAM_MASK = "%s=%s";

	private static final String JOIN_CHAR_PARAMS = "&";

	private Map queryParams;

	public QueryParamsBuilder() {
		queryParams = new LinkedHashMap<>();
	}

	public QueryParamsBuilder add(String name, Object value) {
		if (name != null && value != null) {
			queryParams.put(name, value.toString());
		}
		return this;
	}

	private String setParam(Entry entry) {
		try {
			String encodedValue = URLEncoder.encode(entry.getValue(), ENCODE);
			return String.format(PARAM_MASK, entry.getKey(), encodedValue);
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}

	@Override
	public String toString() {
		return queryParams.entrySet().stream()
				.map(this::setParam)
				.filter(Objects::nonNull)
				.collect(Collectors.joining(JOIN_CHAR_PARAMS));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy