Thursday, 12 September 2013

Is it possible to refactor these methods to avoid code duplication?

Is it possible to refactor these methods to avoid code duplication?

I have a bunch of utility-like methods, which looks very similar, like:
public static void addLeadingAttorney(EventAttorneyModel newAttorney,
List<EventAttorneyModel> existingAttorneys) {
for (EventAttorneyModel existingAttorney : existingAttorneys) {
existingAttorney.setSequence(existingAttorney.getSequence() + 1);
}
newAttorney.setSequence(1L);
existingAttorneys.add(0, newAttorney);
}
public static void addLeadingAttorney(CaseAttorneyModel newAttorney,
List<CaseAttorneyModel> existingAttorneys) {
for (CaseAttorneyModel existingAttorney : existingAttorneys) {
existingAttorney.setSequence(existingAttorney.getSequence() + 1);
}
newAttorney.setSequence(1L);
existingAttorneys.add(0, newAttorney);
}
Classes EventAttorneyModel and CaseAttorneyModel are JPA entities and do
not have common predecessors except for the Object class.
I wonder if there's a way I can get rid of duplicating the code, as there
will be many such methods in future?

No comments:

Post a Comment