List<Bed> bedList = patientAdmissionSchedule.getBedList();
List<Move> moveList = new ArrayList<Move>();
// For every 2 distinct beds
for (ListIterator<Bed> leftBedIt = bedList.listIterator(); leftBedIt.hasNext();) {
Bed leftBed = leftBedIt.next();
for (ListIterator<Bed> rightBedIt = bedList.listIterator(leftBedIt.nextIndex());
rightBedIt.hasNext();) {
Bed rightBed = rightBedIt.next();
List<BedDesignation> leftBedDesignationList = bedToBedDesignationList.get(leftBed);
if (leftBedDesignationList == null) {
leftBedDesignationList = Collections.emptyList();
}
List<BedDesignation> rightBedDesignationList = bedToBedDesignationList.get(rightBed);
if (rightBedDesignationList == null) {
rightBedDesignationList = Collections.emptyList();
}
LowestFirstNightBedDesignationIterator lowestIt = new LowestFirstNightBedDesignationIterator(
leftBedDesignationList, rightBedDesignationList);
// For every pillar part duo
while (lowestIt.hasNext()) {
BedDesignation pillarPartBedDesignation = lowestIt.next();
// Note: the initialCapacity is probably to high,
// which is bad for memory, but the opposite is bad for performance (which is worse)
List<Move> moveListByPillarPartDuo = new ArrayList<Move>(
leftBedDesignationList.size() + rightBedDesignationList.size());
int lastNightIndex = pillarPartBedDesignation.getAdmissionPart().getLastNight().getIndex();
Bed otherBed;
int leftMinimumFirstNightIndex = Integer.MIN_VALUE;
int rightMinimumFirstNightIndex = Integer.MIN_VALUE;
if (lowestIt.isLastNextWasLeft()) {
otherBed = rightBed;
leftMinimumFirstNightIndex = lastNightIndex;