org.hamcrest.Matchers for matching different properties simultaneously of an Object
Asked Answered
S

1

5

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?

Shaftesbury answered 15/4, 2011 at 4:39 Comment(0)
L
8

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)))));
Lobby answered 15/4, 2011 at 6:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.