I know hashtable doesnt allow null keys …but how is the below code working.
And what does initializing the Big Decimal to -99 in the below code do.
private static final BigDecimal NO_REGION = new BigDecimal (-99);
public List getAllParameters (BigDecimal region, String key) {
List values = null;
if (region==null) {
region = NO_REGION;
}
Hashtable paramCache = (Hashtable)CacheManager.getInstance().get(ParameterCodeConstants.PARAMETER_CACHE);
if (paramCache.containsKey(region)) {
values = (List) ((Hashtable)paramCache.get(region)).get(key);
}
return values;
}
Am struggling for a long time and dont understand it.
This is an implementation of the null object pattern: a special object,
BigDecimal(-99), is designated to play the role ofnullin a situation where “real”nulls are not allowed.The only requirement is that the null object must be different from all “regular” objects. This way, the next time the program needs to find entries with no region, all it needs to do is a lookup by the
NO_REGIONkey.