Java Map to String mapToString(String aName, Map aMap, String aPrefix, String aSuffix, String aKeyValueSeparator, String anEntrySeparator, String anArrayPrefix, String anArraySuffix, String anArraySeparator, String anEscapeChars, char anEscapeSymbol)

Here you can find the source of mapToString(String aName, Map aMap, String aPrefix, String aSuffix, String aKeyValueSeparator, String anEntrySeparator, String anArrayPrefix, String anArraySuffix, String anArraySeparator, String anEscapeChars, char anEscapeSymbol)

Description

map To String

License

Open Source License

Parameter

Parameter Description
aName The name of the map. If `null`, no name is prefixed.
aMap The map to convert. If `null` or empty, only the name, prefix and suffix are concatenated.
aPrefix The prefix to insert before listing keys and values. Omitted entirely if `null`.
aSuffix The suffix to append to the end of the string. Omitted entirely if `null`.
aKeyValueSeparator The character sequence to separate keys from values in the result. Defaults to a single space (' ') if `null`.
anEntrySeparator The character sequence to separate key-value pairs from other entries in the result. Defaults to a single space (' ') if `null`.
anArrayPrefix The character sequence that defines the beginning of an array value. Omitted entirely if `null`.
anArraySuffix The character sequence that defines the end of an array value. Omitted entirely if `null`.
anArraySeparator The character sequence to separate elements in an array value. Defaults to a single space (' ') if `null`.
anEscapeChars A string containing the characters that need to be escaped in the name, key and value strings. Note that characters in `aKeyValueSeparator` and `entrySeparator` are not escaped. No escaping is done if `null` or empty.
anEscapeSymbol The symbol to be inserted before characters that need to be escaped. May be used to escape itself.

Return

A string representation of the input map. This value is never `null`, although it may be empty if the map, name, prefix and suffix are all empty or `null`.

Declaration

public static String mapToString(String aName, Map<String, Object> aMap, String aPrefix, String aSuffix,
        String aKeyValueSeparator, String anEntrySeparator, String anArrayPrefix, String anArraySuffix,
        String anArraySeparator, String anEscapeChars, char anEscapeSymbol) 

Method Source Code

//package com.java2s;
/*/*from   w  w w  . j av  a 2  s  . c o  m*/
 * TextUtilities.java (Class: com.madphysicist.tools.util.TextUtilities)
 *
 * Mad Physicist JTools Project (General Purpose Utilities)
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2012 by Joseph Fox-Rabinovitz
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.util.Map;

public class Main {
    /**
     * @brief Converts a map into a string.
     *
     * The map may contain strings and arrays. All other value types (including
     * non-string array elements) will be converted to strings using their
     * `toString()` method. This method does not currently support primitive or
     * nested arrays.
     *
     * The return value has the following format:
     *
     *      <name><prefix><escaped key><keyValueSeparator><escaped value><entrySeparator> ... <suffix>
     *
     * The entry separator does not appear after the last key-value element.
     * Array values have the following format:
     *
     *      <arrayPrefix><escaped element><arraySeparator> ... <arraySuffix>
     *
     * The array separator does not appear after the last element.
     *
     * The name, keys and values will have all characters that appear in
     * `anEscapeChars` escaped by preceding them with the escape symbol. The
     * prefix, suffix, key-value separator and entry separator will not be
     * escaped. The recommended use of this method is to add the symbols in the
     * prefix, suffix, array delimiters and separators to the escape characters
     * so that they can be identified unambiguously.
     *
     * @param aName The name of the map. If `null`, no name is prefixed.
     * @param aMap The map to convert. If `null` or empty, only the name, prefix
     * and suffix are concatenated.
     * @param aPrefix The prefix to insert before listing keys and values.
     * Omitted entirely if `null`.
     * @param aSuffix The suffix to append to the end of the string. Omitted
     * entirely if `null`.
     * @param aKeyValueSeparator The character sequence to separate keys from
     * values in the result. Defaults to a single space (' ') if `null`.
     * @param anEntrySeparator The character sequence to separate key-value
     * pairs from other entries in the result. Defaults to a single space (' ')
     * if `null`.
     * @param anArrayPrefix The character sequence that defines the beginning of
     * an array value. Omitted entirely if `null`.
     * @param anArraySuffix The character sequence that defines the end of an
     * array value. Omitted entirely if `null`.
     * @param anArraySeparator The character sequence to separate elements in an
     * array value. Defaults to a single space (' ') if `null`.
     * @param anEscapeChars A string containing the characters that need to be
     * escaped in the name, key and value strings. Note that characters in
     * `aKeyValueSeparator` and `entrySeparator` are not escaped. No escaping is
     * done if `null` or empty.
     * @param anEscapeSymbol The symbol to be inserted before characters that
     * need to be escaped. May be used to escape itself.
     * @return A string representation of the input map. This value is never
     * `null`, although it may be empty if the map, name, prefix and suffix are
     * all empty or `null`.
     * @see #arrayToString()
     * @see #propertiesToString()
     * @see #stringToMap()
     * @since 1.0.0
     */
    public static String mapToString(String aName, Map<String, Object> aMap, String aPrefix, String aSuffix,
            String aKeyValueSeparator, String anEntrySeparator, String anArrayPrefix, String anArraySuffix,
            String anArraySeparator, String anEscapeChars, char anEscapeSymbol) {
        StringBuilder sb = new StringBuilder();
        if (aName != null) {
            sb.append(escapeString(aName, anEscapeChars, anEscapeSymbol));
        }
        if (aPrefix != null) {
            sb.append(aPrefix);
        }
        if (aMap != null && !aMap.isEmpty()) {
            if (aKeyValueSeparator == null)
                aKeyValueSeparator = " ";
            if (anEntrySeparator == null)
                anEntrySeparator = " ";

            // indicates that the loop is starting
            boolean first = true;
            for (Map.Entry<String, Object> entry : aMap.entrySet()) {
                if (first) {
                    // once the loop has started, the next iteration won't be first
                    first = false;
                } else {
                    // if not on the first iteration, prepend a separator
                    sb.append(anEntrySeparator);
                }
                sb.append(escapeString(entry.getKey(), anEscapeChars, anEscapeSymbol));
                sb.append(aKeyValueSeparator);
                Object value = entry.getValue();
                if (value instanceof Object[]) {
                    sb.append(arrayToString((Object[]) value, anArrayPrefix, anArraySeparator, anArraySuffix,
                            anEscapeChars, anEscapeSymbol));
                } else {
                    sb.append(escapeString(value.toString(), anEscapeChars, anEscapeSymbol));
                }
            }
        }
        if (aSuffix != null) {
            sb.append(aSuffix);
        }
        return sb.toString();
    }

