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.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1800299 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Brohl
2017-06-29 16:04:01 +00:00
parent babe08a1b2
commit eab1123394
@@ -95,54 +95,6 @@ public final class UtilMisc {
return col.iterator();
}
/**
* 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
@@ -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
* Create a set from the passed objects.
* @param data
* @return
*/
public static <T> Set<T> toSet(T obj1) {
@SafeVarargs
public static <T> Set<T> toSet(T... data) {
if (data == null) {
return null;
}
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
*/
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) {
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) {
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) {
/**
* 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);
list.add(obj7);
list.add(obj8);
list.add(obj9);
for(T t : data){
list.add(t);
}
return list;
}