Java org.hamcrest.Matchers,用于同时匹配对象的不同属性
我试图通过org.hamcrest.Matchers匹配对象的两个不同属性。这是:Java org.hamcrest.Matchers,用于同时匹配对象的不同属性,java,hamcrest,lambdaj,Java,Hamcrest,Lambdaj,我试图通过org.hamcrest.Matchers匹配对象的两个不同属性。这是: List<LeaveApply> leaveApplyList = Lambda.select( allLeaveApplyList, Matchers.allOf( Lambda.having( Lambda.on(LeaveApply.class).getUser().getId(),
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)))
)
);
List 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(自日期起),
Matchers.lessThanOrEqualTo(toDate)))
)
);
它提供了一个LeaveApply对象列表,该对象的用户id等于给定id,日期小于或等于to date,大于或等于from date。它正在工作。我想知道这是匹配不同属性字段的正确方法吗?据我所知,应该可以。您可以做两个改进:使用静态导入使其更具可读性,并使用
having(…)。和(…)
而不是使用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)))));
导入静态ch.lambdaj.Lambda.*;
导入静态org.hamcrest.Matchers.*;
List LEVEAPPLYLIST=select(ALLEVEAPPLYLIST,具有(on(leveApply.class).getUser().getId(),equalTo(userId)))和(on(leveApply.class).getDate(),allOf(greaterThanOrEqualTo(fromDate),lessThanOrEqualTo(toDate '));