svn commit: r1862228 - in /ofbiz/ofbiz-framework/trunk/framework/base/src: main/java/org/apache/ofbiz/base/conversion/ main/java/org/apache/ofbiz/base/util/ test/java/org/apache/ofbiz/base/conversion/ test/java/org/apache/ofbiz/base/util/

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1862228 - in /ofbiz/ofbiz-framework/trunk/framework/base/src: main/java/org/apache/ofbiz/base/conversion/ main/java/org/apache/ofbiz/base/util/ test/java/org/apache/ofbiz/base/conversion/ test/java/org/apache/ofbiz/base/util/

mthl
Author: mthl
Date: Thu Jun 27 14:40:25 2019
New Revision: 1862228

URL: http://svn.apache.org/viewvc?rev=1862228&view=rev
Log:
Improved: Inline ‘StringUtil#toMap’
(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.

Added:
    ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/conversion/CollectionConvertersTest.java   (with props)
Modified:
    ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/conversion/CollectionConverters.java
    ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/StringUtil.java
    ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/StringUtilTests.java

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/conversion/CollectionConverters.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/conversion/CollectionConverters.java?rev=1862228&r1=1862227&r2=1862228&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/conversion/CollectionConverters.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/conversion/CollectionConverters.java Thu Jun 27 14:40:25 2019
@@ -25,6 +25,7 @@ import java.util.LinkedList;
 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;
@@ -167,10 +168,14 @@ public class CollectionConverters implem
 
         @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]));
         }
     }
 

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/StringUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/StringUtil.java?rev=1862228&r1=1862227&r2=1862228&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/StringUtil.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/StringUtil.java Thu Jun 27 14:40:25 2019
@@ -212,33 +212,6 @@ public class StringUtil {
         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
      *

Added: ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/conversion/CollectionConvertersTest.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/conversion/CollectionConvertersTest.java?rev=1862228&view=auto
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/conversion/CollectionConvertersTest.java (added)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/conversion/CollectionConvertersTest.java Thu Jun 27 14:40:25 2019
@@ -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 }"));
+    }
+}

Propchange: ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/conversion/CollectionConvertersTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/conversion/CollectionConvertersTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/conversion/CollectionConvertersTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/StringUtilTests.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/StringUtilTests.java?rev=1862228&r1=1862227&r2=1862228&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/StringUtilTests.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/test/java/org/apache/ofbiz/base/util/StringUtilTests.java Thu Jun 27 14:40:25 2019
@@ -84,23 +84,6 @@ public class StringUtilTests {
     }
 
     @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[] {"", "[", "]", "]["}) {
             IllegalArgumentException caught = null;