Closed thisiscam closed 4 years ago
Workaround is to use model adaptor, but native support would be better
template.registerModelAdaptor(Optional.class, new ObjectModelAdaptor() {
@Override
public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) throws STNoSuchPropertyException {
Optional optional = (Optional) o;
switch (propertyName) {
case "get":
return optional.get();
case "present":
return optional.isPresent();
}
return super.getProperty(interp, self, o, property, propertyName);
}
});
I don't know how feasible this is due to the project in general being limited to Java 6. Perhaps the requirement should be raised to Java 8 now that 6 and 7 are EOL.
I'm worried about how this would change the semantics...and this is no longer my focus so I don't have the time to evaluate this, I'm afraid.
Java 8 introduces Optional which is more idiomatic than using null. Yet the retriever of Optional is named "get", and there seems to be no way in stringtemplate to retrieve the inner value.