Improved: Make ‘UtilGenerics#checkCollection(Object, Class)’ more generic

(OFBIZ-11141)

Remove ‘checkList’, ‘checkSet’ and ‘checkStack’ by using
‘checkCollection’ instead.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1863489 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mathieu Lirzin
2019-07-20 16:18:23 +00:00
parent 1e22cbc6a0
commit c2e2c3fb15
10 changed files with 31 additions and 51 deletions
@@ -23,7 +23,6 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
public final class UtilGenerics {
@@ -55,20 +54,15 @@ public final class UtilGenerics {
return cast(object);
}
public static <T> Collection<T> checkCollection(Object object, Class<T> type) {
public static <E, C extends Collection<E>> C checkCollection(Object object, Class<E> type) {
checkCollectionContainment(object, Collection.class, type);
return checkCollection(object);
return cast(object);
}
public static <T> List<T> checkList(Object object) {
return cast(object);
}
public static <T> List<T> checkList(Object object, Class<T> type) {
checkCollectionContainment(object, List.class, type);
return checkList(object);
}
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> checkMap(Object object) {
if (object != null && !(object instanceof Map)) {
@@ -97,20 +91,10 @@ public final class UtilGenerics {
return checkMap(object);
}
public static <T> Stack<T> checkStack(Object object, Class<T> type) {
checkCollectionContainment(object, Stack.class, type);
return cast(object);
}
public static <T> Set<T> checkSet(Object object) {
return cast(object);
}
public static <T> Set<T> checkSet(Object object, Class<T> type) {
checkCollectionContainment(object, Set.class, type);
return checkSet(object);
}
/** Returns the Object argument as a parameterized List if the Object argument
* is an instance of List. Otherwise returns null.
*/
@@ -23,7 +23,6 @@ import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static org.apache.ofbiz.base.util.UtilGenerics.checkList;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
@@ -236,7 +235,7 @@ public final class UtilHttp {
if (multiPartMap.containsKey(fieldName)) {
Object mapValue = multiPartMap.get(fieldName);
if (mapValue instanceof List<?>) {
checkList(mapValue, Object.class).add(item.getString());
UtilGenerics.checkCollection(mapValue, Object.class).add(item.getString());
} else if (mapValue instanceof String) {
List<String> newList = new LinkedList<>();
newList.add((String) mapValue);