我 Collection 了 Predicates
,说 List<Predicate<File>>
.然后我有一个 File
我需要获取与文件匹配的谓词(如果有的话)。我在考虑使用 Iterables.find()
但当然需要 Predicate
不是传递给 Predicate
的值.我考虑过实现以下但不知道是否已经存在一种机制。
public static <T> Predicate<Predicate<? super T>> createInversePredicate(
final T value) {
return new Predicate<Predicate<? super T>>() {
@Override
public boolean apply(Predicate<? super T> input) {
return input.apply(value);
}
};
}
这将允许我执行以下操作:
private List<Predicate<File>> filters = ...;
@Nullable
Predicate<File> findMatching(File file){
return Iterables.find(filters, createInversePredicate(file), null);
}
有没有更好的办法?
最佳答案
这里是 Guava 团队成员。
我就是这样做的。没有更好的方法。
关于java - 找到匹配的谓词。谓词的谓词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10417075/