Code example for ConcurrentHashMap

Methods: put

0
     * @param key the key name for the new param. 
     * @param value the value string for the new param. 
     */ 
    public void put(String key, String value){
        if(key != null && value != null) {
            urlParams.put(key, value);
        } 
    } 
 
    /** 
     * Adds a file to the request. 
     * @param key the key name for the new param. 
     * @param file the file to add. 
     */ 
    public void put(String key, File file) throws FileNotFoundException {
        put(key, new FileInputStream(file), file.getName());
    } 
 
    /** 
     * Adds param with more than one value. 
     * @param key the key name for the new param. 
     * @param values is the ArrayList with values for the param. 
     */ 
    public void put(String key, ArrayList<String> values)  {
        if(key != null && values != null) {
            urlParamsWithArray.put(key, values);
        } 
    } 
 
    /** 
     * Adds value to param which can have more than one value. 
     * @param key the key name for the param, either existing or new. 
     * @param value the value string for the new param. 
     */ 
    public void add(String key, String value) {
        if(key != null && value != null) {
            ArrayList<String> paramArray = urlParamsWithArray.get(key);
            if (paramArray == null) {
                paramArray = new ArrayList<String>();
                this.put(key, paramArray);
            } 
            paramArray.add(value);
        } 
    } 
 
    /** 
     * Adds an input stream to the request. 
     * @param key the key name for the new param. 
     * @param stream the input stream to add. 
     */ 
    public void put(String key, InputStream stream) {
        put(key, stream, null);
    } 
 
    /** 
     * Adds an input stream to the request. 
     * @param key the key name for the new param. 
     * @param stream the input stream to add. 
     * @param fileName the name of the file. 
     */ 
    public void put(String key, InputStream stream, String fileName) {
        put(key, stream, fileName, null);
    } 
 
    /** 
     * Adds an input stream to the request. 
     * @param key the key name for the new param. 
     * @param stream the input stream to add. 
     * @param fileName the name of the file. 
     * @param contentType the content type of the file, eg. application/json 
     */ 
    public void put(String key, InputStream stream, String fileName, String contentType) {
        if(key != null && stream != null) {
            fileParams.put(key, new FileWrapper(stream, fileName, contentType));
        } 
    } 
 
    /** 
     * Removes a parameter from the request. 
     * @param key the key name for the parameter to remove. 
     */ 
    public void remove(String key){
        urlParams.remove(key);
        fileParams.remove(key);
        urlParamsWithArray.remove(key);
    } 
 
    @Override 
    public String toString() {
        StringBuilder result = new StringBuilder();
        for(ConcurrentHashMap.Entry<String, String> entry : urlParams.entrySet()) {
            if(result.length() > 0)
                result.append("&");
 
            result.append(entry.getKey());
            result.append("=");
            result.append(entry.getValue());
        } 
 
        for(ConcurrentHashMap.Entry<String, FileWrapper> entry : fileParams.entrySet()) {
            if(result.length() > 0)
                result.append("&");
 
            result.append(entry.getKey());
            result.append("=");
            result.append("FILE");
        } 
 
        for(ConcurrentHashMap.Entry<String, ArrayList<String>> entry : urlParamsWithArray.entrySet()) {
            if(result.length() > 0)
                result.append("&");
 
            ArrayList<String> values = entry.getValue();
            for (int i = 0; i < values.size(); i++) {
                if (i != 0)
                    result.append("&");
                result.append(entry.getKey());
                result.append("=");
                result.append(values.get(i));
            } 
        } 
 
        return result.toString();
    } 
 
   /** 
     * Returns an HttpEntity containing all request parameters 
     */ 
    public HttpEntity getEntity() {
        HttpEntity entity = null;
 
        if(!fileParams.isEmpty()) {
            SimpleMultipartEntity multipartEntity = new SimpleMultipartEntity();
 
            // Add string params 
            for(ConcurrentHashMap.Entry<String, String> entry : urlParams.entrySet()) {
                multipartEntity.addPart(entry.getKey(), entry.getValue());
            } 
 
            // Add dupe params 
            for(ConcurrentHashMap.Entry<String, ArrayList<String>> entry : urlParamsWithArray.entrySet()) {
                ArrayList<String> values = entry.getValue();
                for (String value : values) {
                    multipartEntity.addPart(entry.getKey(), value);
                } 
            } 
 
            // Add file params 
            int currentIndex = 0;
            int lastIndex = fileParams.entrySet().size() - 1;
            for(ConcurrentHashMap.Entry<String, FileWrapper> entry : fileParams.entrySet()) {
                FileWrapper file = entry.getValue();
                if(file.inputStream != null) {
                    boolean isLast = currentIndex == lastIndex;
                    if(file.contentType != null) {
                        multipartEntity.addPart(entry.getKey(), file.getFileName(), file.inputStream, file.contentType, isLast);
                    } else { 
                        multipartEntity.addPart(entry.getKey(), file.getFileName(), file.inputStream, isLast);
                    } 
                } 
                currentIndex++;
            } 
 
            entity = multipartEntity;
        } else { 
            try {