1

我有几节课:

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.

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

4

2 回答 2

0

我建议让这两种类型都实现相同的接口:

public interface Content {
    public String getExternalId();

    public static String externalId(Content content) {
        return content == null ? null : content.getExternalId();
    }
}

然后将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());
}
于 2018-05-12T08:43:22.710 回答
0

在泛型的帮助下完成此操作:

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;
    }
}

然后使用以下命令调用它:

validateInput(imageAssets, requestId, ImageContent::externalImageId);
于 2018-05-12T22:33:05.507 回答