Here is my problem. I have currently a function called by a route that works properly ! However, I would like to factorize the checking parameter block.
Let me explain. When the user enters the URL to reach the function, he can put some optional parameters (6 in total). At least one of these parameters is needed to continue. My framework is configured to assign a null value to parameters that haven't been informed by user.
To check which parameters have been informed, and verify them, I have a block :
public Result edit(String param1, String param2, String param3, String param4, String param5, String param6) {
Map<String, Object> parameters = new HashMap<>();
if (param1 != null) {
// Checking function depending on data type (URL, Boolean, ..), return a clean param or throw an InvalidParamException
// Variable param depends on type returned by checkParamType1
param = checkParamType1(param1);
parameters.put("param1", param);
}
if (param2 != null) {
param = checkParamType1(param2);
parameters.put("param2", param);
}
if (param3 != null) {
param = checkParamType2(param3);
parameters.put("param3", param);
}
if (param4 != null) {
param = checkParamType2(param4);
parameters.put("param4", param);
}
if (param5 != null) {
param = checkParamType3(param5);
parameters.put("param5", param);
}
if (param6 != null) {
param = checkParamType3(param6);
parameters.put("param6", param);
}
assert(parameters.size() > 0, "At least one parameter required");
// [TREATMENT]
}
My question is, in your opinion, is it possible to factorize this block ?
My project's running on JAVA 8.
Thank you :)
public Result edit(String... params) { for (String param : params) // and the rest of your logicOr you could even make a stream, filter it, map and collect - no if statments and no loops.