Skip to content
Permalink
Browse files

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

…eric

(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
  • Loading branch information
mthl committed Jul 20, 2019
1 parent 1e22cbc commit c2e2c3fb1561392c8361866a13181cf0b3effc0d
@@ -440,7 +440,7 @@ protected static void getPartyUrl(Property property, GenericValue partyAssign, M
protected static List<GenericValue> getRelatedWorkEfforts(GenericValue workEffort, Map<String, Object> context) {
Map<String, ? extends Object> serviceMap = UtilMisc.toMap("workEffortId", workEffort.getString("workEffortId"));
Map<String, Object> resultMap = invokeService("getICalWorkEfforts", serviceMap, context);
List<GenericValue> workEfforts = UtilGenerics.checkList(resultMap.get("workEfforts"), GenericValue.class);
List<GenericValue> workEfforts = UtilGenerics.checkCollection(resultMap.get("workEfforts"), GenericValue.class);
if (workEfforts != null) {
return WorkEffortWorker.removeDuplicateWorkEfforts(workEfforts);
}
@@ -735,7 +735,7 @@ public static ResponseProperties storeCalendar(InputStream is, Map<String, Objec
validWorkEfforts.add(workEffort.getString("workEffortId"));
}
}
List<Component> components = UtilGenerics.checkList(calendar.getComponents(), Component.class);
List<Component> components = UtilGenerics.checkCollection(calendar.getComponents(), Component.class);
ResponseProperties responseProps = null;
for (Component component : components) {
if (Component.VEVENT.equals(component.getName()) || Component.VTODO.equals(component.getName())) {
@@ -784,9 +784,9 @@ protected static ResponseProperties storePartyAssignments(String workEffortId, C
ResponseProperties responseProps = null;
Map<String, Object> serviceMap = new HashMap<>();
List<Property> partyList = new LinkedList<>();
partyList.addAll(UtilGenerics.checkList(component.getProperties("ATTENDEE"), Property.class));
partyList.addAll(UtilGenerics.checkList(component.getProperties("CONTACT"), Property.class));
partyList.addAll(UtilGenerics.checkList(component.getProperties("ORGANIZER"), Property.class));
partyList.addAll(UtilGenerics.checkCollection(component.getProperties("ATTENDEE"), Property.class));
partyList.addAll(UtilGenerics.checkCollection(component.getProperties("CONTACT"), Property.class));
partyList.addAll(UtilGenerics.checkCollection(component.getProperties("ORGANIZER"), Property.class));
for (Property property : partyList) {
String partyId = fromXParameter(property.getParameters(), partyIdXParamName);
if (partyId == null) {
@@ -23,7 +23,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

public final class UtilGenerics {

@@ -55,20 +54,15 @@ private 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 @@ private 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.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 @@ private static Object transformParamValue(String[] value) {
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);
@@ -21,28 +21,29 @@
import static org.junit.Assert.assertNull;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;

public class UtilGenericsTest {

@Test
public void basicCheckCollection() {
UtilGenerics.<String>checkCollection(Arrays.asList("foo", "bar", "baz"), String.class);
UtilGenerics.<String, Collection<String>>checkCollection(Arrays.asList("foo", "bar", "baz"), String.class);
}

@Test(expected = ClassCastException.class)
public void incompatibleCollectionCheckCollection() {
UtilGenerics.<String>checkCollection("not a collection", String.class);
UtilGenerics.<String, Collection<String>>checkCollection("not a collection", String.class);
}

@Test(expected = IllegalArgumentException.class)
public void heterogenousCheckCollection() {
UtilGenerics.<String>checkCollection(Arrays.asList("foo", 0), String.class);
UtilGenerics.<String, Collection<String>>checkCollection(Arrays.asList("foo", 0), String.class);
}

@Test
public void nullCheckCollection() {
assertNull(UtilGenerics.<String>checkCollection(null, String.class));
assertNull(UtilGenerics.<String, Collection<String>>checkCollection(null, String.class));
}
}
@@ -18,7 +18,7 @@
*******************************************************************************/
package org.apache.ofbiz.common;

import static org.apache.ofbiz.base.util.UtilGenerics.checkList;
import static org.apache.ofbiz.base.util.UtilGenerics.checkCollection;
import static org.apache.ofbiz.base.util.UtilGenerics.checkMap;

import java.io.BufferedReader;
@@ -432,7 +432,7 @@
}

public static Map<String, Object> simpleMapListTest(DispatchContext dctx, Map<String, ?> context) {
List<String> listOfStrings = checkList(context.get("listOfStrings"), String.class);
List<String> listOfStrings = checkCollection(context.get("listOfStrings"), String.class);
Map<String, String> mapOfStrings = checkMap(context.get("mapOfStrings"), String.class, String.class);

for (String str: listOfStrings) {
@@ -18,7 +18,7 @@
*******************************************************************************/
package org.apache.ofbiz.common;

import static org.apache.ofbiz.base.util.UtilGenerics.checkList;
import static org.apache.ofbiz.base.util.UtilGenerics.checkCollection;
import static org.apache.ofbiz.base.util.UtilGenerics.checkMap;

import java.sql.Timestamp;
@@ -551,7 +551,7 @@ public static EntityCondition createSingleCondition(ModelField modelField, Strin
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonFindErrorPreparingConditions", UtilMisc.toMap("errorString", gse.getMessage()), locale));
}
EntityConditionList<EntityCondition> exprList = UtilGenerics.cast(prepareResult.get("entityConditionList"));
List<String> orderByList = checkList(prepareResult.get("orderByList"), String.class);
List<String> orderByList = checkCollection(prepareResult.get("orderByList"), String.class);

Map<String, Object> executeResult = null;
try {
@@ -667,7 +667,7 @@ public static EntityCondition createSingleCondition(ModelField modelField, Strin
public static Map<String, Object> executeFind(DispatchContext dctx, Map<String, ?> context) {
String entityName = (String) context.get("entityName");
EntityConditionList<EntityCondition> entityConditionList = UtilGenerics.cast(context.get("entityConditionList"));
List<String> orderByList = checkList(context.get("orderByList"), String.class);
List<String> orderByList = checkCollection(context.get("orderByList"), String.class);
boolean noConditionFind = "Y".equals(context.get("noConditionFind"));
boolean distinct = "Y".equals(context.get("distinct"));
List<String> fieldList = UtilGenerics.checkList(context.get("fieldList"));
@@ -18,8 +18,7 @@
*******************************************************************************/
package org.apache.ofbiz.common;

import static org.apache.ofbiz.base.util.UtilGenerics.checkList;

import static org.apache.ofbiz.base.util.UtilGenerics.checkCollection;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -96,7 +95,7 @@
errorList.add(UtilProperties.getMessage(resource, "CommonFtpFileNotSentSuccesfully", UtilMisc.toMap("replyString", ftp.getReplyString()), locale));
} else {
Debug.logInfo("[putFile] store was successful", module);
List<String> siteCommands = checkList(context.get("siteCommands"), String.class);
List<String> siteCommands = checkCollection(context.get("siteCommands"), String.class);
if (siteCommands != null) {
for (String command : siteCommands) {
Debug.logInfo("[putFile] sending SITE command: " + command, module);
@@ -18,8 +18,7 @@
*******************************************************************************/
package org.apache.ofbiz.common.status;

import static org.apache.ofbiz.base.util.UtilGenerics.checkList;

import static org.apache.ofbiz.base.util.UtilGenerics.checkCollection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
@@ -46,7 +45,7 @@

public static Map<String, Object> getStatusItems(DispatchContext ctx, Map<String, ?> context) {
Delegator delegator = ctx.getDelegator();
List<String> statusTypes = checkList(context.get("statusTypeIds"), String.class);
List<String> statusTypes = checkCollection(context.get("statusTypeIds"), String.class);
Locale locale = (Locale) context.get("locale");
if (UtilValidate.isEmpty(statusTypes)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonStatusMandatory", locale));
@@ -18,8 +18,7 @@
*******************************************************************************/
package org.apache.ofbiz.common.telecom;

import static org.apache.ofbiz.base.util.UtilGenerics.checkList;

import static org.apache.ofbiz.base.util.UtilGenerics.checkCollection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -51,7 +50,7 @@
String telecomMsgTypeEnumId = (String) context.get("telecomMsgTypeEnumId");
String telecomMethodTypeId = (String) context.get("telecomMethodTypeId");
String telecomGatewayConfigId = (String) context.get("telecomGatewayConfigId");
List<String> numbers = checkList(context.get("numbers"), String.class);
List<String> numbers = checkCollection(context.get("numbers"), String.class);
String message = (String) context.get("message");

String telecomEnabled = EntityUtilProperties.getPropertyValue("general", "telecom.notifications.enabled", delegator);
@@ -18,8 +18,7 @@
*******************************************************************************/
package org.apache.ofbiz.entityext.synchronization;

import static org.apache.ofbiz.base.util.UtilGenerics.checkList;

import static org.apache.ofbiz.base.util.UtilGenerics.checkCollection;
import java.io.IOException;
import java.net.URL;
import java.sql.Timestamp;
@@ -325,11 +324,11 @@
gotMoreData = true;

// at least one of the is not empty, make sure none of them are null now too...
List<GenericValue> valuesToCreate = checkList(result.get("valuesToCreate"), GenericValue.class);
List<GenericValue> valuesToCreate = checkCollection(result.get("valuesToCreate"), GenericValue.class);
if (valuesToCreate == null) valuesToCreate = Collections.emptyList();
List<GenericValue> valuesToStore = checkList(result.get("valuesToStore"), GenericValue.class);
List<GenericValue> valuesToStore = checkCollection(result.get("valuesToStore"), GenericValue.class);
if (valuesToStore == null) valuesToStore = Collections.emptyList();
List<GenericEntity> keysToRemove = checkList(result.get("keysToRemove"), GenericEntity.class);
List<GenericEntity> keysToRemove = checkCollection(result.get("keysToRemove"), GenericEntity.class);
if (keysToRemove == null) keysToRemove = Collections.emptyList();

Map<String, Object> callLocalStoreContext = UtilMisc.toMap("entitySyncId", entitySyncId, "delegatorName", context.get("localDelegatorName"),
@@ -557,9 +556,9 @@

// de-serialize the value lists
try {
List<GenericValue> valuesToCreate = checkList(XmlSerializer.deserialize(createString, delegator), GenericValue.class);
List<GenericValue> valuesToStore = checkList(XmlSerializer.deserialize(storeString, delegator), GenericValue.class);
List<GenericEntity> keysToRemove = checkList(XmlSerializer.deserialize(removeString, delegator), GenericEntity.class);
List<GenericValue> valuesToCreate = checkCollection(XmlSerializer.deserialize(createString, delegator), GenericValue.class);
List<GenericValue> valuesToStore = checkCollection(XmlSerializer.deserialize(storeString, delegator), GenericValue.class);
List<GenericEntity> keysToRemove = checkCollection(XmlSerializer.deserialize(removeString, delegator), GenericEntity.class);

Map<String, Object> storeContext = UtilMisc.toMap("entitySyncId", entitySyncId, "valuesToCreate", valuesToCreate,
"valuesToStore", valuesToStore, "keysToRemove", keysToRemove, "userLogin", userLogin);

0 comments on commit c2e2c3f

Please sign in to comment.
You can’t perform that action at this time.