Java Message Format format(String pattern, Map params)

Here you can find the source of format(String pattern, Map params)

Description

format

License

MIT License

Declaration

public static String format(String pattern, Map<String, Object> params) 

Method Source Code


//package com.java2s;
/*!//from  w  ww . j a v  a  2s .  c o m
 * mifmi-commons4j
 * https://github.com/mifmi/mifmi-commons4j
 *
 * Copyright (c) 2015 mifmi.org and other contributors
 * Released under the MIT license
 * https://opensource.org/licenses/MIT
 */

import java.text.MessageFormat;

import java.util.Map;

public class Main {
    public static String format(String pattern, Object... params) {
        if (pattern == null) {
            return null;
        }
        if (params == null || params.length == 0) {
            return pattern;
        }
        return MessageFormat.format(pattern, params);
    }

    public static String format(String pattern, Map<String, Object> params) {
        return format(pattern, params, "{", "}");
    }

    public static String format(String pattern, Map<String, Object> params, String varPrefix, String varSuffix) {
        int len = pattern.length();

        StringBuilder sb = new StringBuilder(len * 2);
        for (int i = 0; i < len; i++) {
            int beginIdx = pattern.indexOf(varPrefix, i);
            if (beginIdx == -1) {
                sb.append(pattern, i, len);
                break;
            }
            sb.append(pattern, i, beginIdx);
            int varBeginIdx = beginIdx + varPrefix.length();

            int endIdx = pattern.indexOf(varSuffix, varBeginIdx);
            if (endIdx == -1) {
                sb.append(pattern, beginIdx, len);
                break;
            }
            int varEndIdx = endIdx + varSuffix.length();

            String varName = pattern.substring(varBeginIdx, varEndIdx);
            Object val = params.get(varName);
            if (val != null) {
                sb.append(val);
            }
        }

        return sb.toString();
    }

    public static int indexOf(CharSequence charSeq, char ch) {
        return indexOf(charSeq, ch, 0);
    }

    public static int indexOf(CharSequence charSeq, char ch, int fromIndex) {
        if (charSeq == null) {
            return -1;
        }
        int len = charSeq.length();
        for (int i = fromIndex; i < len; i++) {
            char c = charSeq.charAt(i);
            if (c == ch) {
                return i;
            }
        }
        return -1;
    }

    public static int indexOf(CharSequence charSeq, char[] ch) {
        return indexOf(charSeq, ch, 0);
    }

    public static int indexOf(CharSequence charSeq, char[] ch, int fromIndex) {
        if (charSeq == null) {
            return -1;
        }
        int len = charSeq.length();
        for (int i = fromIndex; i < len; i++) {
            char c = charSeq.charAt(i);
            for (int j = 0; j < ch.length; j++) {
                if (c == ch[j]) {
                    return i;
                }
            }
        }
        return -1;
    }

    public static String toString(StringBuilder sb, boolean trim) {
        if (sb == null) {
            return null;
        }

        if (!trim) {
            return sb.toString();
        }

        int len = sb.length();
        if (len == 0) {
            return "";
        }

        int sIdx;
        for (sIdx = 0; sIdx < len; sIdx++) {
            char ch = sb.charAt(sIdx);
            if (!Character.isWhitespace(ch)) {
                break;
            }
        }

        if (sIdx == len) {
            return "";
        }

        int eIdx;
        for (eIdx = len - 1; 0 <= eIdx; eIdx--) {
            char ch = sb.charAt(eIdx);
            if (!Character.isWhitespace(ch)) {
                break;
            }
        }

        return sb.substring(sIdx, eIdx + 1);
    }
}

Related

  1. format(String key, Object... values)
  2. format(String message, Object argument)
  3. format(String message, Object object)
  4. format(String message, Object parameter)
  5. format(String message, Object... params)
  6. format(String pattern, Object... args)
  7. format(String pattern, String[] replacements)
  8. format(String str, int replacement)
  9. format(String str, Object... arguments)