Skip to content

Commit

Permalink
Improved: Inline ‘UtilGenerics#toMap’
Browse files Browse the repository at this point in the history
(OFBIZ-11141)


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1863497 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Jul 20, 2019
1 parent 7618a51 commit 365b044
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,4 @@ public static <T> Set<T> checkSet(Object object) {
public static <T> List<T> toList(Object obj) {
return (obj instanceof List) ? cast(obj) : null;
}

/** Returns the Object argument as a parameterized Map if the Object argument
* is an instance of Map. Otherwise returns null.
*/
public static <K, V> Map<K, V> toMap(Object obj) {
return (obj instanceof Map) ? cast(obj) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public void testJSONToMap() throws Exception {
map = new HashMap<>();
map.put("field1", "value1");
JSON json = JSON.from(map);
convertedMap = UtilGenerics.toMap(converter.convert(json));
Object obj = converter.convert(json);
convertedMap = (obj instanceof Map) ? UtilGenerics.cast(obj) : null;
assertEquals("JSON to Map", map, convertedMap);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,15 +685,17 @@ public void runAction(Map<String, Object> context) {
DispatchContext dc = WidgetWorker.getDispatcher(context).getDispatchContext();
// try a map called "parameters", try it first so values from here are overriden by values in the main context
Map<String, Object> combinedMap = new HashMap<>();
Map<String, Object> parametersObj = UtilGenerics.toMap(context.get("parameters"));
Object obj = context.get("parameters");
Map<String, Object> parametersObj = (obj instanceof Map) ? UtilGenerics.cast(obj) : null;
if (parametersObj != null) {
combinedMap.putAll(parametersObj);
}
combinedMap.putAll(context);
serviceContext = dc.makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, combinedMap);
} else if (UtilValidate.isNotEmpty(autoFieldMapString) && !"false".equals(autoFieldMapString)) {
FlexibleMapAccessor<Object> fieldFma = FlexibleMapAccessor.getInstance(autoFieldMapString);
Map<String, Object> autoFieldMap = UtilGenerics.toMap(fieldFma.get(context));
Object obj = fieldFma.get(context);
Map<String, Object> autoFieldMap = (obj instanceof Map) ? UtilGenerics.cast(obj) : null;
if (autoFieldMap != null) {
serviceContext = WidgetWorker.getDispatcher(context).getDispatchContext()
.makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, autoFieldMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,8 @@ public boolean eval(Map<String, Object> context) {
Debug.logWarning("No permission service-name specified!", module);
return false;
}
Map<String, Object> serviceContext = UtilGenerics.toMap(context.get(contextMap));
Object obj = context.get(contextMap);
Map<String, Object> serviceContext = (obj instanceof Map) ? UtilGenerics.cast(obj) : null;
if (serviceContext != null) {
// copy the required internal fields
serviceContext.put("userLogin", context.get("userLogin"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ public void renderNextPrev(Appendable writer, Map<String, Object> context, int l
}

Map<String, Object> inputFields = UtilGenerics.checkMap(context.get("requestParameters"));
Map<String, Object> queryStringMap = UtilGenerics.toMap(context.get("queryStringMap"));
Object obj = context.get("queryStringMap");
Map<String, Object> queryStringMap = (obj instanceof Map) ? UtilGenerics.cast(obj) : null;
if (UtilValidate.isNotEmpty(queryStringMap)) {
inputFields.putAll(queryStringMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public void accept(ModelConditionVisitor visitor) throws Exception {

@Override
public boolean eval(Map<String, Object> context) {
Map<String, Object> sectionsMap = UtilGenerics.toMap(context.get("sections"));
Object obj = context.get("sections");
Map<String, Object> sectionsMap = (obj instanceof Map) ? UtilGenerics.cast(obj) : null;
return !sectionsMap.containsKey(this.sectionExdr.expandString(context));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,8 @@ public boolean eval(Map<String, Object> context) {
Debug.logWarning("No permission service-name specified!", module);
return false;
}
Map<String, Object> serviceContext = UtilGenerics.toMap(context.get(contextMap));
Object obj = context.get(contextMap);
Map<String, Object> serviceContext = (obj instanceof Map) ? UtilGenerics.cast(obj) : null;
if (serviceContext != null) {
// copy the required internal fields
serviceContext.put("userLogin", context.get("userLogin"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public void putInContext(String name, Object value) {
}

public void putInContext(String menuItemName, String valueName, Object value) {
Map<String, Object> valueMap = UtilGenerics.toMap(context.get(menuItemName));
Object obj = context.get(menuItemName);
Map<String, Object> valueMap = (obj instanceof Map) ? UtilGenerics.cast(obj) : null;
if (valueMap == null) {
valueMap = new HashMap<>();
context.put(menuItemName, valueMap);
Expand All @@ -164,7 +165,8 @@ public Object getFromContext(String name) {
}

public Object getFromContext(String menuItemName, String valueName) {
Map<String, Object> valueMap = UtilGenerics.toMap(context.get(menuItemName));
Object obj = context.get(menuItemName);
Map<String, Object> valueMap = (obj instanceof Map) ? UtilGenerics.cast(obj) : null;
if (valueMap == null) {
valueMap = new HashMap<>();
context.put(menuItemName, valueMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,8 @@ public void renderMultiFormClose(Appendable writer, Map<String, Object> context,
@Override
public void renderFormatListWrapperOpen(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException {
Map<String, Object> inputFields = UtilGenerics.checkMap(context.get("requestParameters"));
Map<String, Object> queryStringMap = UtilGenerics.toMap(context.get("queryStringMap"));
Object obj = context.get("queryStringMap");
Map<String, Object> queryStringMap = (obj instanceof Map) ? UtilGenerics.cast(obj) : null;
if (UtilValidate.isNotEmpty(queryStringMap)) {
inputFields.putAll(queryStringMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,9 @@ protected void renderScreenletPaginateMenu(Appendable writer, Map<String, Object
}

RequestHandler rh = RequestHandler.from(request);
Object obj = context.get("requestParameters");

Map<String, Object> inputFields = UtilGenerics.toMap(context.get("requestParameters"));
Map<String, Object> inputFields = (obj instanceof Map) ? UtilGenerics.cast(obj) : null;
// strip out any multi form fields if the form is of type multi
if ("multi".equals(modelForm.getType())) {
inputFields = UtilHttp.removeMultiFormParameters(inputFields);
Expand Down

0 comments on commit 365b044

Please sign in to comment.