Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Iterator;

import java.util.Map;
import java.util.Set;

public class Main {
    private final static String STRING = "java.lang.String";
    private final static String DATE = "java.util.Date";
    private final static String INTEGER = "java.lang.Integer";
    private final static String LONG = "java.lang.Long";
    private final static String ARRAY = "ARRAY";
    private final static String BOOLEAN = "java.lang.Boolean";
    private final static int BUFFER_SIZE = 100;

    public static String getStringByMap(Map<String, Object> map) {
        StringBuffer sb = new StringBuffer(BUFFER_SIZE);
        Set<String> keys = map.keySet();
        Iterator<String> iter = keys.iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            Object value = map.get(key);
            if (value == null) {
                continue;
            }

            String clsName = value.getClass().getName();
            if (value.getClass().isArray()) {
                sb.append("[").append(key).append(":").append(ARRAY).append(":")
                        .append(toStringByArray((Object[]) value)).append("]");
            } else if (STRING.equals(clsName) || INTEGER.equals(clsName) || LONG.equals(clsName)
                    || BOOLEAN.equals(clsName) || DATE.equals(clsName)) {
                if ("".equals(value.toString())) {
                    continue;
                }
                sb.append("[").append(key).append(":").append(value.getClass().getName()).append(":")
                        .append(esc(value.toString())).append("]");
            }
        }
        return sb.toString();
    }

    private static String toStringByArray(Object[] value) {
        StringBuffer sb = new StringBuffer(100);
        String type = value[0].getClass().getName();
        sb.append("{").append(type).append("@");
        for (int i = 0; i < value.length; i++) {
            sb.append(esc(value[i].toString())).append(",");
        }
        return sb.substring(0, sb.length() - 1) + "}";
    }

    private static String esc(String str) {
        return str.replaceAll(":", "&&&").replaceAll("\\[", "&!&").replaceAll("\\]", "!&!").replaceAll(",", "!!!")
                .replaceAll("\\{", "!!&").replaceAll("\\}", "&&!").replaceAll("@", "###");
    }
}