svn commit: r1800299 - /ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java

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

svn commit: r1800299 - /ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java

mbrohl
Author: mbrohl
Date: Thu Jun 29 16:04:01 2017
New Revision: 1800299

URL: http://svn.apache.org/viewvc?rev=1800299&view=rev
Log:
Improved: UtilMisc.toMap(), toList() and toSet() refactoring taking Varargs for
creating the wanted data structure.
(OFBIZ-9393)

This patch removes a number of outdated methods that took a fixed number of
objects to create a data structure, such as a linked list. Since the code is
outdated and inflexible, this patch removes these methods and added methods to
create the given data structure via a variable amount of arguments.
This approach also ensures that no other code needs a refactoring. In addition
the method populateMap() was removed which was private and was only used by the
multiple outdated toMap() functions.

Thanks Martin Becker for reporting and providing the patch.

Modified:
    ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java?rev=1800299&r1=1800298&r2=1800299&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java Thu Jun 29 16:04:01 2017
@@ -99,54 +99,6 @@ public final class UtilMisc {
      * Create a map from passed nameX, valueX parameters
      * @return The resulting Map
      */
-    public static <V, V1 extends V> Map<String, V> toMap(String name1, V1 value1) {
-        return populateMap(new HashMap<String, V>(), name1, value1);
-    }
-
-    /**
-     * Create a map from passed nameX, valueX parameters
-     * @return The resulting Map
-     */
-    public static <V, V1 extends V, V2 extends V> Map<String, V> toMap(String name1, V1 value1, String name2, V2 value2) {
-        return populateMap(new HashMap<String, V>(), name1, value1, name2, value2);
-    }
-
-    /**
-     * Create a map from passed nameX, valueX parameters
-     * @return The resulting Map
-     */
-    public static <V, V1 extends V, V2 extends V, V3 extends V> Map<String, V> toMap(String name1, V1 value1, String name2, V2 value2, String name3, V3 value3) {
-        return populateMap(new HashMap<String, V>(), name1, value1, name2, value2, name3, value3);
-    }
-
-    /**
-     * Create a map from passed nameX, valueX parameters
-     * @return The resulting Map
-     */
-    public static <V, V1 extends V, V2 extends V, V3 extends V, V4 extends V> Map<String, V> toMap(String name1, V1 value1, String name2, V2 value2, String name3, V3 value3, String name4, V4 value4) {
-        return populateMap(new HashMap<String, V>(), name1, value1, name2, value2, name3, value3, name4, value4);
-    }
-
-    /**
-     * Create a map from passed nameX, valueX parameters
-     * @return The resulting Map
-     */
-    public static <V, V1 extends V, V2 extends V, V3 extends V, V4 extends V, V5 extends V> Map<String, V> toMap(String name1, V1 value1, String name2, V2 value2, String name3, V3 value3, String name4, V4 value4, String name5, V5 value5) {
-        return populateMap(new HashMap<String, V>(), name1, value1, name2, value2, name3, value3, name4, value4, name5, value5);
-    }
-
-    /**
-     * Create a map from passed nameX, valueX parameters
-     * @return The resulting Map
-     */
-    public static <V, V1 extends V, V2 extends V, V3 extends V, V4 extends V, V5 extends V, V6 extends V> Map<String, V> toMap(String name1, V1 value1, String name2, V2 value2, String name3, V3 value3, String name4, V4 value4, String name5, V5 value5, String name6, V6 value6) {
-        return populateMap(new HashMap<String, V>(), name1, value1, name2, value2, name3, value3, name4, value4, name5, value5, name6, value6);
-    }
-
-    /**
-     * Create a map from passed nameX, valueX parameters
-     * @return The resulting Map
-     */
     @SuppressWarnings("unchecked")
     public static <K, V> Map<String, V> toMap(Object... data) {
         if (data.length == 1 && data[0] instanceof Map) {
@@ -164,14 +116,6 @@ public final class UtilMisc {
         return map;
     }
 
-    @SuppressWarnings("unchecked")
-    private static <K, V> Map<String, V> populateMap(Map<String, V> map, Object... data) {
-        for (int i = 0; i < data.length;) {
-            map.put((String) data[i++], (V) data[i++]);
-        }
-        return map;
-    }
-
     public static <K, V> String printMap(Map<? extends K, ? extends V> theMap) {
         StringBuilder theBuf = new StringBuilder();
         for (Map.Entry<? extends K, ? extends V> entry: theMap.entrySet()) {
@@ -312,90 +256,19 @@ public final class UtilMisc {
     }
 
     /**
-     * Create a Set from passed objX parameters
-     * @return The resulting Set
-     */
-    public static <T> Set<T> toSet(T obj1) {
-        Set<T> theSet = new LinkedHashSet<T>();
-        theSet.add(obj1);
-        return theSet;
-    }
-
-    /**
-     * Create a Set from passed objX parameters
-     * @return The resulting Set
-     */
-    public static <T> Set<T> toSet(T obj1, T obj2) {
-        Set<T> theSet = new LinkedHashSet<T>();
-        theSet.add(obj1);
-        theSet.add(obj2);
-        return theSet;
-    }
-
-    /**
-     * Create a Set from passed objX parameters
-     * @return The resulting Set
-     */
-    public static <T> Set<T> toSet(T obj1, T obj2, T obj3) {
-        Set<T> theSet = new LinkedHashSet<T>();
-        theSet.add(obj1);
-        theSet.add(obj2);
-        theSet.add(obj3);
-        return theSet;
-    }
-
-    /**
-     * Create a Set from passed objX parameters
-     * @return The resulting Set
-     */
-    public static <T> Set<T> toSet(T obj1, T obj2, T obj3, T obj4) {
-        Set<T> theSet = new LinkedHashSet<T>();
-        theSet.add(obj1);
-        theSet.add(obj2);
-        theSet.add(obj3);
-        theSet.add(obj4);
-        return theSet;
-    }
-
-    /**
-     * Create a Set from passed objX parameters
-     * @return The resulting Set
-     */
-    public static <T> Set<T> toSet(T obj1, T obj2, T obj3, T obj4, T obj5) {
-        Set<T> theSet = new LinkedHashSet<T>();
-        theSet.add(obj1);
-        theSet.add(obj2);
-        theSet.add(obj3);
-        theSet.add(obj4);
-        theSet.add(obj5);
-        return theSet;
-    }
-
-    /**
-     * Create a Set from passed objX parameters
-     * @return The resulting Set
+     * Create a set from the passed objects.
+     * @param data
+     * @return
      */
-    public static <T> Set<T> toSet(T obj1, T obj2, T obj3, T obj4, T obj5, T obj6) {
-        Set<T> theSet = new LinkedHashSet<T>();
-        theSet.add(obj1);
-        theSet.add(obj2);
-        theSet.add(obj3);
-        theSet.add(obj4);
-        theSet.add(obj5);
-        theSet.add(obj6);
-        return theSet;
-    }
-    
-    public static <T> Set<T> toSet(T obj1, T obj2, T obj3, T obj4, T obj5, T obj6, T obj7, T obj8) {
+    @SafeVarargs
+    public static <T> Set<T> toSet(T... data) {        
+        if (data == null) {
+            return null;
+        }
         Set<T> theSet = new LinkedHashSet<T>();
-        theSet.add(obj1);
-        theSet.add(obj2);
-        theSet.add(obj3);
-        theSet.add(obj4);
-        theSet.add(obj5);
-        theSet.add(obj6);
-        theSet.add(obj7);
-        theSet.add(obj8);
+        for (T elem : data) {
+            theSet.add(elem);
+        }
         return theSet;
     }
 
@@ -420,100 +293,24 @@ public final class UtilMisc {
         }
         return set;
     }
-
-    /**
-     * Create a list from passed objX parameters
-     * @return The resulting List
-     */
-    public static <T> List<T> toList(T obj1) {
-        List<T> list = new LinkedList<T>();
-
-        list.add(obj1);
-        return list;
-    }
-
-    /**
-     * Create a list from passed objX parameters
-     * @return The resulting List
-     */
-    public static <T> List<T> toList(T obj1, T obj2) {
-        List<T> list = new LinkedList<T>();
-
-        list.add(obj1);
-        list.add(obj2);
-        return list;
-    }
-
-    /**
-     * Create a list from passed objX parameters
-     * @return The resulting List
-     */
-    public static <T> List<T> toList(T obj1, T obj2, T obj3) {
-        List<T> list = new LinkedList<T>();
-
-        list.add(obj1);
-        list.add(obj2);
-        list.add(obj3);
-        return list;
-    }
-
-    /**
-     * Create a list from passed objX parameters
-     * @return The resulting List
-     */
-    public static <T> List<T> toList(T obj1, T obj2, T obj3, T obj4) {
-        List<T> list = new LinkedList<T>();
-
-        list.add(obj1);
-        list.add(obj2);
-        list.add(obj3);
-        list.add(obj4);
-        return list;
-    }
-
-    /**
-     * Create a list from passed objX parameters
-     * @return The resulting List
-     */
-    public static <T> List<T> toList(T obj1, T obj2, T obj3, T obj4, T obj5) {
-        List<T> list = new LinkedList<T>();
-
-        list.add(obj1);
-        list.add(obj2);
-        list.add(obj3);
-        list.add(obj4);
-        list.add(obj5);
-        return list;
-    }
-
+    
     /**
-     * Create a list from passed objX parameters
-     * @return The resulting List
-     */
-    public static <T> List<T> toList(T obj1, T obj2, T obj3, T obj4, T obj5, T obj6) {
+     * Creates a list from passed objects.
+     * @param data
+     * @return
+     */
+    @SafeVarargs
+    public static <T> List<T> toList(T... data) {
+        if(data == null){
+            return null;
+        }
+        
         List<T> list = new LinkedList<T>();
 
-        list.add(obj1);
-        list.add(obj2);
-        list.add(obj3);
-        list.add(obj4);
-        list.add(obj5);
-        list.add(obj6);
-        return list;
-    }
-    
-    public static <T> List<T> toList(T obj1, T obj2, T obj3, T obj4, T obj5, T obj6, T obj7, T obj8, T obj9) {
-        List<T> list = new LinkedList<T>();
+        for(T t : data){
+            list.add(t);
+        }
 
-        list.add(obj1);
-        list.add(obj2);
-        list.add(obj3);
-        list.add(obj4);
-        list.add(obj5);
-        list.add(obj6);
-        list.add(obj7);
-        list.add(obj8);
-        list.add(obj9);
         return list;
     }