【发布时间】:2019-11-08 10:05:39
【问题描述】:
我想知道是否有一种方法可以删除以下代码的未经检查的警告,该代码提供了一个返回自身的流畅 API。
public interface Taggable<T> {
/**
* Should return the underlying set that holds the tags.
*
* @return set
*/
Set<String> getTags();
@SuppressWarnings("unchecked")
default T tag(@NotNull String... tags) {
for (String tag : tags) {
getTags().add(tag);
}
return (T) this;
}
@SuppressWarnings("unchecked")
default T untag(@NotNull String tag) {
getTags().remove(tag);
return (T) this;
}
}
用法是
@Data
public class MyObject implements Taggable<MyObject> {
private Set<String> tags;
}
MyObject t = new MyObject()
.tag("abc")
.tag("def");
带有抑制警告的工作示例https://repl.it/@trajano/fluent
【问题讨论】:
-
不,T是包含集合的类型
-
你只是想摆脱
@SuppressWarnings? -
@daniu 是的(如果可能的话)到目前为止,它看起来不是。