A nullability question
Eirik Lygre <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Hi,
For one of our FreeMarker scenarios, we use a model object with a signature
like this:
class MyNumberModel implements TemplateHashModelEx, TemplateNumberModel {
...
public Number getAsNumber() {
return functionThatReturnsActualNumber();
}
}
In general, the HashModel returns lots of metadata (like datatype, length,
name, origin, etc), while the NumberModel returns the actual value. My
question is how to best handle null values, to that "${field!0}" works properly.
I was hoping that having getAsNumber() return "null" would be the proper
thing to do: Then, we can reuse the model for all numeric types, regardless
of their actual value, and still access the metadata even when the value is
null. This doesn't seem to be the case, though: Rather, if
functionThatReturnsActualNumber() returns null, we get this stack trace:
freemarker.template.TemplateException: value!0 evaluated to null number.
at freemarker.core.EvaluationUtil.getNumber(EvaluationUtil.java:117)
...
The alternative way is to actually have two different models, and instantiate
them as required. This is shown below, but it complicates our code quite a
bit, since we can no longer just instantiate the model object based on our
underlying data type, but also must care for the underlying *value*:
class MyNullNumberModel implements TemplateHashModelEx {
...
}
class MyNotNullNumberModel extends MyNullNumberModel
implements TemplateNumberModel {
...
public Number getAsNumber() {
return functionThatReturnsActualNumber();
}
}
So, the questions are:
1) Currently, will I have to do the two-different-model-classes approach,
or am I missing something?
2) In the future, will this change & become simpler?
Eirik
------------------------------------------------------------------------------
vRanger cuts backup time in half-while increasing security.
With the market-leading solution for virtual backup and recovery,
you get blazing-fast, flexible, and affordable data protection.
Download your free trial now.
http://p.sf.net/sfu/quest-d2dcopy1