From c2e2c3fb1561392c8361866a13181cf0b3effc0d Mon Sep 17 00:00:00 2001 From: Mathieu Lirzin Date: Sat, 20 Jul 2019 16:18:23 +0000 Subject: [PATCH] =?UTF-8?q?Improved:=20Make=20=E2=80=98UtilGenerics#checkC?= =?UTF-8?q?ollection(Object,=20Class)=E2=80=99=20more=20generic=20(OFBIZ-1?= =?UTF-8?q?1141)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../workeffort/workeffort/ICalConverter.java | 10 +++++----- .../apache/ofbiz/base/util/UtilGenerics.java | 20 ++----------------- .../org/apache/ofbiz/base/util/UtilHttp.java | 3 +-- .../ofbiz/base/util/UtilGenericsTest.java | 9 +++++---- .../apache/ofbiz/common/CommonServices.java | 4 ++-- .../org/apache/ofbiz/common/FindServices.java | 6 +++--- .../org/apache/ofbiz/common/FtpServices.java | 5 ++--- .../ofbiz/common/status/StatusServices.java | 5 ++--- .../ofbiz/common/telecom/TelecomServices.java | 5 ++--- .../synchronization/EntitySyncServices.java | 15 +++++++------- 10 files changed, 31 insertions(+), 51 deletions(-) diff --git a/applications/workeffort/src/main/java/org/apache/ofbiz/workeffort/workeffort/ICalConverter.java b/applications/workeffort/src/main/java/org/apache/ofbiz/workeffort/workeffort/ICalConverter.java index f84e8f9513..858700714f 100644 --- a/applications/workeffort/src/main/java/org/apache/ofbiz/workeffort/workeffort/ICalConverter.java +++ b/applications/workeffort/src/main/java/org/apache/ofbiz/workeffort/workeffort/ICalConverter.java @@ -440,7 +440,7 @@ public class ICalConverter { protected static List getRelatedWorkEfforts(GenericValue workEffort, Map context) { Map serviceMap = UtilMisc.toMap("workEffortId", workEffort.getString("workEffortId")); Map resultMap = invokeService("getICalWorkEfforts", serviceMap, context); - List workEfforts = UtilGenerics.checkList(resultMap.get("workEfforts"), GenericValue.class); + List workEfforts = UtilGenerics.checkCollection(resultMap.get("workEfforts"), GenericValue.class); if (workEfforts != null) { return WorkEffortWorker.removeDuplicateWorkEfforts(workEfforts); } @@ -735,7 +735,7 @@ public class ICalConverter { validWorkEfforts.add(workEffort.getString("workEffortId")); } } - List components = UtilGenerics.checkList(calendar.getComponents(), Component.class); + List 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 @@ public class ICalConverter { ResponseProperties responseProps = null; Map serviceMap = new HashMap<>(); List 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) { diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilGenerics.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilGenerics.java index 8f42e712f3..286a2fc071 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilGenerics.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilGenerics.java @@ -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 Collection checkCollection(Object object, Class type) { + public static > C checkCollection(Object object, Class type) { checkCollectionContainment(object, Collection.class, type); - return checkCollection(object); + return cast(object); } public static List checkList(Object object) { return cast(object); } - public static List checkList(Object object, Class type) { - checkCollectionContainment(object, List.class, type); - return checkList(object); - } - @SuppressWarnings("unchecked") public static Map checkMap(Object object) { if (object != null && !(object instanceof Map)) { @@ -97,20 +91,10 @@ public final class UtilGenerics { return checkMap(object); } - public static Stack checkStack(Object object, Class type) { - checkCollectionContainment(object, Stack.class, type); - return cast(object); - } - public static Set checkSet(Object object) { return cast(object); } - public static Set checkSet(Object object, Class 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. */ diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java index 139847a790..ea9e45a9a3 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilHttp.java @@ -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 newList = new LinkedList<>(); newList.add((String) mapValue); diff --git a/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilGenericsTest.java b/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilGenericsTest.java index 17dde96823..ac05dd9a50 100644 --- a/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilGenericsTest.java +++ b/framework/base/src/test/java/org/apache/ofbiz/base/util/UtilGenericsTest.java @@ -21,6 +21,7 @@ package org.apache.ofbiz.base.util; import static org.junit.Assert.assertNull; import java.util.Arrays; +import java.util.Collection; import org.junit.Test; @@ -28,21 +29,21 @@ public class UtilGenericsTest { @Test public void basicCheckCollection() { - UtilGenerics.checkCollection(Arrays.asList("foo", "bar", "baz"), String.class); + UtilGenerics.>checkCollection(Arrays.asList("foo", "bar", "baz"), String.class); } @Test(expected = ClassCastException.class) public void incompatibleCollectionCheckCollection() { - UtilGenerics.checkCollection("not a collection", String.class); + UtilGenerics.>checkCollection("not a collection", String.class); } @Test(expected = IllegalArgumentException.class) public void heterogenousCheckCollection() { - UtilGenerics.checkCollection(Arrays.asList("foo", 0), String.class); + UtilGenerics.>checkCollection(Arrays.asList("foo", 0), String.class); } @Test public void nullCheckCollection() { - assertNull(UtilGenerics.checkCollection(null, String.class)); + assertNull(UtilGenerics.>checkCollection(null, String.class)); } } diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/CommonServices.java b/framework/common/src/main/java/org/apache/ofbiz/common/CommonServices.java index 83af117dae..342f0ac5dc 100644 --- a/framework/common/src/main/java/org/apache/ofbiz/common/CommonServices.java +++ b/framework/common/src/main/java/org/apache/ofbiz/common/CommonServices.java @@ -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 class CommonServices { } public static Map simpleMapListTest(DispatchContext dctx, Map context) { - List listOfStrings = checkList(context.get("listOfStrings"), String.class); + List listOfStrings = checkCollection(context.get("listOfStrings"), String.class); Map mapOfStrings = checkMap(context.get("mapOfStrings"), String.class, String.class); for (String str: listOfStrings) { diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/FindServices.java b/framework/common/src/main/java/org/apache/ofbiz/common/FindServices.java index 72bb560463..578c43ad2f 100644 --- a/framework/common/src/main/java/org/apache/ofbiz/common/FindServices.java +++ b/framework/common/src/main/java/org/apache/ofbiz/common/FindServices.java @@ -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 class FindServices { return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonFindErrorPreparingConditions", UtilMisc.toMap("errorString", gse.getMessage()), locale)); } EntityConditionList exprList = UtilGenerics.cast(prepareResult.get("entityConditionList")); - List orderByList = checkList(prepareResult.get("orderByList"), String.class); + List orderByList = checkCollection(prepareResult.get("orderByList"), String.class); Map executeResult = null; try { @@ -667,7 +667,7 @@ public class FindServices { public static Map executeFind(DispatchContext dctx, Map context) { String entityName = (String) context.get("entityName"); EntityConditionList entityConditionList = UtilGenerics.cast(context.get("entityConditionList")); - List orderByList = checkList(context.get("orderByList"), String.class); + List orderByList = checkCollection(context.get("orderByList"), String.class); boolean noConditionFind = "Y".equals(context.get("noConditionFind")); boolean distinct = "Y".equals(context.get("distinct")); List fieldList = UtilGenerics.checkList(context.get("fieldList")); diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/FtpServices.java b/framework/common/src/main/java/org/apache/ofbiz/common/FtpServices.java index ba633ffd69..52f6406814 100644 --- a/framework/common/src/main/java/org/apache/ofbiz/common/FtpServices.java +++ b/framework/common/src/main/java/org/apache/ofbiz/common/FtpServices.java @@ -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 @@ public class FtpServices { errorList.add(UtilProperties.getMessage(resource, "CommonFtpFileNotSentSuccesfully", UtilMisc.toMap("replyString", ftp.getReplyString()), locale)); } else { Debug.logInfo("[putFile] store was successful", module); - List siteCommands = checkList(context.get("siteCommands"), String.class); + List siteCommands = checkCollection(context.get("siteCommands"), String.class); if (siteCommands != null) { for (String command : siteCommands) { Debug.logInfo("[putFile] sending SITE command: " + command, module); diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/status/StatusServices.java b/framework/common/src/main/java/org/apache/ofbiz/common/status/StatusServices.java index 80651deaa2..433909fc6a 100644 --- a/framework/common/src/main/java/org/apache/ofbiz/common/status/StatusServices.java +++ b/framework/common/src/main/java/org/apache/ofbiz/common/status/StatusServices.java @@ -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 class StatusServices { public static Map getStatusItems(DispatchContext ctx, Map context) { Delegator delegator = ctx.getDelegator(); - List statusTypes = checkList(context.get("statusTypeIds"), String.class); + List 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)); diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/telecom/TelecomServices.java b/framework/common/src/main/java/org/apache/ofbiz/common/telecom/TelecomServices.java index 4d21cfae0d..b3e61fa78c 100644 --- a/framework/common/src/main/java/org/apache/ofbiz/common/telecom/TelecomServices.java +++ b/framework/common/src/main/java/org/apache/ofbiz/common/telecom/TelecomServices.java @@ -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 @@ public class TelecomServices { String telecomMsgTypeEnumId = (String) context.get("telecomMsgTypeEnumId"); String telecomMethodTypeId = (String) context.get("telecomMethodTypeId"); String telecomGatewayConfigId = (String) context.get("telecomGatewayConfigId"); - List numbers = checkList(context.get("numbers"), String.class); + List numbers = checkCollection(context.get("numbers"), String.class); String message = (String) context.get("message"); String telecomEnabled = EntityUtilProperties.getPropertyValue("general", "telecom.notifications.enabled", delegator); diff --git a/framework/entityext/src/main/java/org/apache/ofbiz/entityext/synchronization/EntitySyncServices.java b/framework/entityext/src/main/java/org/apache/ofbiz/entityext/synchronization/EntitySyncServices.java index 33c3b95c7e..5ba9a115a3 100644 --- a/framework/entityext/src/main/java/org/apache/ofbiz/entityext/synchronization/EntitySyncServices.java +++ b/framework/entityext/src/main/java/org/apache/ofbiz/entityext/synchronization/EntitySyncServices.java @@ -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 @@ public class EntitySyncServices { gotMoreData = true; // at least one of the is not empty, make sure none of them are null now too... - List valuesToCreate = checkList(result.get("valuesToCreate"), GenericValue.class); + List valuesToCreate = checkCollection(result.get("valuesToCreate"), GenericValue.class); if (valuesToCreate == null) valuesToCreate = Collections.emptyList(); - List valuesToStore = checkList(result.get("valuesToStore"), GenericValue.class); + List valuesToStore = checkCollection(result.get("valuesToStore"), GenericValue.class); if (valuesToStore == null) valuesToStore = Collections.emptyList(); - List keysToRemove = checkList(result.get("keysToRemove"), GenericEntity.class); + List keysToRemove = checkCollection(result.get("keysToRemove"), GenericEntity.class); if (keysToRemove == null) keysToRemove = Collections.emptyList(); Map callLocalStoreContext = UtilMisc.toMap("entitySyncId", entitySyncId, "delegatorName", context.get("localDelegatorName"), @@ -557,9 +556,9 @@ public class EntitySyncServices { // de-serialize the value lists try { - List valuesToCreate = checkList(XmlSerializer.deserialize(createString, delegator), GenericValue.class); - List valuesToStore = checkList(XmlSerializer.deserialize(storeString, delegator), GenericValue.class); - List keysToRemove = checkList(XmlSerializer.deserialize(removeString, delegator), GenericEntity.class); + List valuesToCreate = checkCollection(XmlSerializer.deserialize(createString, delegator), GenericValue.class); + List valuesToStore = checkCollection(XmlSerializer.deserialize(storeString, delegator), GenericValue.class); + List keysToRemove = checkCollection(XmlSerializer.deserialize(removeString, delegator), GenericEntity.class); Map storeContext = UtilMisc.toMap("entitySyncId", entitySyncId, "valuesToCreate", valuesToCreate, "valuesToStore", valuesToStore, "keysToRemove", keysToRemove, "userLogin", userLogin);