如何使方法泛化,以适用于所有实体?

huangapple 未分类评论47阅读模式
英文:

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&lt;Integer, Map&lt;String, Double&gt;&gt; extract() throws IllegalAccessException {
	Field[] fields = PurchasingDeadlineValuesEntity.class.getDeclaredFields();
	Multimap&lt;Integer, Map&lt;String, Double&gt;&gt; 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&lt;Integer, Map&lt;String, Double&gt;&gt; extract(Class&lt;T&gt; clazz,	                                                                                  Function&lt;? super T, Object&gt; func) {
	Field[] fields = clazz.getDeclaredFields();
	Multimap&lt;Integer, Map&lt;String, Double&gt;&gt; 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&lt;Integer, Map&lt;String, Double&gt;&gt; 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?

huangapple
  • 本文由 发表于 2020年5月4日 20:42:00
  • 转载请务必保留本文链接:https://java.coder-hub.com/61592459.html
匿名

发表评论

匿名网友

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

确定