for (int i = 0; i < roomListSize; i++) {
String line = bufferedReader.readLine();
String[] lineTokens = splitByPipelineAndTrim(line, 6);
String[] roomTokens = splitBySpace(lineTokens[0], 2);
Room room = new Room();
room.setId(Long.parseLong(roomTokens[0]));
room.setName(roomTokens[1]);
room.setCapacity(Integer.parseInt(lineTokens[1]));
Department department = idToDepartmentMap.get(
Long.parseLong(lineTokens[2]));
room.setDepartment(department);
room.setGenderLimitation(GenderLimitation.valueOfCode(lineTokens[3]));
room.setBedList(new ArrayList<Bed>());
roomList.add(room);
idToRoomMap.put(room.getId(), room);
department.getRoomList().add(room);
String[] roomSpecialismTokens = splitBySpace(lineTokens[4]);
if (roomSpecialismTokens.length % 2 != 0) {
throw new IllegalArgumentException("Read line (" + line
+ ") is expected to contain even number of tokens (" + roomSpecialismTokens.length
+ ") after 4th pipeline (|) separated by a space ( ).");
}
List<RoomSpecialism> roomSpecialismListOfRoom = new ArrayList<RoomSpecialism>(roomSpecialismTokens.length / 2);
for (int j = 0; j < roomSpecialismTokens.length; j += 2) {
int priority = Integer.parseInt(roomSpecialismTokens[j]);
long specialismId = Long.parseLong(roomSpecialismTokens[j + 1]);
if (specialismId != 0) {
RoomSpecialism roomSpecialism = new RoomSpecialism();
roomSpecialism.setId(roomSpecialismId);
roomSpecialism.setRoom(room);
Specialism specialism = idToSpecialismMap.get(specialismId);
if (specialism == null) {
throw new IllegalArgumentException("Read line (" + line
+ ") has a non existing specialismId (" + specialismId + ").");
}
roomSpecialism.setSpecialism(specialism);
roomSpecialism.setPriority(priority);
roomSpecialismListOfRoom.add(roomSpecialism);
roomSpecialismList.add(roomSpecialism);
roomSpecialismId++;
}
}
room.setRoomSpecialismList(roomSpecialismListOfRoom);
List<RoomEquipment> roomEquipmentListOfRoom = new ArrayList<RoomEquipment>(equipmentListSize);
String[] roomEquipmentTokens = splitBySpace(lineTokens[5]);
if (roomEquipmentTokens.length != equipmentListSize) {
throw new IllegalArgumentException("Read line (" + line
+ ") is expected to contain equal number of tokens (" + roomEquipmentTokens.length
+ ") as equipmentListSize (" + equipmentListSize + ") after 5th pipeline (|).");
}
for (int j = 0; j < roomEquipmentTokens.length; j++) {
int hasEquipment = Integer.parseInt(roomEquipmentTokens[j]);
if (hasEquipment == 1) {
RoomEquipment roomEquipment = new RoomEquipment();
roomEquipment.setId(roomEquipmentId);
roomEquipment.setRoom(room);
roomEquipment.setEquipment(indexToEquipmentMap.get(j));
roomEquipmentListOfRoom.add(roomEquipment);
roomEquipmentList.add(roomEquipment);
roomEquipmentId++;
} else if (hasEquipment != 0) {
throw new IllegalArgumentException("Read line (" + line
+ ") is expected to have 0 or 1 hasEquipment (" + hasEquipment + ").");
}
}
room.setRoomEquipmentList(roomEquipmentListOfRoom);
}
Collections.sort(roomList, new Comparator<Room>() {
public int compare(Room a, Room b) {
return new CompareToBuilder()
.append(a.getDepartment().getId(), b.getDepartment().getId())