简体   繁体   English

如何在.filter体中简化巨大的if语句?

[英]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: 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: filter的类型为LocationFilter,包含:

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 首先是if语句:如果为客户端ID设置了过滤器 - >不包含其关联客户端没有此id的任何LocationVO

Second if statement: If a filter was set for location -> remove/filter all LocationVO that do not have this id 第二个if语句:如果为位置设置了过滤器 - >删除/过滤所有没有此ID的LocationVO

Third if statement: Filter all VOs that do not have the location_type of the filter. 第三个if语句:过滤所有没有过滤器的location_type的VO。

(( I think, none are obsolete (( as mentioned in the comments)) )) ((我认为,没有一个是过时的((如评论中所述))))

You can build up the Predicate<LocationVO in stages: 您可以分阶段构建Predicate<LocationVO

    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: 现在,如果将谓词构建移动到filter类中,它看起来更好:

    // 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 : 您可以将此类逻辑移动到对象本身内的equals (以及hashCode )实现,然后再使用更简单的流管道:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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