英文:
Java Generic method for accept any class and return data?
问题
在Spring Batch代码中,在编写batchUpdate时,我已经添加了异常处理,以防有任何记录失败,我正在使用下面的逻辑处理,这个逻辑运行良好。但是我在项目中有大约70个写入器,我想为下面的代码创建一个通用方法,并在运行时传递Employee、Department、Student、Stock等类,并获取详细信息。
我该如何做?
私有List
List
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<Employee> getFailedRecords(Exception e, List<? extends Employee> items, Map<Integer, String> map){
List<Employee> 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;
}
答案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 <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;
}
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<T>
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论