I have the following code that i want to put into a method to be reused:
List<ClText150> names = attribute.getName().getClText150();
for (ClText150 clText : names) {
Locale locale = Locale.forLanguageTag(clText.getCl());
AttributeText attributeText = textsMap.get(locale);
if (attributeText == null) {
attributeText = createText(locale);
textsMap.put(locale, attributeText);
}
attributeText.setName(clText.getValue());
}
This i need for different lists of ClText150 (an object from a xml). Same code i need for
List<ClText150> shortNames = attribute.getShortname().getClText150();
But this time caling setShortname
with the value. The same i need for other fields.
Is there a way to parametrize this setter?
I created a method doing the job:
private void handle150(List<ClText150> list, Consumer<String> setter) {
for (ClText150 clText : list) {
Locale locale = Locale.forLanguageTag(clText.getCl());
AttributeText attributeText = textsMap.get(locale);
if (attributeText == null) {
attributeText = createText(locale);
}
setter.accept(clText.getValue());
//attributeText.setName(clText.getValue());
}
}
The problem is how to call this method declaring the setter of the object (attributeText
) that exists only in the method. This is obviously not working:
handle150(names, AttributeText::setName);
If not possible what alternatives i have to just copy paste the code?