Reputation: 4672
I have AuditLog entity class which Im saving to DB using the method insertAuditLogs based on LogType enum using multiple if calls
private void processData(LogData logObj, LogType type)
{
if (type.toString().equalsIgnoreCase(LogType.LOG1_PROCESSING.toString()))
{
AuditLog audObj = new AuditLog();
audObj.setImportId(logObj.id);
audObj.setLogMessage(logObj.description);
audObj.setEntryDateTimestampJoda(null);
audObj.setExitDateTimestampJoda(logObj.getExitDateTime());
insertAuditLogs(audObj);
}
if (type.toString().equalsIgnoreCase(LogType.LOG2_PROCESSING.toString()))
{
AuditLog audObj = new AuditLog();
audObj.setImportId(logObj.id);
audObj.setLogMessage(logObj.description);
audObj.setEntryDateTimestampJoda(logObj.getStartDateTime());
audObj.setExitDateTimestampJoda(logObj.getExitDateTime());
insertAuditLogs(audObj);
}
}
Question
1 How can I use Replace Method with Method Object or Extact to a Class or a Parameter Object to refactor the above calls and improve the refactoring ?
Upvotes: 0
Views: 44
Reputation: 98
I'm not sure how the refactoring patterns you mention may apply in this case, but it looks to me (?) like the bodies of your if
blocks are all the same. If that is the case in your real application, you should be able to simplify to:
private void processData(LogData logObj, LogType type) {
AuditLog audObj = new AuditLog();
audObj.setImportId(logObj.id);
audObj.setLogMessage(logObj.description);
audObj.setEntryDateTimestampJoda(logObj.getStartDateTime());
audObj.setExitDateTimestampJoda(logObj.getExitDateTime());
insertAuditLogs(audObj);
}
But if there are other LogType
values that are not shown that you do not want logged, I suppose you would need to filter those out.
Also, here is a simpler way to compare enum
values if you still need to do that:
if (type == LogType.LOG2_PROCESSING)
Hope this helps!
Upvotes: 3