如何以函数式方式处理这个查询?

huangapple go评论73阅读模式
英文:

How to handle this query in functional way?

问题

//如何以函数式的方式处理下面的代码,例如收集所有无效的排序参数,准备包含所有这些参数的消息,最后抛出InvalidSortParam异常。

你认为下面的查询代码可以实现吗

public Sort resolveArgument() {

    Sort sort = sortHandlerMethodArgumentResolver.resolveArgument();

    List<Sort.Order> orders = sort.stream().collect(Collectors.toList());
    List<String> invalidSortList = orders.stream().map(Sort.Order::getProperty)
            .filter(property -> !allowedSortParams.contains(property))
            .collect(Collectors.toList());

    if (orders.isEmpty()) {
        sort = Sort.by(Sort.Direction.DESC, defaultSortParam);
    } else {
        if (orders.size() > sortMaxCount) {
            throw new InvalidSortException(INVALID_SIZE_PARAMS);
        } else {
            if (!invalidSortList.isEmpty()) {
                throw new InvalidSortException(invalidSortList.stream()
                        .collect(Collectors.joining(",")) + INVALID_SORT_PARAMS);
            }
        }
    }

    return sort;
}
英文:

//How can we handle the below code in functional way like collect all invalid sort params, prepare message with all them listed and finally throw InvalidSortParam exception.

DO you think the below query does it?

public Sort resolveArgument() {

    Sort sort = sortHandlerMethodArgumentResolver.resolveArgument();

    List&lt;Sort.Order&gt; orders = sort.stream().collect(Collectors.toList());
    List&lt;String&gt; invalidSortList = orders.stream().map(Sort.Order::getProperty)
            .filter(property -&gt; !allowedSortParams.contains(property))
            .collect(Collectors.toList());

    if (orders.isEmpty()) {
        sort = Sort.by(Sort.Direction.DESC, defaultSortParam);
    } else {
        if (orders.size() &gt; sortMaxCount) {
            throw new InvalidSortException(INVALID_SIZE_PARAMS);
        } else {
            if (!invalidSortList.isEmpty()) {
                throw new InvalidSortException(invalidSortList.stream()
                        .collect(Collectors.joining(&quot;,&quot;)) + INVALID_SORT_PARAMS);
            }
        }
    }

    return sort;
}

答案1

得分: 1

首先,您没有充分利用流式功能,将其分为两个独立的步骤 - 加载数据,以及在完成加载后进行转换。您可以将其合并为一个单一的流水线。其次,抛出异常并不是一种函数式的方式。您应该使用某种类型来返回状态,例如使用一些Try实现。

final class Try<T> {

    private final Exception e;
    private final T t;

    public Try(Exception e) {
        this.e = e;
        t = null;
    }

    public Try(T t) {
        this.t = t;
        e = null;
    }

    public Exception getE() {
        return e;
    }

    public T getT() {
        return t;
    }
}

代码本身可以像这样:

public Try<Sort> resolveArgument()  {
    int cnt = getCount();
    return cnt == 0 ? Sort.by(Sort.Direction.DESC, defaultSortParam) : someErrorHandlingLogic(cnt);
}

private int getCount() {
    return (int) sort.stream().map(Sort.Order::getProperty)
            .filter(property -> !allowedSortParams.contains(property))
            .count();
}

如果您对Java中的函数式编程范 paradigm 感兴趣,我推荐这个优秀的演示:https://dev.tube/video/YnzisJh-ZNI

英文:

First of all - you do not take advantage of the streaming functionality, you divided it into two separate steps - loading data, and when it is completed - transforming it. You could merge it into a single pipeline. Secondly - throwing exceptions is not a functional way. You should use some type to return a state e.g with some Try implementation.

final class Try&lt;T&gt; {

    private final Exception e;
    private final T t;

    public Try(Exception e) {
        this.e = e;
        t = null;
    }

    public Try(T t) {
        this.t = t;
        e = null;
    }

    public Exception getE() {
        return e;
    }

    public T getT() {
        return t;
    }
}

and the code itself could look something like

public Try&lt;Sort&gt; resolveArgument()  {
    int cnt = getCount();
    return cnt == 0 ? Sort.by(Sort.Direction.DESC, defaultSortParam) : someErrorHandlingLogic(cnt);

}

private static void getCount() {
    sort.stream().map(Sort.Order::getProperty)
            .filter(property -&gt; !allowedSortParams.contains(property))
            .count();
}

If you're interested in the Functional Programming paradigm in java - I recommend great presentation https://dev.tube/video/YnzisJh-ZNI

huangapple
  • 本文由 发表于 2020年5月29日 13:23:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/62079220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定