简体   繁体   中英

How to simplify huge if-statement in .filter body?

Is there a better meaning more readable way to express this?

    List<LocationVO> listLocation = listLocationAll.stream().filter(l -> {
        boolean ok = true;
        if ( filter.getClient_id() != null && filter.getClient_id().longValue() !=  l.getParent_client_id() ) {
            ok = false;
        }
        if ( filter.getLocation_id() != null && filter.getLocation_id().longValue() !=  l.getLocation_id() ) {
            ok = false;
        }
        if (filter.getLocation_type() != null && (filter.getLocation_type().equals(l.getLocation_type()) == false) ) {
            ok = false;
        }
        return ok;
    }).collect(Collectors.toList());

LocationVO cotains:

public class LocationVO implements Serializable {

    private static final long serialVersionUID = 1L;

    private long location_id;
    private long parent_client_id;
    private String name;
    private String location_type;
    ...
}

filter is of type LocationFilter and contains:

public class LocationFilter implements Serializable {

    private Long client_id;
    private Long location_id;
    private String location_type;
}

First if statement: If a filter was set for the client id -> do not contain any LocationVO whose associated client does not have this id

Second if statement: If a filter was set for location -> remove/filter all LocationVO that do not have this id

Third if statement: Filter all VOs that do not have the location_type of the filter.

(( I think, none are obsolete (( as mentioned in the comments)) ))

You can build up the Predicate<LocationVO in stages:

    Predicate<LocationVO> p = l -> true;
    if (filter.getClient_id() != null) {
        p = p.and(l -> filter.getClient_id().longValue() !=  l.getParent_client_id());
    }
    if (filter.getLocation_id() != null) {
        p = p.and(l -> l.getLocation_id().longValue() == filter.getLocation_id());
    }
    if (filter.getLocation_type() != null) {
        p = p.and(l -> filter.getLocation_type().equals(l.getLocation_type()));
    }

and then use the built predicate to filter the stream:

    List<LocationVO> listLocation = listLocationAll.stream()
            .filter(p)
            .collect(Collectors.toList());

Now, if you move the predicate building into the filter s class, it looks even nicer:

    // within the class of "filter"
    Predicate<LocationVO> createLocationVOPredicate() {
        Predicate<LocationVO> p = l -> true;
        if (getClient_id() != null) {
            p = p.and(l -> getClient_id().longValue() ==  l.getParent_client_id());
        }
        if (getLocation_id() != null) {
            p = p.and(l -> l.getLocation_id().longValue() == getLocation_id());
        }
        if (getLocation_type() != null) {
            p = p.and(l -> getLocation_type().equals(l.getLocation_type()));
        }
        return p;
    }

and the usage:

    listLocation = listLocationAll.stream()
            .filter(filter.createLocationVOPredicate())
            .collect(Collectors.toList());

Assuming that the rest of the logic would have a sequential check of each attribute to add to the filtering condition. You can move such logic to equals (along with hashCode ) implementation within the object itself and then again use a simpler stream pipeline as :

List<LocationVO> filterList(List<LocationVO> input, LocationVO elem) {
    return input.stream()
        .filter(o -> elem.equals(o))
        .collect(Collectors.toList());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM