Filtering list on multiple conditions based on multiple properties in Java

I'm using Google guava 17.0. I have a class Employee containing only two properties namely, id of type Integer and salary of type BigDecimal.

I'm trying to filter based on these two properties as follows.

List<Employee>entireList=null; //This list is already populated somehow by a list of employee objects.

entireList = FluentIterable.from(entireList).filter(new Predicate<Employee>() {
    @Override
    public boolean apply(Employee employee) {
        if(filters.containsKey("id")&&filters.get("id") instanceof Integer&&filters.containsKey("salary")&&filters.get("salary") instanceof BigDecimal)
        {
            return ((Integer)filters.get("id")).equals(employee.getId())&&((BigDecimal)filters.get("salary")).compareTo(employee.getSalary())>=0;
        }

        if(filters.containsKey("id")&&filters.get("id") instanceof Integer)
        {
            return ((Integer)filters.get("id")).equals(employee.getId());
        }

        if(filters.containsKey("salary")&&filters.get("salary") instanceof BigDecimal)
        {
            return ((BigDecimal)filters.get("salary")).compareTo(employee.getSalary())>=0;
        }
        return false;
    }
}).toList();

filters is a Map<String, Object>. When this map contains both keys id and salary, the first condition in the apply() method is evaluated to true and accordingly, it should filter based on both the properties. It however, filters the list based on only on one of them.

So how to apply filtering on multiple properties here?

Answers


That's pretty perfectly obfuscated. So that it even may work without anyone noticing.

Let's go through:

 filters.containsKey("id")

Useless, drop it.

filters.get("id") instanceof Integer

Better throw if someone has put something else in. So drop it, too.

return ((Integer)filters.get("id")).equals(employee.getId())&&((BigDecimal)filters.get("salary")).compareTo(employee.getSalary())>=0;

What would you do with 5 or 10 conditions? Combinatorial explosion?

So what about this?

Integer id = (Integer) filters.get("id");
if (id != null && !id.equals(employee.getId())) return false;

BigDecimal salary = (BigDecimal) filters.get("salary");
if (salary != null && ((salary.compareTo(employee.getSalary()) < 0)) return false;

return true;

Need Your Help

iOS code drawings vs png

ios iphone image cocoa-touch svg

I've found a program for converting SVG images directly into code. Sounds like an epic solution for iOS application size problems but here occurs some obstacles.

Configuring SpringSource Tool Suite wth AspectJ

java eclipse aspectj sts-springsourcetoolsuite

How can I configure the SpringSource Tool Suite(ver 2.3.0) Spring project to accept .aj (AspectJ) file?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.