    /**
     * Escapes the specified characters of a string by prepending an escape
     * symbol. Only one pass is made through the string, so that the escape
     * symbol may safely be present in the list of characters to escape.
     *
     * @param string the string to escape.
     * @param escapeChars the characters to escape. Every occurrence of any of
     * the characters in this string is prepended with the escape symbol. If
     * this argument is empty or null, this method does nothing.
     * @param escapeSymbol a symbol used to escape the required characters.
     * @return a string similar to the input but with all of the characters in
     * {@code escapeChars} preceded by an escape sequence
     * @since 1.0.0
     */
    public static String escapeString(String string, String escapeChars, char escapeSymbol) {
        if (string == null || string.isEmpty() || escapeChars == null || escapeChars.isEmpty()) {
            return string;
        }
        StringBuilder sb = new StringBuilder(string.length());
        for (int index = 0; index < string.length(); index++) {
            char ch = string.charAt(index);
            if (escapeChars.indexOf(ch) >= 0)
                sb.append(escapeSymbol);
            sb.append(ch);
        }
        return sb.toString();
    }

    /**
     * @brief Converts an array of objects into a string.
     *
     * Non-string elements will be converted to strings using their `toString()`
     * method. This method does not currently support primitive or nested
     * arrays.
     *
     * The return value has the following format:
     *
     *      <arrayPrefix><escaped element><arraySeparator> ... <arraySuffix>
     *
     * The array separator does not appear after the last element.
     *
     * Elements will have all characters that appear in `anEscapeChars` escaped
     * by preceding them with the escape symbol. The prefix, suffix and
     * separator and entry separator will not be escaped. The recommended use of
     * this method is to add the symbols in the prefix, suffix and separator to
     * the escape characters so that they can be identified unambiguously.
     *
     * To convert primitive arrays to arrays of string objects that can be
     * processed by this method, use the appropriate methods of the
     * `ArrayUtilities` class.
     *
     * @param anArray The array to convert. If `null` or empty, only the prefix
     * and suffix are concatenated.
     * @param aPrefix The prefix to delimit the beginning of the array with.
     * Omitted entirely if `null`.
     * @param aSeparator The character sequence to separate elements in the
     * array with. Defaults to a single space (' ') if `null`.
     * @param aSuffix The suffix to delimit the end of the array with. Omitted
     * entirely if `null`.
     * @param anEscapeChars A string containing the characters that need to be
     * escaped in the elements. Note that characters in the prefix, suffix and
     * separator are not escaped. No escaping is done if `null` or empty.
     * @param anEscapeSymbol The symbol to be inserted before characters that
     * need to be escaped. May be used to escape itself.
     * @return A string representation of the array. This value is never `null`,
     * although it may be empty if the array, prefix and suffix are empty.
     * @see ArrayUtilities
     * @since 3.0.0
     */
    public static String arrayToString(Object[] anArray, String aPrefix, String aSeparator, String aSuffix,
            String anEscapeChars, char anEscapeSymbol) {
        if (aPrefix == null)
            aPrefix = "";
        if (aSeparator == null)
            aSeparator = " ";
        if (aSuffix == null)
            aSuffix = "";

        if (anArray == null || anArray.length == 0)
            return aPrefix + aSuffix;

        StringBuilder sb = new StringBuilder();

        sb.append(aPrefix);
        for (int i = 0; i < anArray.length; i++) {
            if (i > 0)
                sb.append(aSeparator);
            sb.append(escapeString(anArray[i].toString(), anEscapeChars, anEscapeSymbol));
        }
        sb.append(aSuffix);
        return sb.toString();
    }
}

Related

  1. mapToString(Map map)
  2. mapToString(Map map)
  3. mapToString(Map map)
  4. mapToString(Map parameterMap)
  5. mapToString(Map map, String splitter)
  6. mapToString(String itemName, Map map)
  7. mapToStringArray(Map map)
  8. mapToStringBuilder(Map m)
  9. mapToStringList(Map map, String pairDelimiter, String listDelimiter)