英文:
How to make method generic, that works for all Entities?
问题
以下是您要翻译的内容:
我在代码中定义了几个DeadlineEntities
。现在我有一个方法,我想将其应用于所有这些DeadlineEntities
。为了不必为每个实体编写一个方法,我想以通用的方式解决这个问题。
代码
protected Multimap<Integer, Map<String, Double>> extract() throws IllegalAccessException {
Field[] fields = PurchasingDeadlineValuesEntity.class.getDeclaredFields();
Multimap<Integer, Map<String, Double>> map = ArrayListMultimap.create();
for (MandantKagAccountEntity mandantEntity : m_k_a_E) {
PurchasingDeadlineValuesEntity deadlineEntity = mandantEntity.getPurchasingDeadlineValuesEntity();
if (deadlineEntity != null) {
map.put(mandantEntity.getDb_5(), extractKagAndDeadlines(fields, deadlineEntity));
}
}
return map;
}
现在,我在部分情况下已经成功以通用方式构建了它,但我无法进一步进行。
我的尝试
protected Multimap<Integer, Map<String, Double>> extract(Class<T> clazz, Function<? super T, Object> func) {
Field[] fields = clazz.getDeclaredFields();
Multimap<Integer, Map<String, Double>> map = ArrayListMultimap.create();
for (MandantKagAccountEntity mandantKagAccountEntity : m_k_a_E) {
}
return map;
}
有人知道我现在应该如何继续吗?我如何从mandantKagAccountEntity
获取对象并检查对象是否不为null
?
然后,我会这样调用该方法:
protected Multimap<Integer, Map<String, Double>> getMap() {
return this.extract(PurchasingDeadlineValuesEntity.class, MandantKagAccountEntity::getPurchasingDeadlineValuesEntity);
}
但是这里会报错,提示方法不是静态的。
有人可以告诉我我做错了什么,并帮助我吗?
英文:
I have defined several DeadlineEntities
in my code. Now I have a method that I want to apply to all these DeadlineEntities
. In order not to write a method for each entity I thought to solve this generically.
Code
protected Multimap<Integer, Map<String, Double>> extract() throws IllegalAccessException {
Field[] fields = PurchasingDeadlineValuesEntity.class.getDeclaredFields();
Multimap<Integer, Map<String, Double>> map = ArrayListMultimap.create();
for (MandantKagAccountEntity mandantEntity : m_k_a_E) {
PurchasingDeadlineValuesEntity deadlineEntity = mandantEntity.getPurchasingDeadlineValuesEntity();
if (deadlineEntity != null) {
map.put(mandantEntity.getDb_5(), extractKagAndDeadlines(fields, deadlineEntity));
}
}
return map;
}
Now I have partly managed to build it up generically but I can't get any further.
My attempt
protected Multimap<Integer, Map<String, Double>> extract(Class<T> clazz, Function<? super T, Object> func) {
Field[] fields = clazz.getDeclaredFields();
Multimap<Integer, Map<String, Double>> map = ArrayListMultimap.create();
for (MandantKagAccountEntity mandantKagAccountEntity : m_k_a_E) {
}
return map;
}
Does anyone know how I should proceed now? How do I get the object from the mandantKagAccountEntity and check if the object is not null
?
I would then call the method like this:
protected Multimap<Integer, Map<String, Double>> getMap() {
return this.extract(PurchasingDeadlineValuesEntity.class, MandantKagAccountEntity::getPurchasingDeadlineValuesEntity);
}
but here I get an error that the method is not static.
Can anyone tell me what I'm doing wrong and help me?
专注分享java语言的经验与见解,让所有开发者获益!
评论