// outdated code
void setCacheValue(String name, Object value){
getServletContext().setAttribute(name, value);
}
Object getCacheValue(String name){
return getServletContext().getAttribute(name);
}
// therefore I want to use generic for "type security"
<T> void setCacheObject(String name, T value){
getServletContext().setAttribute(name, value);
}
//, then there is a problem
<T> T getCacheValue(String name){
Object value = getServletContext().getAttribute(name);
return (T) value;
}
// This is what I need to do in my clean code:
{
double x = 1.2;
setCacheValue("x", x);
Double value = getCacheValue("x");
}
So what is the right way to do this?
source
share