我不得不删除其中的一些细节,但基本上我是在尝试缓存一个昂贵的操作的结果,该操作返回一个对象集合,但这些单个对象的类型在编译时是未知的(仅是基类)。
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;
}
}
// ...
}这里的缓存检索是一个未经检查的强制转换,因为代码只是相信调用者传递给方法的类参数与最初在缓存中创建对象的调用传递的类参数是兼容的(应该是相同的)。
是否有某种方法或某种设计模式可以将实际的类与对象本身一起缓存,从而避免这种未经检查的强制转换?
发布于 2010-11-22 00:49:48
没有对这种行为的直接支持。
如果缓存包含单个元素,则可以使用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));发布于 2010-11-22 00:49:35
有许多可能的方法来处理这个问题。下面是我要做的-创建一个Collection类来存储它的元素类型:
class TypedList<T> extends ArrayList<T> {
private Class<T> type;
TypedList(Class<T> type) {
super();
this.type = type;
}
Class<T> getType() {
return type;
}
}并确保someExpensiveOperation()使用该类返回其结果。这样,您就可以在检索缓存项时查询该类型。
https://stackoverflow.com/questions/4238868
复制相似问题