org.hamcrest.Matchers 用于同时匹配对象的不同属性

发布于 2024-11-02 04:07:20 字数 680 浏览 2 评论 0原文

我试图通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

据我所知,它应该有效。您可以进行两项改进:使用静态导入使其更具可读性,并使用 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)))));

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 using 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)))));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文