Skip to content

Commit

Permalink
Implemented: Add new StringUtil function to convert map to encoded st…
Browse files Browse the repository at this point in the history
…ring (#852)

Reverse logical of StringUtil.strToMap, the function StringUtil.mapToStr convert a map to an encoded String
  • Loading branch information
nmalin authored Nov 22, 2024
1 parent 8d69f18 commit 4bbd638
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -216,6 +217,30 @@ public static Map<String, String> strToMap(String str) {
return strToMap(str, "|", false);
}

/**
* Creates an encoded String from a Map of name/value pairs
* @param mapToConvert The Map of name/value pairs
* @return String The encoded String like key1=value1|key2=value2, null if map is empty
*/
public static String mapToStr(Map<? extends Object, ? extends Object> mapToConvert) {
if (UtilValidate.isEmpty(mapToConvert)) {
return null;
}
return mapToConvert.entrySet().stream().map(entry -> {
String key = String.valueOf(entry.getKey());
String value = String.valueOf(entry.getValue());

try {
return new StringBuilder(URLEncoder.encode(key, "UTF-8"))
.append("=")
.append(URLEncoder.encode(value, "UTF-8"));
} catch (UnsupportedEncodingException e) {
Debug.logError(e, MODULE);
}
return "";
}).collect(Collectors.joining("|"));
}

/**
* Reads a String version of a List (should contain only strings) and creates a new List
* @param s String value of a Map ({n1=v1, n2=v2})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.apache.ofbiz.base.util;

import com.ibm.icu.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -83,6 +86,33 @@ public void testStrToMap() {
StringUtil.strToMap(" 1 = one | 2 = two ", true));
}

@Test
public void testMapToStr() {
// Test Null
assertNull("null-string", StringUtil.mapToStr(null));
assertNull("empty", StringUtil.mapToStr(Map.of()));

// Test simple case
assertEquals("single", "1=one", StringUtil.mapToStr(Map.of("1", "one")));
LinkedHashMap<String, String> doubleMap = new LinkedHashMap<>();
doubleMap.put("1", "one");
doubleMap.put("2", "two");
assertEquals("double", "1=one|2=two", StringUtil.mapToStr(doubleMap));

// Test with object case
LinkedHashMap<Object, Object> doubleObjectMap = new LinkedHashMap<>();
doubleObjectMap.put(Integer.valueOf(1), Long.valueOf(1));
doubleObjectMap.put(Integer.valueOf(2), BigDecimal.ONE);
assertEquals("double with number classe", "1=1|2=1", StringUtil.mapToStr(doubleObjectMap));

// Test with special char
assertEquals("single with =", "1=%3Done", StringUtil.mapToStr(Map.of("1", "=one")));
LinkedHashMap<String, String> doublePipeMap = new LinkedHashMap<>();
doublePipeMap.put("1", "|one");
doublePipeMap.put("2|", "two");
assertEquals("double with pipe", "1=%7Cone|2%7C=two", StringUtil.mapToStr(doublePipeMap));
}

@Test
public void testToList() {
for (String s: new String[] {"", "[", "]", "]["}) {
Expand Down

0 comments on commit 4bbd638

Please sign in to comment.