简体   繁体   English

Java:使用谓词分解[暂停]

[英]Java : Factorization using predicate [on hold]

Can we factor one more this code:我们能否再考虑一下这段代码:

protected Comparator<Personne> getComparator() {
    Comparator<Personne> comparator1 = LambdaUtil.wrapComparator(
                     personne -> personne != null && personne.Y() != null ?
                     APIPersonne.getAliasedName(personne.Y().getX()) : 
                     null, 
                String.CASE_INSENSITIVE_ORDER);

    Comparator<Personne> comparator2 = LambdaUtil.wrapComparator(
                     personne -> personne != null && personne .getY() != null ? 
                     APIPersonne.getAliasedName(personne.getY()) : 
                     null, 
                String.CASE_INSENSITIVE_ORDER);

    Comparator<Personne> comparator3 = LambdaUtil.wrapComparator(
                APIPersonne::getZ(), String.CASE_INSENSITIVE_ORDER);

    return comparator1.thenComparing(comparator2).thenComparing(comparator3);
}

Knowing that getY() and getX throw exceptions.知道getY()getX抛出异常。

If you meant using Predicate specifically, you can abstract that out as:如果您的意思是专门使用 Predicate,您可以将其抽象为:

Predicate<Personne> predicate = personne -> personne != null && personne.Y() != null;

and further use it as:并进一步将其用作:

Comparator<Personne> comparator1 = LambdaUtil.wrapComparator(
            personne -> predicate.test(personne) ? APIPersonne.getAliasedName(personne.Y().getX()) : null,
            String.CASE_INSENSITIVE_ORDER);

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

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