Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.util.Map;

public class Main {
    /**
     * Appends the given set of parameters to the given query string
     * @param   queryBuilder   The query url string to append the parameters
     * @param   parameters   The parameters to append
     * @return   The modified query url string */
    public static void appendUrlWithQueryParameters(StringBuilder queryBuilder, Map<String, Object> parameters) {
        //perform parameter validation
        if (null == queryBuilder)
            throw new IllegalArgumentException("Given value for parameter \"queryBuilder\" is invalid.");

        if (null == parameters)
            return;

        //does the query string already has parameters
        boolean hasParams = (queryBuilder.indexOf("?") > 0);

        //iterate and append parameters
        for (Map.Entry<String, Object> pair : parameters.entrySet()) {
            //ignore null values
            if (null == pair.getValue())
                continue;
            //if already has parameters, use the &amp; to append new parameters
            char separator = (hasParams) ? '&' : '?';

            queryBuilder.append(separator + pair.getKey() + "=" + pair.getValue());

            //indicate that now the query has some params
            hasParams = true;
        }
    }
}