/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for
 * license information.
 */

package com.microsoft.rest.http;

/**
 * A builder class that is used to create URLs.
 */
public class UrlBuilder {
    private String scheme;
    private String host;
    private String path;
    private String query;

    /**
     * Set the scheme/protocol that will be used to build the final URL.
     * @param scheme The scheme/protocol that will be used to build the final URL.
     * @return This UrlBuilder so that multiple setters can be chained together.
     */
    public UrlBuilder withScheme(String scheme) {
        this.scheme = scheme;
        return this;
    }

    /**
     * Set the host that will be used to build the final URL.
     * @param host The host that will be used to build the final URL.
     * @return This UrlBuilder so that multiple setters can be chained together.
     */
    public UrlBuilder withHost(String host) {
        if (host != null && host.endsWith("/")) {
            host = host.substring(0, host.length() - 1);
        }
        this.host = host;
        return this;
    }

    /**
     * Set the path that will be used to build the final URL.
     * @param path The path that will be used to build the final URL.
     * @return This UrlBuilder so that multiple setters can be chained together.
     */
    public UrlBuilder withPath(String path) {
        this.path = path;
        return this;
    }

    /**
     * Add the provided query parameter name and encoded value to query string for the final URL.
     * @param queryParameterName The name of the query parameter.
     * @param queryParameterEncodedValue The encoded value of the query parameter.
     * @return The provided query parameter name and encoded value to query string for the final
     * URL.
     */
    public UrlBuilder withQueryParameter(String queryParameterName, String queryParameterEncodedValue) {
        if (query == null) {
            query = "?";
        }
        else {
            query += "&";
        }
        query += queryParameterName + "=" + queryParameterEncodedValue;
        return this;
    }

    /**
     * Get the string representation of the URL that is being built.
     * @return The string representation of the URL that is being built.
     */
    public String toString() {
        final StringBuilder result = new StringBuilder();

        if (scheme != null) {
            result.append(scheme);

            if (!scheme.endsWith("://")) {
                result.append("://");
            }
        }

        if (host != null) {
            result.append(host);
        }

        if (path != null) {
            if (result.length() != 0 && !path.startsWith("/")) {
                result.append('/');
            }
            result.append(path);
        }

        if (query != null) {
            result.append(query);
        }

        return result.toString();
    }
}

@@ -1476,25 +1476,618 @@
     
-this.path = path;
+if (path != null) %7B%0A            String%5B%5D parts = path.split(%22%5C%5C?%22);%0A            this.path = parts%5B0%5D;%0A            if (parts.length %3E 1) %7B%0A                String%5B%5D queryPairs = parts%5B1%5D.split(%22&%22);%0A                for (String queryPair : queryPairs) %7B%0A                    String%5B%5D nameAndValue = queryPair.split(%22=%22);%0A                    if (nameAndValue.length != 2) %7B%0A                        throw new IllegalArgumentException(%22Path contained malformed query: %22 + path);%0A                    %7D%0A%0A                    withQueryParameter(nameAndValue%5B0%5D, nameAndValue%5B1%5D);%0A                %7D%0A            %7D%0A        %7D
 %0A   

Support path containing a query in UrlBuilder