我正试图为Stash插件使用一段代码,但编译器一直给我一个我似乎无法解决的错误。它使用com.google.common.cache.cache(Guava)
static final RepositorySettings DEFAULT_SETTINGS = new RepositorySettings(0);
private final PluginSettings pluginSettings;
private final Cache<Integer, RepositorySettings> cache = CacheBuilder.newBuilder().build(
new CacheLoader<Integer, RepositorySettings>()
{
@Override
public RepositorySettings load(@Nonnull Integer repositoryId)
{
@SuppressWarnings("unchecked")
Map<String, String> data = (Map) pluginSettings.get(repositoryId.toString());
return data == null ? DEFAULT_SETTINGS : deserialize(data);
}
});
.build给了我以下错误
The method build(CacheLoader<? super Integer,RepositorySettings>) is ambiguous for the type CacheBuilder<Object,Object>
Cache有一个不带参数的build()
方法,而LoadingCache
有一个以CacheLoader
为参数的build()
方法。
private final LoadingCache<Integer, RepositorySettings> cache = CacheBuilder.newBuilder().build(
new CacheLoader<Integer, RepositorySettings>() {
@Override
public RepositorySettings load(@Nonnull Integer repositoryId) {
@SuppressWarnings("unchecked")
Map<String, String> data = (Map) pluginSettings.get(repositoryId.toString());
return data == null ? DEFAULT_SETTINGS : deserialize(data);
}
});
这应该行得通。
作为参考:http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/cache/CacheBuilder.html