I wrote code which sorts items, based on parameters (IconConfiguration). I have a map of items which have index and item so basically a predefined list on which index item should be, and maxPositions parameter which is kind of a limit of how big my list can be. I wrote a code but its repetitive and pretty ugly when i check it, but i am unable to get idea on what to refactor.
private List<GWBaseData> applyItemsIndex(List<GWBaseData> gwInputBaseDataList) { //TODO: Refactor repetitive code?
List<GWBaseData> gwBaseDataList = new ArrayList<>();
if (iconConfiguration.getMax_positions() != -1) {
for (int i = 0; i < gwInputBaseDataList.size(); i++) {
if (gwBaseDataList.size() < iconConfiguration.getMax_positions()) {
if (getItemsMap().get(i) != null && getBaseDataInListById(gwInputBaseDataList, getItemsMap().get(i)) != null) {
gwBaseDataList.add(i, getBaseDataInListById(gwInputBaseDataList, getItemsMap().get(i)));
} else {
for (int j = 0; j < gwInputBaseDataList.size(); j++) {
if (!gwBaseDataList.contains(gwInputBaseDataList.get(j))) {
gwBaseDataList.add(gwInputBaseDataList.get(j));
break;
}
}
}
}
}
} else {
for (int i = 0; i < gwInputBaseDataList.size(); i++) {
if (getItemsMap().get(i) != null && getBaseDataInListById(gwInputBaseDataList, getItemsMap().get(i)) != null) {
gwBaseDataList.add(i, getBaseDataInListById(gwInputBaseDataList, getItemsMap().get(i)));
} else {
for (int j = 0; j < gwInputBaseDataList.size(); j++) {
if (!gwBaseDataList.contains(gwInputBaseDataList.get(j))) {
gwBaseDataList.add(gwInputBaseDataList.get(j));
break;
}
}
}
}
}
return gwBaseDataList;
}
So basically the whole difference between both block is only one additional if (if (gwBaseDataList.size() < iconConfiguration.getMax_positions())
)