I have a function that parse an object. But this function is required in two services and the parameter have same class name, but different package name. What i need is to avoid duplicated code.
Suppose the function is:
private HashMap<String, Integer> getPagination(PagingRequestType pagingRequestType) {
int pageSize = 200;
int pageNumber = 1;
if(pagingRequestType != null) {
if (pagingRequestType.getNumberOfRecordsPerPage() != 0) {
pageSize = pagingRequestType.getNumberOfRecordsPerPage();
}
if (pagingRequestType.getStartAtRecordNumber() != 0) {
pageNumber = pagingRequestType.getStartAtRecordNumber();
}
}
HashMap<String, Integer> result = new HashMap<>();
result.put("pageNumber", pageNumber);
result.put("pageSize", pageSize);
return result;
}
Possible function calls:
- getPagination(new Abc.PagingRequestType());
- getPagination(new Xyz.PagingRequestType());
PagingRequestType is an auto-generated class in two different packages. The function needs to be implemented once and used in both services.
Thanks.
PagingRequestType? That just screams to be fixed, especially if the two implementations are used the same.PagingRequestTypein two places.