gpt4 book ai didi

java - CacheBuilder.newBuilder().build 不明确

转载 作者:行者123 更新时间:2023-12-01 11:44:25 25 4
gpt4 key购买 nike

我正在尝试使用 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 另一方面有一个 build() 方法,它需要 CacheLoader 作为参数。

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

关于java - CacheBuilder.newBuilder().build 不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29266888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com