【发布时间】:2019-09-27 17:31:58
【问题描述】:
我有一个简单的 Book 类,其中包含类似
private String ISBN;
private String title;
private String author;
我想创建搜索查询,它将BookDto 作为条件,并将所有不可为空的字段与我的List<Book> 的元素进行比较。所以我写了几个简单的Predicates
private Predicate<Book> matchingAuthor(Book another) {
return book -> book.getAuthor() != null && book.getAuthor().equals(another.getAuthor());
}
private Predicate<Book> matchingTitle(Book another) {
return book -> book.getTitle() != null && book.getTitle().equals(another.getTitle());
}
private Predicate<Book> matchingISBN(Book another) {
return book -> book.getISBN() != null && book.getISBN().equals(another.getISBN());
}
我希望有 1 种搜索方法来处理所有逻辑
private List<BookDto> findMatchingBooks(BookDto criteria) {
return books.stream().map(BookConverter::toEntity).filter(this::matchingBook).map(BookConverter::toDto).collect(Collectors.toList());
}
但是这个逻辑很丑……而且它不会像我想要的那样工作。
private Predicate<Book> matchingBook(Book criteria) {
if(criteria.getISBN() != null) {
return matchingISBN(criteria);
}
else if(criteria.getISBN() == null && criteria.getTitle() == null && criteria.getAuthor() != null) {
return matchingAuthor(criteria);
}
else if(criteria.getISBN() == null && criteria.getTitle() != null && criteria.getAuthor() != null) {
return matchingAuthor(criteria) && matchingTitle(criteria);
}
}
前两个if/else 可以说是好的(丑陋但有效),第三个是导致
二元运算符'&&'的错误操作数类型 第一种:谓词 第二种:谓词
问题是,我怎样才能做到这一点?
【问题讨论】:
-
如何在
Book类本身中添加一些布尔方法,例如boolean hasSameAuthor(Book other)? -
旁白:不清楚
return books.stream().map(BookConverter::toEntity).filter(this::matchingBook).map(BookConverter::toDto).collect(Collectors.toList());正在执行什么。filter(this::matchingBook)如何处理当前签名?另请注意matchingBook方法中缺少返回。可以默认为真为return book -> true; -
只是一种预感,您可能正在寻找一个简单的链式或条件来逐个匹配属性,否则返回 false。像:
matchingISBN(criteria).or(matchingAuthor(criteria)).or(matchingTitle(criteria)); -
你的条件是多余的。当您使用
if(criteria.getISBN() != null) { …}时,您不需要else if(criteria.getISBN() == null …,因为此时它必须是null。这同样适用于其他条件。此方法甚至无法编译,因为编译器无法识别冗余,并会说最后必须有另一个return。只需使用return criteria.getISBN() != null? matchingISBN(criteria): criteria.getTitle() == null? matchingAuthor(criteria): matchingAuthor(criteria).and(matchingTitle(criteria));
标签: java java-stream predicate