package core.framework.internal.template.model;

import core.framework.util.Encodings;

import static core.framework.util.Strings.format;

/**
 * @author neo
 */
public class URIBuilder {
    private final StringBuilder uri;
    private boolean queryStarted;

    public URIBuilder(String prefix) {
        uri = new StringBuilder(prefix);
        queryStarted = prefix.indexOf('?') > 0;
    }

    public URIBuilder addPath(String segment) {
        if (queryStarted) throw new Error(format("path segment must not be added after query, uri={}", uri.toString()));
        if (uri.length() > 0 && uri.charAt(uri.length() - 1) != '/') uri.append('/');
        uri.append(Encodings.uriComponent(segment));
        return this;
    }

    public URIBuilder addQueryParam(String name, String value) {
        uri.append(queryStarted ? '&' : '?').append(Encodings.uriComponent(name)).append('=').append(Encodings.uriComponent(value));
        queryStarted = true;
        return this;
    }

    public String toURI() {
        return uri.toString();
    }
}

@@ -85,59 +85,8 @@
 s;%0A%0A
-import static core.framework.util.Strings.format;%0A%0A
 /**%0A
@@ -431,15 +431,8 @@
 ror(
-format(
 %22pat
@@ -480,12 +480,11 @@
 uri=
-%7B%7D%22,
+%22 +
  uri
@@ -495,17 +495,16 @@
 tring())
-)
 ;%0A      

remove unnecessary strings.format