Value conversion using ValueMap vs standard java methods
I was looking into aem-core-wcm-components GitHub repo and noticed the different patterns for converting string to integer or boolean. Below is the code snippet from BreadcrumpImpl.java.
@ScriptVariable
private ValueMap properties;
private boolean showHidden;
private int startLevel;
@PostConstruct
private void initModel() {
startLevel = properties.get(PN_START_LEVEL, currentStyle.get(PN_START_LEVEL, PROP_START_LEVEL_DEFAULT));
showHidden = properties.get(PN_SHOW_HIDDEN, currentStyle.get(PN_SHOW_HIDDEN, PROP_SHOW_HIDDEN_DEFAULT));
}
Another thing is, the properties have not been directly mapped and have been accessed through "private ValueMap properties".
Link to original code: https://github.com/adobe/aem-core-wcm-components/blob/development/bundles/core/src/main/java/com/adobe/cq/wcm/core/components/internal/models/v1/BreadcrumbImpl.java#:~:text=%40ScriptVariable,%7D
This pattern has been used in other components too, e.g. List
Usually, what we do is that we map the properties using @ValueMapValue annotation and then use the standard java methods to convert those values into an int or boolean like below.
@ValueMapValue
private string startLevel;
private int startingLevel;
@PostConstruct
private void initModel() {
startingLevel = Integer.parseInt(startLevel);
}
public int getStartingLevel() {
return startingLevel;
}
The disadvantage here is that we have to create two different variables for the same thing. Please ignore null check or any typo. This is just an example.My question is, what are the best practices here? What is the advantage of using @ScriptVariable ValueMap properties over the standards Java methods for the conversion? Is it recommended to use @ScriptVariable ValueMap properties when you have a checkbox or numberfield which are of type boolean and int respective in your author dialog for conversion?
Additionally, as in the second code snippet, I have converted the value into int and then have a getter method to return that converted value to be used in HTL code. Do you think there is a need for conversion here, or you can return the value as a string, and then Sightly engine will take care of the conversion?