Java

Java library for performing different image operations.

package gumlet;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class GumletURLBuilder {
    private String scheme;
    private String host;
    private int port = -1;
    private String path;
    private Map<String, String> queryParams;

    public GumletURLBuilder() {
        this.queryParams = new HashMap<>();
    }

    public GumletURLBuilder setScheme(String scheme) {
        this.scheme = scheme;
        return this;
    }

    public GumletURLBuilder setHost(String host) {
        this.host = host;
        return this;
    }

    public GumletURLBuilder setPort(int port) {
        this.port = port;
        return this;
    }

    public GumletURLBuilder setPath(String path) {
        this.path = path;
        return this;
    }

    public GumletURLBuilder addQueryParameter(String name, String value) {
        this.queryParams.put(name, value);
        return this;
    }

    private String buildQuery() throws UnsupportedEncodingException {
        StringBuilder query = new StringBuilder();
        for (Map.Entry<String, String> entry : queryParams.entrySet()) {
            if (query.length() > 0) {
                query.append("&");
            }
            query.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8.toString()));
            query.append("=");
            query.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString()));
        }
        return query.toString();
    }

    public URI build() throws URISyntaxException, UnsupportedEncodingException {
        StringBuilder uri = new StringBuilder();
        if (scheme != null) {
            uri.append(scheme).append("://");
        }
        if (host != null) {
            uri.append(host);
        }
        if (port != -1) {
            uri.append(":").append(port);
        }
        if (path != null) {
            if (!path.startsWith("/")) {
                uri.append("/");
            }
            uri.append(path);
        }
        String queryString = buildQuery();
        if (!queryString.isEmpty()) {
            uri.append("?").append(queryString);
        }
        return new URI(uri.toString());
    }

}



import gumlet.GumletURLBuilder;

public class Demo {

  public static void main(String[] args) {
        try {
            GumletURLBuilder builder = new GumletURLBuilder();
            URI uri = builder.setScheme("https")
                             .setHost("demo.gumlet.io")
                             .setPath("/flower.jpeg")
                             .addQueryParameter("width", "250")
                             .addQueryParameter("dpr", "2.0")
                             .addQueryParameter("overlay", "https://demo.gumlet.io/logo.png")
                             .build();

            System.out.println(uri.toString()); //output - https://demo.gumlet.io/flower.jpeg?overlay=https%3A%2F%2Fdemo.gumlet.io%2Flogo.png&dpr=2.0&width=250
        } catch (URISyntaxException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
  
}