apache-commonsapache-commons-beanutils

BeanUtils filter Set based on object property and value compare


I have a Set of Objects whose exact type i dont know upfront . Lets assume the Set has 4 objects A , B , C and D.

class A{
    id = "test";
    order = "ship";
    getId();
    getOrder();
},
class B {
    id = "fail";
    order = "ship";
    getId();
    getOrder();
},class C {
    id = "fail";
    order = "ship";
    getId();
    getOrder();
},class D {
    id = "test";
    order = "ship";
    getId();
    getOrder();
},

I have an of Array of Property objects where Property looks like this :

class Property {
    propName : "id"; //Will never be null.
    propValueMatch : "test"; 
},
class Property {
    propName : "order"; //Will never be null.
    propValueMatch : null; 
}

I need to iterate over the set , apply the Property array logic to each object in the set and return the object that qualifies. It would go like this :

Check if the Object in Set has a readable property by the name : Property.propName. If TRUE { 
if Property.propValueMatch is not null : Check if that property value of the Object has the same value as defined in Property.propValueMatch. Then the object passess the criteria and should be returned.
if Property.propValueMatch is null : Then the object passess the criteria and should be returned
}

One catch is that the property could exist in an object variable inside the Object we are iterating over.

I started to implement this but i am sure there is a cleaner way using BeanUtils, PropertyUtils, Comaparator etc to do this.


Solution

  • Ended up using apache commons Predicate to create criteria to filter the set and then BeanWrapper to populate my specific fields