org.hamcrest.Matchers 用于同时匹配对象的不同属性
我试图通过 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 对象的列表,其中用户 ID 等于给定 ID,日期小于或等于当前日期且大于或等于起始日期。它正在发挥作用。我想知道这是匹配不同属性字段的正确方法吗?
I am trying to match two different properties of an Object by org.hamcrest.Matchers. Here it is:
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)))
)
);
It gives a List of LeaveApply Object having user-id equals to given id and date less than or equals to to-date and greater than or equals to from-date. It is working. I want to know is it the right way to match different property fields?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,它应该有效。您可以进行两项改进:使用静态导入使其更具可读性,并使用
having(...).and(...)
而不是使用allOf
:It should work, as far as I see. You can do two improvements: use static imports to make it more readable and use
having(...).and(...)
instead of usingallOf
: