使用Java supplier / Function在静态方法中传递方法

时间:2018-05-12 04:01:47

标签: java java-8 supplier

我有几个课程:

public class TextContent {
    private String externalId;
}

public class ImageContent {
    private String externalImageId;
}

public static void validateImageInput(List<ImageContent> imageAssets, String requestId) {
    if(CollectionUtils.isEmpty(imageAssets)) {
        throw some Error;
    }

    Set<String> uniqueIds = imageAssets.stream().map(ImageContent::externalImageId).collect(Collectors.toSet());
    if(uniqueIds.size() != imageAssets().size()) {
        throw some Error;
    }

    //Do some processing
}

public static void validateTextInput(List<TextContent> textAssets, String requestId) {
    if(CollectionUtils.isEmpty(textAssets)) {
        throw some Error;
    }

    Set<String> uniqueIds = textAssets.stream().map(ImageContent::externalId).collect(Collectors.toSet());
    if(uniqueIds.size() != textAssets().size()) {
        throw some Error;
    }

    //Do some processing
}

正如您所看到的,这两个类的验证部分是相同的。我想尝试将其作为一种常用方法。为此:

public static void validateInput(List<?> assets, String requestId, Supplier<String> mapper) {
    if(CollectionUtils.isEmpty(assets)) {
        throw some error;
    }

    Set<String> uniqueIds = assets.stream().map(x -> mapper.get()).collect(Collectors.toSet());
    if(uniqueIds.size() != assets().size()) {
        throw some Error;
    }
}

然后用:

调用它
public static void validateAllInputs(List<ImageContent> imageAssets, List<TextContent> textAssets, String requestId) {
    validateInput(imageAssets, requestId, ImageContent::externalImageId);
    validateInput(textAssets, requestId, TextContent::externalId);
    doSomeProcessingWithText(textAssets, requestId);
    doSomeProcessingWithImage(imageAssets, requestId);
}

但我收到错误Non static method cannot be referenced from static context.

编辑: 我尝试的另一个选项是使用Function,即我传入<TextContent, String> mapper,在我的流中使用.map(x -> mapper.apply(x)。但是,当我尝试将其传递给函数validateInputs(textAsset, requestId, TextContent::externalId)时,我得到了相同的错误Non static method cannot be referenced from static context.

2 个答案:

答案 0 :(得分:0)

我建议让这两种类型实现相同的界面:

validateInput

然后将public static void validateInput(List<? extends Content> assets, String requestId) { if(CollectionUtils.isEmpty(assets)) { throw some error; } Set<String> uniqueIds = assets.stream().map(Content::externalId).collect(Collectors.toSet()); if(uniqueIds.size() != assets.size()) { throw some error; } //Do some processing } 签名更改为:

public static <E> void commonValidate(List<E> assets, String requestId, Function<E, String> mapper) {
    Set<String> uniqueIds = assets.stream().map(mapper).collect(Collectors.toSet());
}

如果你仍想在这里提取一个常用方法,你可以写一个这样的方法:

a.go

答案 1 :(得分:0)

在泛型的帮助下完成:

__get__

然后使用:

调用它
public static <T> void validateInput(List<T> assets, String requestId, Function<T, String> mapper) {
    if(CollectionUtils.isEmpty(assets)) {
        throw some error;
    }

    Set<String> uniqueIds = assets.stream().map(mapper).collect(Collectors.toSet());
    if(uniqueIds.size() != assets().size()) {
        throw some Error;
    }
}