Selecting on nested list's object (person.dogList.dog.name)

68 views
Skip to first unread message

Mark Richards

unread,
Oct 31, 2012, 6:14:43 PM10/31/12
to lam...@googlegroups.com
Hi,
 This is a great piece of software, I'm loving it. Along with Hamcrest, it makes my life all the more easier.

 I've been breaking my head over something..I would really appreciate any help I can get.

Say I have this setup.

class Person {
    private List<Dog> dogList;
}
   
class Dog {
    private String name;
    private Integer age;
}


Person1 [Dog(Jono, 10)]
Person2 [Dog(Whatever, 10)]
Person3 [Dog(Whatever, 20)]

I want to find all person objects who have a dog with name "Jono" (which should return just Person1). Not sure how to code the "on" clause for it? I could implement the equals method for Dog class comparing just the names, but that wouldn't work if I have to match by Dog's age later. I can't seem to find similar examples in the Lambdaj Wiki.

on(Person.class).getDogList()????. Any thoughts on how to make this work?

Thanks

Mark Richards

unread,
Oct 31, 2012, 8:35:56 PM10/31/12
to lam...@googlegroups.com

I mean I'm trying to do something like
select(personList, having(on(Person.class).getDogList()????, hamcrestMatcher)

Mark Richards

unread,
Nov 4, 2012, 4:36:42 PM11/4/12
to lam...@googlegroups.com
Thanks Matt. Works beautiful, can't thank you enough :-)

On Friday, November 2, 2012 8:51:00 AM UTC+13, Matt Painter wrote:
The key is realising that having() itself returns a Matcher and can therefore be easily nested as Hamcrest allows.
Reply all
Reply to author
Forward
0 new messages