我试图通过org.hamcrest.Matchers匹配对象的两个不同属性。
List<LeaveApply> leaveApplyList = Lambda.select(
allLeaveApplyList,
Matchers.allOf(
Lambda.having(
Lambda.on(LeaveApply.class).getUser().getId(),
Matchers.equalTo(userId)),
Lambda.having(
Lambda.on(LeaveApply.class).getDate(),
Matchers.allOf(
Matchers.greaterThanOrEqualTo(fromDate),
Matchers.lessThanOrEqualTo(toDate)))
)
);
它给出了一个LeaveApply Object的List,它的user-id等于给定的id, date小于等于to-date,大于等于from-date。它正在起作用。我想知道这是匹配不同属性字段的正确方法吗?
它应该工作,就我所见。您可以做两个改进:使用静态导入使其更具可读性;使用having(...).and(...)
而不是使用allOf
:
import static ch.lambdaj.Lambda.*;
import static org.hamcrest.Matchers.*;
List<LeaveApply> leaveApplyList = select(allLeaveApplyList, having(on(LeaveApply.class).getUser().getId(), equalTo(userId)).and(on(LeaveApply.class).getDate(), allOf(greaterThanOrEqualTo(fromDate), lessThanOrEqualTo(toDate)))));