番石榴功能组成

发布于 2024-12-26 05:55:47 字数 1337 浏览 0 评论 0原文

我看到有一个实用程序可以创建 Function,它是 FunctionFunction 的组合;

我有一个类似的情况,但有点不同。

我的第一个函数是 valueOfFunction,它根据我的 BO 类中的键返回一个枚举。

secind 函数使用参数调用 Enum 上的方法,该参数是 BO 对象。

所以它不完全是 A->B->C

这是函数:

private final static class RequestConvertor implements Function<CoreData, List<Request>> {
    private final static Function<String,RequestConvertorEnum> typeConvertor =  valueOfFunction(RequestConvertorEnum.class);

    @Override
    public List<Request> apply(CoreData coreData) {
        RequestConvertorEnum requestConvertorEnum = typeConvertor.apply(coreData.getType());
        return requestConvertorEnum.convertToRequests(coreData);
    }

}

这是枚举的方法:

   private final List<Request> convertToRequests(CoreData coreData) {
        List<PropertyWrapper> properties = getProperties(coreData);
        if (properties.size() == 0) {
            return Collections.emptyList();
        }
        Request request = new Request(coreData.getKey(), properties, new RequestMetaData(
                coreData.getFoo()));
        return newArrayList(request);
    }

有没有更好的方法将这两个函数组合在一起?

I saw that there is a utility to create Function<A,C>, which is a composition of Function<A,B> and Function<B,C>

I have a similar, but a little bit different case.

My first Function is valueOfFunction that returns an enum based on a key in my BO class.

The secind Function calls a method on the Enum with a parameter, which is the BO object.

So it's not exactly A->B->C

Here's the Function:

private final static class RequestConvertor implements Function<CoreData, List<Request>> {
    private final static Function<String,RequestConvertorEnum> typeConvertor =  valueOfFunction(RequestConvertorEnum.class);

    @Override
    public List<Request> apply(CoreData coreData) {
        RequestConvertorEnum requestConvertorEnum = typeConvertor.apply(coreData.getType());
        return requestConvertorEnum.convertToRequests(coreData);
    }

}

Here's the method of the enum:

   private final List<Request> convertToRequests(CoreData coreData) {
        List<PropertyWrapper> properties = getProperties(coreData);
        if (properties.size() == 0) {
            return Collections.emptyList();
        }
        Request request = new Request(coreData.getKey(), properties, new RequestMetaData(
                coreData.getFoo()));
        return newArrayList(request);
    }

Is there a nicer way to compose these two functions together?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城°AllureLove 2025-01-02 05:55:47

我认为 valueOfFunction 的使用首先是不合适的 - 您在调用 的另一个函数(它是 private static 成员)中使用它立即应用方法。您应该使用良好的旧 Enum.valueOf(String) 静态方法:

private final static class RequestConvertor 
        implements Function<CoreData, List<Request>> {
    @Override
    public List<Request> apply(CoreData coreData) {
        return RequestConvertorEnum.valueOf(coreData.getType())
                .convertToRequests(coreData);
    }
}

请注意,提供不代表任何枚举值的 String 会抛出 IllegalArgumentException,但在您当前的代码中,您在运行时有潜在的 NullPointerException如果 Enum 常量不存在,Enums.valueOfFunction 返回 null),我想您已经意识到了这一点(如果您没有意识到,那么您现在已经意识到了) :))。

此外,关于其余代码的建议很少。如果您不需要可变性,请使用 ImmutableLists 而不是 ArrayLists (我假设你不修改结果,因为 Collections.emptyList 本身是不可变的,所以如果您修改 convertToRequests 方法的结果,它将在运行时失败)。

private final ImmutableList<Request> convertToRequests(CoreData coreData) { // 1.
    List<PropertyWrapper> properties = getProperties(coreData);
    if (properties.size() == 0) {
        return ImmutableList.of(); // 2.
    }
    Request request = new Request(coreData.getKey(), properties, 
            new RequestMetaData(coreData.getFoo()));

    return ImmutableList.of(request); // 3.
}

一些解释:

  1. 使用 ImmutableList 作为返回类型来保证行为(不可变性)。
  2. 返回空的不可变列表,但比 JDK 的更好
  3. 与 2. 相同,但这里是单例不可变列表。

如果您需要可变性,请将 ImmutableList.of 替换为 Lists.newArrayList 并保持方法签名不变。

I think usage of valueOfFunction is inappropriate in first place - you are using it only in another function (it's private static member) invoking apply method immediately. You should use good old Enum.valueOf(String) static method:

private final static class RequestConvertor 
        implements Function<CoreData, List<Request>> {
    @Override
    public List<Request> apply(CoreData coreData) {
        return RequestConvertorEnum.valueOf(coreData.getType())
                .convertToRequests(coreData);
    }
}

Note that supplying String that doesn't represent any of enum values throws IllegalArgumentException, but in your present code you have potential NullPointerException at runtime (Enums.valueOfFunction returns null if the Enum constant does not exist) and I suppose you are aware of it (if you weren't, you are now :)).

Moreover few suggestions about rest of code. If you don't need mutability, use ImmutableLists instead of ArrayLists (I assume you don't modify result because Collections.emptyList is immutable itself so it will fail at runtime if you will modify result of convertToRequests method).

private final ImmutableList<Request> convertToRequests(CoreData coreData) { // 1.
    List<PropertyWrapper> properties = getProperties(coreData);
    if (properties.size() == 0) {
        return ImmutableList.of(); // 2.
    }
    Request request = new Request(coreData.getKey(), properties, 
            new RequestMetaData(coreData.getFoo()));

    return ImmutableList.of(request); // 3.
}

Some explanations:

  1. Use ImmutableList as return type to guarantee behavior (immutablility).
  2. Return empty immutable list, but better than JDK's one.
  3. Same as 2. but singleton immutable list here.

In case you need mutability, replace ImmutableList.of with Lists.newArrayList and leave method signature intact.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文