java - Applying a list of values as predicate using Collection Utils by the use of pedicates -
i want implement database systems in functionality using predicate.
this if sql filter recordset in cumbersome results.
but if pass list in predicate takes 1 value i.e. if passing 53 , 54 filter results 53 only.
public class classnamepredicate implements predicate<classname> { private object expected1; private string property; private list<object> listofvalues = new arraylist<object>(); public salesorderpredicate(object expected1, string property) { super(); this.expected1 = expected1; this.property = property; } public salesorderpredicate(list<object> listvalues, string property) { this.listofvalues = listvalues; this.property = property; } @override public boolean evaluate(salesorder object) { try { if (property.equals("volume")) { return ((integer) expected1 < object.getvolume()); } if (property.equals("startdateid")) { return (expected1.equals(object.getstartdateid())); } if (property.equals("enddateid")) { return (expected1.equals(object.getenddateid())); } if (property.equals("productids")) { (object value : listofvalues) { return (object.getproductid() == (integer) value); } } if (property.equals("sourceids")) { (object value : listofvalues) { return (object.getsourceid() == (integer) value); } } return false; } catch (exception e) { return false; } }
}
i trying use per following way:
list<object> productids = new arraylist<object>(); productids.add(53); productids.add(54); list<object> sourceids = new arraylist<object>(); sourceids.add(122); predicate[] classnameorderpredicate = { (predicate) new classnamepredicate(4415, "startdateid"), (predicate) new classnamepredicate(4443, "enddateid"), (predicate) new classnamepredicate(100000, "volume"), (predicate) new classnamepredicate(productids, "productids"), (predicate) new classnamepredicate(sourceids, "sourceids") }; predicate classnameallpredicategeneric = (predicate) predicateutils .allpredicate((org.apache.commons.collections4.predicate<? super classname>[]) classnamepredicate); collection<classname> classnamefilteredcollectiongeneric = genericcollectionutils.select(classname, classnameallpredicategeneric);
please suggest in design perspective too.
thanks in advance
you're evaluating first item in collection:
for (object value : listofvalues) { return (object.getproductid() == (integer) value); }
you want evaluate of them, , java conveniently provides contains()
method that:
return listofvalues.contains(object.getproductid());
other that, code looks pretty awful, should create smaller, targeted predicate
s, instead of writing generic 1 lots of different cases. rid of casts @ same time.
you failed @ obfuscation failing replace few salesorder
classname
(which doesn't respect java coding standard , distracting).
Comments
Post a Comment