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

net.joningi.icndb.QueryString Maven / Gradle / Ivy

Go to download

Java API client for The Internet Chuck Norris Database (http://www.icndb.com/)

The newest version!
package net.joningi.icndb;

import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;

import com.google.common.collect.Maps;

public class QueryString {

    private final Map parameters;

    public QueryString() {
        this.parameters = Maps.newHashMap();
    }

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

    private String build() {
        final StringBuilder builder = new StringBuilder();

        if (!this.parameters.isEmpty()) {
            builder.append("?");
        }

        final Iterator> iterator = this.parameters.entrySet().iterator();
        while (iterator.hasNext()) {
            final Map.Entry entry = iterator.next();

            builder.append(URLEncoder.encode(entry.getKey()))
                    .append('=')
                    .append(URLEncoder.encode(entry.getValue()));

            if (iterator.hasNext()) {
                builder.append('&');
            }
        }
        return builder.toString();
    }

    public String toString() {
        return build();
    }

    public void addAll(final Map parameters) {
        this.parameters.putAll(parameters);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy