I have the following methods in CacheUtil class
public void addCacheEntry(String key, Serializable entry) {
//add to cache
}
public void removeCacheEntry(String key) {
//remove from cache
}
public Serializable getCacheEntry(String key) {
Serializable entry = // get from cache
return entry;
}
And in the class where I use the cache, I have the following methods,
public List<String> fetchList() {
List<String> aList = (List<String>) CacheUtil.getInstance().getCacheEntry("aList"); //IDE warns of unchecked casting
if(aList == null) {
aList = fetchFromDB():
CacheUtil.getInstance().addCacheEntry("aList", new ArrayList<String>(aList));
}
}
How could I have avoided the warning of unchecked cast? I can not change the methods in cache to store lists explicitly, as I want to store anything that is serializable in cache and that is just a library.
I believe there is a better way to do it.