Applying a list of values as predicate using Collection Utils by the use of pedicates

StackOverflow https://stackoverflow.com/questions/22297064

  •  12-06-2023
  •  | 
  •  

سؤال

I want to implement Database systems in functionality by using the predicate.

This is as like if SQL filter a recordset by in it cumbersome the results.

But if i pass the List as in predicate it takes only one value i.e. if i am passing 53 and 54 it filter the results for 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")) {
            for (Object value : listOfValues) {
                return (object.getProductId() == (Integer) value);
            }
        }
        if (property.equals("sourceIds")) {
            for (Object value : listOfValues) {
                return (object.getSourceId() == (Integer) value);
            }
        }
        return false;
    } catch (Exception e) {
        return false;
    }
}

}

I am trying to use this as per the 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 only evaluating the first item in the collection:

for (Object value : listOfValues) {
    return (object.getProductId() == (Integer) value);
}

You want to evaluate all of them, and Java conveniently provides a contains() method for that:

return listOfValues.contains(object.getProductId());

Other than that, the code looks pretty awful, you should create smaller, targeted Predicates, instead of writing a generic one with lots of different cases. You could get rid of those casts at the same time.

You also failed at your obfuscation by failing to replace a few SalesOrder by className (which doesn't respect the Java coding standard and is distracting).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top