Reputation: 49
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 :)
Upvotes: 3
Views: 810
Reputation: 298838
The best you can do IMHO is to extract a method and use a method reference for your custom checker. Something like this:
private void checkAndAssign(Map<String, Object> map, String paramName, String paramValue, Function<String, Object> checker){
if(paramValue!=null){
Object param = checker.apply(paramValue);
map.put(paramName, param);
}
}
public Result edit(String param1, String param2, String param3, String param4, String param5, String param6) {
Map<String, Object> parameters = new HashMap<>();
checkAndAssign(map, "param1", param1, MyClass::checkParam1);
checkAndAssign(map, "param2", param2, MyClass::checkParam2);
checkAndAssign(map, "param3", param3, MyClass::checkParam3);
checkAndAssign(map, "param4", param4, MyClass::checkParam4);
checkAndAssign(map, "param5", param5, MyClass::checkParam5);
checkAndAssign(map, "param6", param6, MyClass::checkParam6);
assert(parameters.size() > 0, "At least one parameter required");
// [TREATMENT]
}
MyClass::checkParam1 is a method reference. The compiler will allow you to substitute such a reference for a functional interface like Function
.
See Method References in the Java 8 Tutorial.
Upvotes: 6
Reputation: 5647
If the number of arguments isn't limited, try using a var-args parameter and loop over the resulting array.
public Result edit(String... params) {
Map<String, Object> parameters = new HashMap<>();
IntStream.range(0, params.length).forEach(idx -> {
String param = params[idx];
if (param != null) {
Object p = checkParamType(param);
parameters.put("param" + idx, p);
}
});
}
Upvotes: 0
Reputation: 44965
In Java 8 you could replace
if (param1 != null) {
param = checkParamType1(param1);
parameters.put("param1", param);
}
With something like:
Optional.ofNullable(param1)
.ifPresent((param) -> parameters.put("param1", checkParamType1(param)));
Upvotes: 2
Reputation: 43
At first, you could use switch case statements. But in OO languages it would be better to use a design pattern like chain of responsibility...
Upvotes: 0
Reputation: 188
java 8 introduced a new feature to avoid null checks. Have a look: http://winterbe.com/posts/2015/03/15/avoid-null-checks-in-java/
Upvotes: -4