Skip to content

Commit

Permalink
Improved: Inline ‘StringUtil#toMap’
Browse files Browse the repository at this point in the history
(OFBIZ-11014)

‘StringUtil#toMap’ was only used in the ‘CollectionConverters.StringToMap’
converter so in order to clean ‘StringUtil’ it seems better to inline it.  The
corresponding tests has been adapted to check
‘CollectionConverters.StringToMap#convert’ behavior instead.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1862228 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Jun 27, 2019
1 parent b0c0ec7 commit 8c3c6cd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.ofbiz.base.util.StringUtil;
import org.apache.ofbiz.base.util.UtilGenerics;
Expand Down Expand Up @@ -167,10 +168,14 @@ public StringToMap() {

@Override
public Map<String, String> convert(String obj) throws ConversionException {
if (obj.startsWith("{") && obj.endsWith("}")) {
return StringUtil.toMap(obj);
if (!obj.startsWith("{") || !obj.endsWith("}")) {
throw new ConversionException("Could not convert " + obj + " to Map: ");
}
throw new ConversionException("Could not convert " + obj + " to Map: ");
String kvs = obj.substring(1, obj.length() - 1);
return Arrays.stream(kvs.split("\\,\\s"))
.map(entry -> entry.split("\\="))
.filter(kv -> kv.length == 2)
.collect(Collectors.toMap(kv -> kv[0], kv -> kv[1]));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,33 +212,6 @@ public static Map<String, String> strToMap(String str) {
return strToMap(str, "|", false);
}


/**
* Reads a String version of a Map (should contain only strings) and creates a new Map.
* Partial Map elements are skipped: <code>{foo=fooValue, bar=}</code> will contain only
* the foo element.
*
* @param s String value of a Map ({n1=v1, n2=v2})
* @return new Map
*/
public static Map<String, String> toMap(String s) {
Map<String, String> newMap = new HashMap<>();
if (s.startsWith("{") && s.endsWith("}")) {
s = s.substring(1, s.length() - 1);
String[] entries = s.split("\\,\\s");
for (String entry: entries) {
String[] nv = entry.split("\\=");
if (nv.length == 2) {
newMap.put(nv[0], nv[1]);
}
}
} else {
throw new IllegalArgumentException("String is not from Map.toString()");
}

return newMap;
}

/**
* Reads a String version of a List (should contain only strings) and creates a new List
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.ofbiz.base.conversion;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.Map;

import org.apache.ofbiz.base.util.UtilMisc;
import org.junit.Test;

public class CollectionConvertersTest {

@Test
public void testToMap() throws ConversionException {
Converter<String, Map<String, String>> cvt = new CollectionConverters.StringToMap();
for (String s: new String[] {"", "{", "}", "}{"}) {
ConversionException caught = null;
try {
cvt.convert(s);
} catch (ConversionException e) {
caught = e;
} finally {
assertNotNull("bad(" + s + ")", caught);
}
}
assertEquals("single", UtilMisc.toMap("1", "one"), cvt.convert("{1=one}"));
assertEquals("double", UtilMisc.toMap("2", "two", "1", "one"), cvt.convert("{1=one, 2=two}"));
assertEquals("double-space", UtilMisc.toMap("2", "two ", " 1", "one"), cvt.convert("{ 1=one, 2=two }"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,6 @@ public void testStrToMap() {
StringUtil.strToMap(" 1 = one | 2 = two ", true));
}

@Test
public void testToMap() {
for (String s: new String[] {"", "{", "}", "}{"}) {
IllegalArgumentException caught = null;
try {
StringUtil.toMap(s);
} catch (IllegalArgumentException e) {
caught = e;
} finally {
assertNotNull("bad(" + s + ")", caught);
}
}
assertEquals("single", UtilMisc.toMap("1", "one"), StringUtil.toMap("{1=one}"));
assertEquals("double", UtilMisc.toMap("2", "two", "1", "one"), StringUtil.toMap("{1=one, 2=two}"));
assertEquals("double-space", UtilMisc.toMap("2", "two ", " 1", "one"), StringUtil.toMap("{ 1=one, 2=two }"));
}

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

0 comments on commit 8c3c6cd

Please sign in to comment.