番石榴功能组成
我看到有一个实用程序可以创建 Function
,它是 Function
和 Function 的组合;
我有一个类似的情况,但有点不同。
我的第一个函数是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为
valueOfFunction
的使用首先是不合适的 - 您仅在调用的另一个函数(它是
方法。您应该使用良好的旧private static
成员)中使用它立即应用Enum.valueOf(String)
静态方法:请注意,提供不代表任何枚举值的 String 会抛出
IllegalArgumentException
,但在您当前的代码中,您在运行时有潜在的NullPointerException
(如果 Enum 常量不存在,Enums.valueOfFunction 返回 null),我想您已经意识到了这一点(如果您没有意识到,那么您现在已经意识到了) :))。此外,关于其余代码的建议很少。如果您不需要可变性,请使用 ImmutableLists 而不是 ArrayLists (我假设你不修改结果,因为
Collections.emptyList
本身是不可变的,所以如果您修改convertToRequests
方法的结果,它将在运行时失败)。一些解释:
如果您需要可变性,请将 ImmutableList.of 替换为 Lists.newArrayList 并保持方法签名不变。
I think usage of
valueOfFunction
is inappropriate in first place - you are using it only in another function (it'sprivate static
member) invokingapply
method immediately. You should use good oldEnum.valueOf(String)
static method:Note that supplying String that doesn't represent any of enum values throws
IllegalArgumentException
, but in your present code you have potentialNullPointerException
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 ofconvertToRequests
method).Some explanations:
In case you need mutability, replace ImmutableList.of with Lists.newArrayList and leave method signature intact.