class MessageParameters {

	public static String resolveUrl(String genericUrl, MessageParameters parameters) {
		Preconditions.checkState(parameters.isResolved(), "Not all mandatory message parameters were resolved.");
		StringBuilder path = new StringBuilder(genericUrl);
		StringBuilder queryParameters = new StringBuilder();

		for (MessageParameter<?> pathParameter : parameters.getPathParameters()) {
			if (pathParameter.isResolved()) {
				int start = path.indexOf(':' + pathParameter.getKey());

				final String pathValue = Preconditions.checkNotNull(pathParameter.getValueAsString());

				// only replace path parameters if they are present
				if (start != -1) {
					path.replace(start, start + pathParameter.getKey().length() + 1, pathValue);
				}
			}
		}
		boolean isFirstQueryParameter = true;
		for (MessageQueryParameter<?> queryParameter : parameters.getQueryParameters()) {
			if (queryParameter.isResolved()) {
				if (isFirstQueryParameter) {
					queryParameters.append('?');
					isFirstQueryParameter = false;
				} else {
					queryParameters.append('&');
				}
				queryParameters.append(queryParameter.getKey());
				queryParameters.append('=');
				queryParameters.append(queryParameter.getValueAsString());
			}
		}
		path.append(queryParameters);

		return path.toString();
	}

}