English 中文(简体)
瓜瓦职能构成
原标题:Guava Function Composition
  • 时间:2012-01-12 20:39:53
  •  标签:
  • java
  • guava

我看到,建立<条码>功能和字典”;A,C>,构成<条码>功能和字典;A,B>和<条码>功能和t;B,C>。

我的情况类似,但差别不大。

我的第一个职能是 ValueOfFunction,即根据我的<代码>中的关键内容,回归一 en。 BO。

焚烧功能在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);
    }

是否为这两个职能共同组成了更nic的方法?

最佳回答

我认为,首先使用<条码>价值/代码>是不适当的——你在另一个职能(<条码> 私人静态成员)中直接使用<条码>。 您应使用优老的<代码>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);
    }
}

说明:供应 Str represent values values throw Enums. ValueOfFunction 回去除,如果Enum的不变值不存在,而且我相信你知道(如果你是 t,现在你就是:”)。

此外,对法典其余部分的建议很少。 如果你不需变换,则使用ImmutableLists ,而不是ArrayLists(我假定你不会修改结果,因为Collections.emptyList 是不可改变的,如果你将修改

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. 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.

如果您需要交替,则以名单取代左手党。 射线器和休假方法的签字无损。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签