Java Map to String toString(Map attrs)

Here you can find the source of toString(Map attrs)

Description

to String

License

Open Source License

Declaration

public static final String toString(Map<String, String> attrs) 

Method Source Code

//package com.java2s;

import java.util.Map;

public class Main {
    private static final String SP = ";";
    private static final String SSP = ":";
    private static final String R_SP = "#3A";
    private static final String R_SSP = "#3B";

    public static final String toString(Map<String, String> attrs) {
        StringBuilder sb = new StringBuilder();
        if (null != attrs && !attrs.isEmpty()) {
            sb.append(SP);/*from w  w w.j  a  v a  2  s .  c  om*/
            for (String key : attrs.keySet()) {
                String val = attrs.get(key);
                if (isNotEmpty(val)) {
                    sb.append(encode(key)).append(SSP).append(encode(val)).append(SP);
                }
            }
        }
        return sb.toString();
    }

    public static boolean isNotEmpty(String str) {
        return ((str != null) && (str.length() > 0));
    }

    private static String encode(String val) {
        return replace(replace(val, SP, R_SP), SSP, R_SSP);
    }

    public static String replace(String text, String repl, String with) {
        return replace(text, repl, with, -1);
    }

    public static String replace(String text, String repl, String with, int max) {
        if ((text == null) || (repl == null) || (with == null) || (repl.length() == 0) || (max == 0)) {
            return text;
        }

        StringBuffer buf = new StringBuffer(text.length());
        int start = 0;
        int end = 0;

        while ((end = text.indexOf(repl, start)) != -1) {
            buf.append(text.substring(start, end)).append(with);
            start = end + repl.length();

            if (--max == 0) {
                break;
            }
        }

        buf.append(text.substring(start));
        return buf.toString();
    }
}

Related

  1. toString(Map map)
  2. toString(Map m)
  3. toString(Map map)
  4. toString(Map map)
  5. toString(Map attrs)
  6. toString(Map m)
  7. toString(Map map)
  8. toString(Map params)
  9. toString(Map map)