将一个方法转换为泛型方法

huangapple go评论73阅读模式
英文:

Convert a method to a generic one

问题

我创建了以下接口:

public interface ConverterInterface<T, S> {
    T convert(String id, S record, String event);
}

我的所有转换器类都实现了这个接口。假设我有多个转换器 A、B、C、D...

对于每个实现这个接口的类,我都有一个非常类似的加载方法,像这样:

private void loadA(String id, List<myObjA> objA, String event) {
    if (objA == null) {
        return;
    }

    // 这是一个实现了该接口的类(我有多个类似的类)
    MyConverterA converter = ...

    // 遍历元素,调用转换器并将其保存到输出列表中
    for (myObjA record : objA) {
        OUTPUT_LIST_A.add(converter.convert(id, record, event));
    }
}

private void loadB(...) { ... } 
private void loadC(...) { ... }

我的目标是将这些加载方法转换为通用方法,这样我就可以根据我的输入使用相同的方法。

我正在尝试类似这样的做法:

private <S, T, V> void load(String id, List<S> data, ConverterInterface<T, S> converter, List<V> output, String event) {
    if (data == null) {
        return;
    }

    for (S record : data) {
        // 在这里,我需要调用接口中的转换方法
        output.add(converter.convert(id, record, event));
    }
}

有什么想法吗?

谢谢!

英文:

I created the following interface:

public interface ConverterInterface&lt;T, S&gt; {
    T convert(String id, S record, String event);
}

And All my Converter classes are implementing this one. Let's say I have multiple converters A, B, C, D...

For each class implementing this interface, I have a very similar load method like this:

private void loadA(String id, List&lt;myObjA&gt; objA, String event) {
    if (objA == null) {
        return;
    }

    // This is one class that implements that interface (I have multiple classes like this)
    MyConverterA converter = ...

    // Run over elements, call the converter and save it into an output list
    for (myObjA record : objA) {
        OUTPUT_LIST_A.add(converter.convertToEntity(id, record, event));
    }
}

private void loadB(...) { ... } 
private void loadC(...) { ... }

My goal is to convert this several load methods into a generic one so I can use the same according to my input.

I'm trying something like this:

private &lt;S, T, V&gt; void load (String id, List&lt;S&gt; data, T converter, List&lt;V&gt; output, String event) {
    if (data == null) {
        return;
    }

    for (S record: data) {
        // Here I need to call the convert method from the interface
        output.add()
    }
}

Any ideas?

Thank you!

答案1

得分: 1

如果您想要传入列表:

private <S, T> void load(String id, List<S> data, ConverterInterface<T, S> converter, List<T> output, String event) {
    if (data == null)
        return;

    for (S record : data)
        output.add(converter.convert(id, record, event));
}

否则,如果您只传入输入,您可以使用流式 API 和 map

private <S, T> List<T> load(String id, List<S> data, ConverterInterface<T, S> converter, String event) {
    if (data == null)
        return Collections.emptyList();

    return data.stream()
        .map(record -> converter.convert(id, record, event))
        .collect(toList());
}

关键在于您只需要两个泛型参数,ST,并且可以将 Converter<S, T> 指定为参数类型(如果需要,添加变异)。

英文:

If you want to pass the List in:

private &lt;S, T&gt; void load (String id, List&lt;S&gt; data, ConverterInterface&lt;T, S&gt; converter, List&lt;T&gt; output, String event) {
    if (data == null)
        return;

    for (S record: data)
        output.add(converter.convert(id, record, event));
}

Otherwise, if you only pass in the input, you can use the Stream API and map:

private &lt;S, T&gt; List&lt;T&gt; load (String id, List&lt;S&gt; data, ConverterInterface&lt;T, S&gt; converter, String event) {
    if (data == null)
        return Collections.emptyList();

    return data.stream()
        .map(record -&gt; converter.convert(id, record, event))
        .collect(toList());
}

The Point is that you only need two generic parameters, S and T and can specify Converter&lt;S,T&gt; as a parameter type (add variance if you need to).

huangapple
  • 本文由 发表于 2020年10月20日 20:42:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64445413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定