必须删除一些细节,但是基本上,我试图缓存返回对象集合的昂贵操作的结果,但是这些单个对象的类型在编译时未知(只有基类) )。
public class SomeClass
{
private static final Map<Integer,Collection<? extends SomeBaseClass>> theCache = new HashMap<Integer,Collection<? extends SomeBaseClass>>();
public <T extends SomeBaseClass> Collection<T> theMethod(Class<T> theClass, int index)
{
if (theCache.containsKey(index))
{
return (Collection<T>) theCache.get(index);
}
else
{
Collection<T> result = someExpensiveOperation(theClass, index);
theCache.put(index, result);
return result;
}
}
// ...
}
在这里,缓存检索是未经检查的强制转换,因为代码只是相信调用者传递给方法的类参数与先前调用所传递的类参数兼容(应该相同),而先前调用是在缓存中创建对象的首先。
是否有某种方法或某种设计模式可以将实际的类与对象本身一起缓存,以便可以避免这种未经检查的转换?
没有这种行为的直接支持。
如果您的缓存包含单个元素,则可以使用Class.cast()
,ClassCastException
如果不匹配,则抛出:
private Map<Integer, ?> cache = ...;
public <T> T get(Integer id, Class<T> c) {
return c.cast(cache.get(id));
}
对于缓存的集合,它将更加复杂。如果您实际上想避免未经检查的类型转换,则可以创建一个新集合,并通过Class.cast()
以下方法进行填充:
Collection<T> result = ...;
for (Object o: theCache.get(index))
result.add(theClass.cast(o));
return result;
其他方法包括,例如,使用Guava的创建“检查”集合视图Collections2.transform()
:
public class Cast<T> implements Function<Object, T> {
private Class<T> type;
public Cast(Class<T> type) {
this.type = type;
}
public T apply(Object in) {
return type.cast(in);
}
}
。
return Collections2.transform(theCache.get(index), new Cast<T>(theClass));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句