Java泛型方法用于接受任何类并返回数据?

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

Java Generic method for accept any class and return data?

问题

在Spring Batch代码中,在编写batchUpdate时,我已经添加了异常处理,以防有任何记录失败,我正在使用下面的逻辑处理,这个逻辑运行良好。但是我在项目中有大约70个写入器,我想为下面的代码创建一个通用方法,并在运行时传递Employee、Department、Student、Stock等类,并获取详细信息。

我该如何做?
私有List getFailedRecords(Exception e, List<? extends Employee> items, Map<Integer, String> map){
List data = new ArrayList<>();
if (e.getCause() instanceof BatchUpdateException) {
BatchUpdateException be = (BatchUpdateException) e.getCause();
int[] batchRes = be.getUpdateCounts();
if (batchRes != null && batchRes.length > 0) {
for (int index = 0; index < batchRes.length; index++) {
if (batchRes[index] == Statement.EXECUTE_FAILED) {
data.add(items.get(index));
map.put(index, e.getMessage());
}
}
}
}
return data;
}

英文:

In Spring Batch code, while writing batchUpdate I have added exception handling, in case of any records failed I am handling with the below logic which works fine. But I've around 70 writer in my project and I wanted to make a generic method for the below code and at run time pass the Employee, Department, Student, Stock etc class and get the details back.

How can I do that ?

private List&lt;Employee&gt; getFailedRecords(Exception e, List&lt;? extends Employee&gt; items, Map&lt;Integer, String&gt; map){
	List&lt;Employee&gt; data = new ArrayList&lt;&gt;();
	if (e.getCause() instanceof BatchUpdateException) {
		BatchUpdateException be = (BatchUpdateException) e.getCause();
		int[] batchRes = be.getUpdateCounts();
		if (batchRes != null &amp;&amp; batchRes.length &gt; 0) {
			for (int index = 0; index &lt; batchRes.length; index++) {
				if (batchRes[index] == Statement.EXECUTE_FAILED) {
					data.add(items.get(index));
					map.put(index, e.getMessage());
				}
			}
		}
	}
	return data;
}

答案1

得分: 1

你可以简单地将这个方法变成通用的:

private <T> List<T> getFailedRecords(Exception e, List<? extends T> items, 
        Map<Integer, String> map) {
    
    List<T> data = new ArrayList<>();
    
    if (e.getCause() instanceof BatchUpdateException) {
        BatchUpdateException be = (BatchUpdateException) e.getCause();
        int[] batchRes = be.getUpdateCounts();
        if (batchRes != null && batchRes.length > 0) {
            for (int index = 0; index < batchRes.length; index++) {
                if (batchRes[index] == Statement.EXECUTE_FAILED) {
                    data.add(items.get(index));
                    map.put(index, e.getMessage());
                }
            }
        }
    }

    return data;
}

除非有必要使用有界通配符(例如,当您需要传递Employee/Student等子类的列表时),您应该可以省略它,并将第二个参数类型化为List<T>

英文:

You can simply make the method generic:

private &lt;T&gt; List&lt;T&gt; getFailedRecords(Exception e, List&lt;? extends T&gt; items, 
		Map&lt;Integer, String&gt; map) {
	
	List&lt;T&gt; data = new ArrayList&lt;&gt;();
	
	if (e.getCause() instanceof BatchUpdateException) {
		BatchUpdateException be = (BatchUpdateException) e.getCause();
		int[] batchRes = be.getUpdateCounts();
		if (batchRes != null &amp;&amp; batchRes.length &gt; 0) {
			for (int index = 0; index &lt; batchRes.length; index++) {
				if (batchRes[index] == Statement.EXECUTE_FAILED) {
					data.add(items.get(index));
					map.put(index, e.getMessage());
				}
			}
		}
	}

	return data;
}

Unless the bounded wildcard is necessary (such as when you need to pass a list of subclasses of Employee/Student, etc.), you should be able to dispense with it, and type the second parameter as List&lt;T&gt;.

huangapple
  • 本文由 发表于 2020年8月5日 13:26:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63258903.html
匿名

发表评论

匿名网友

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

确定