我试图通过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对象的列表,该列表的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)))));

10-08 16:01