英文:
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<T, S> {
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<myObjA> 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 <S, T, V> void load (String id, List<S> data, T converter, List<V> 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());
}
关键在于您只需要两个泛型参数,S
和 T
,并且可以将 Converter<S, T>
指定为参数类型(如果需要,添加变异)。
英文:
If you want to pass the List in:
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));
}
Otherwise, if you only pass in the input, you can use the Stream API and 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());
}
The Point is that you only need two generic parameters, S
and T
and can specify Converter<S,T>
as a parameter type (add variance if you need to).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论